David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 10 | use punctuated::Punctuated; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 11 | use proc_macro2::{Span, TokenStream}; |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 12 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 13 | use std::hash::{Hash, Hasher}; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 14 | #[cfg(feature = "extra-traits")] |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 15 | use tt::TokenStreamHelper; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 16 | #[cfg(feature = "full")] |
| 17 | use std::mem; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 18 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 19 | ast_enum_of_structs! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 20 | /// A Rust expression. |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 21 | pub enum Expr { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 22 | /// A box expression: `box f`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 23 | pub Box(ExprBox #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 24 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 25 | pub box_token: Token![box], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 26 | pub expr: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 27 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 28 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 29 | /// A placement expression: `place <- value`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 30 | pub InPlace(ExprInPlace #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 31 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 32 | pub place: Box<Expr>, |
David Tolnay | 8701a5c | 2017-12-28 23:31:10 -0500 | [diff] [blame] | 33 | pub arrow_token: Token![<-], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 34 | pub value: Box<Expr>, |
| 35 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 36 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 37 | /// A slice literal expression: `[a, b, c, d]`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 38 | pub Array(ExprArray #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 39 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 40 | pub bracket_token: token::Bracket, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 41 | pub elems: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 42 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 43 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 44 | /// A function call expression: `invoke(a, b)`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 45 | pub Call(ExprCall { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 46 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 47 | pub func: Box<Expr>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 48 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 49 | pub args: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 50 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 51 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 52 | /// A method call expression: `x.foo::<T>(a, b)`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 53 | pub MethodCall(ExprMethodCall #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 54 | pub attrs: Vec<Attribute>, |
David Tolnay | 7641851 | 2017-12-28 23:47:47 -0500 | [diff] [blame] | 55 | pub receiver: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 56 | pub dot_token: Token![.], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 57 | pub method: Ident, |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 58 | pub turbofish: Option<MethodTurbofish>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 59 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 60 | pub args: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 61 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 62 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 63 | /// A tuple expression: `(a, b, c, d)`. |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 64 | pub Tuple(ExprTuple #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 65 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 66 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 67 | pub elems: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 68 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 69 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 70 | /// A binary operation: `a + b`, `a * b`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 71 | pub Binary(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 72 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 73 | pub left: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 74 | pub op: BinOp, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 75 | pub right: Box<Expr>, |
| 76 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 77 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 78 | /// A unary operation: `!x`, `*x`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 79 | pub Unary(ExprUnary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 80 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 81 | pub op: UnOp, |
| 82 | pub expr: Box<Expr>, |
| 83 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 84 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 85 | /// A literal in place of an expression: `1`, `"foo"`. |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 86 | pub Lit(ExprLit { |
| 87 | pub attrs: Vec<Attribute>, |
| 88 | pub lit: Lit, |
| 89 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 90 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 91 | /// A cast expression: `foo as f64`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 92 | pub Cast(ExprCast { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 93 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 94 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 95 | pub as_token: Token![as], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 96 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 97 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 98 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 99 | /// A type ascription expression: `foo: f64`. |
David Tolnay | 0cf94f2 | 2017-12-28 23:46:26 -0500 | [diff] [blame] | 100 | pub Type(ExprType #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 101 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 102 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 103 | pub colon_token: Token![:], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 104 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 105 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 106 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 107 | /// An `if` expression with an optional `else` block: `if expr { ... } |
| 108 | /// else { ... }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 109 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 110 | /// The `else` branch expression may only be an `If`, `IfLet`, or |
| 111 | /// `Block` expression, not any of the other types of expression. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 112 | pub If(ExprIf #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 113 | pub attrs: Vec<Attribute>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 114 | pub if_token: Token![if], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 115 | pub cond: Box<Expr>, |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 116 | pub then_branch: Block, |
| 117 | pub else_branch: Option<(Token![else], Box<Expr>)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 118 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 119 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 120 | /// An `if let` expression with an optional `else` block: `if let pat = |
| 121 | /// expr { ... } else { ... }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 122 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 123 | /// The `else` branch expression may only be an `If`, `IfLet`, or |
| 124 | /// `Block` expression, not any of the other types of expression. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 125 | pub IfLet(ExprIfLet #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 126 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 127 | pub if_token: Token![if], |
| 128 | pub let_token: Token![let], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 129 | pub pat: Box<Pat>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 130 | pub eq_token: Token![=], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 131 | pub expr: Box<Expr>, |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 132 | pub then_branch: Block, |
| 133 | pub else_branch: Option<(Token![else], Box<Expr>)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 134 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 135 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 136 | /// A while loop: `while expr { ... }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 137 | pub While(ExprWhile #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 138 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 139 | pub label: Option<Label>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 140 | pub while_token: Token![while], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 141 | pub cond: Box<Expr>, |
| 142 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 143 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 144 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 145 | /// A while-let loop: `while let pat = expr { ... }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 146 | pub WhileLet(ExprWhileLet #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 147 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 148 | pub label: Option<Label>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 149 | pub while_token: Token![while], |
| 150 | pub let_token: Token![let], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 151 | pub pat: Box<Pat>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 152 | pub eq_token: Token![=], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 153 | pub expr: Box<Expr>, |
| 154 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 155 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 156 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 157 | /// A for loop: `for pat in expr { ... }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 158 | pub ForLoop(ExprForLoop #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 159 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 160 | pub label: Option<Label>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 161 | pub for_token: Token![for], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 162 | pub pat: Box<Pat>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 163 | pub in_token: Token![in], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 164 | pub expr: Box<Expr>, |
| 165 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 166 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 167 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 168 | /// Conditionless loop: `loop { ... }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 169 | pub Loop(ExprLoop #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 170 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 171 | pub label: Option<Label>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 172 | pub loop_token: Token![loop], |
| 173 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 174 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 175 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 176 | /// A `match` expression: `match n { Some(n) => {}, None => {} }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 177 | pub Match(ExprMatch #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 178 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 179 | pub match_token: Token![match], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 180 | pub expr: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 181 | pub brace_token: token::Brace, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 182 | pub arms: Vec<Arm>, |
| 183 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 184 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 185 | /// A closure expression: `|a, b| a + b`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 186 | pub Closure(ExprClosure #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 187 | pub attrs: Vec<Attribute>, |
David Tolnay | efc96fb | 2017-12-29 02:03:15 -0500 | [diff] [blame] | 188 | pub capture: Option<Token![move]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 189 | pub or1_token: Token![|], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 190 | pub inputs: Punctuated<FnArg, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 191 | pub or2_token: Token![|], |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame] | 192 | pub output: ReturnType, |
| 193 | pub body: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 194 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 195 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 196 | /// An unsafe block: `unsafe { ... }`. |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 197 | pub Unsafe(ExprUnsafe #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 198 | pub attrs: Vec<Attribute>, |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 199 | pub unsafe_token: Token![unsafe], |
| 200 | pub block: Block, |
| 201 | }), |
| 202 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 203 | /// A blocked scope: `{ ... }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 204 | pub Block(ExprBlock #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 205 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 206 | pub block: Block, |
| 207 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 208 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 209 | /// An assignment expression: `a = compute()`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 210 | pub Assign(ExprAssign #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 211 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 212 | pub left: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 213 | pub eq_token: Token![=], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 214 | pub right: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 215 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 216 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 217 | /// A compound assignment expression: `counter += 1`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 218 | pub AssignOp(ExprAssignOp #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 219 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 220 | pub left: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 221 | pub op: BinOp, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 222 | pub right: Box<Expr>, |
| 223 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 224 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 225 | /// Access of a named struct field (`obj.k`) or unnamed tuple struct |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 226 | /// field (`obj.0`). |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 227 | pub Field(ExprField #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 228 | pub attrs: Vec<Attribute>, |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 229 | pub base: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 230 | pub dot_token: Token![.], |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 231 | pub member: Member, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 232 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 233 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame^] | 234 | /// A square bracketed indexing expression: `vector[2]`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 235 | pub Index(ExprIndex { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 236 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 237 | pub expr: Box<Expr>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 238 | pub bracket_token: token::Bracket, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 239 | pub index: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 240 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 241 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 242 | /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 243 | pub Range(ExprRange #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 244 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 245 | pub from: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 246 | pub limits: RangeLimits, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 247 | pub to: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 248 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 249 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 250 | /// A path like `std::mem::replace` possibly containing generic |
| 251 | /// parameters and a qualified self-type. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 252 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 253 | /// A plain identifier like `x` is a path of length 1. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 254 | pub Path(ExprPath { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 255 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 256 | pub qself: Option<QSelf>, |
| 257 | pub path: Path, |
| 258 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 259 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 260 | /// A referencing operation: `&a` or `&mut a`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 261 | pub AddrOf(ExprAddrOf #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 262 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 263 | pub and_token: Token![&], |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 264 | pub mutability: Option<Token![mut]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 265 | pub expr: Box<Expr>, |
| 266 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 267 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 268 | /// A `break`, with an optional label to break and an optional |
| 269 | /// expression. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 270 | pub Break(ExprBreak #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 271 | pub attrs: Vec<Attribute>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 272 | pub break_token: Token![break], |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 273 | pub label: Option<Lifetime>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 274 | pub expr: Option<Box<Expr>>, |
| 275 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 276 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 277 | /// A `continue`, with an optional label. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 278 | pub Continue(ExprContinue #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 279 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 280 | pub continue_token: Token![continue], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 281 | pub label: Option<Lifetime>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 282 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 283 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 284 | /// A `return`, with an optional value to be returned. |
David Tolnay | c246cd3 | 2017-12-28 23:14:32 -0500 | [diff] [blame] | 285 | pub Return(ExprReturn #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 286 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 287 | pub return_token: Token![return], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 288 | pub expr: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 289 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 290 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 291 | /// A macro invocation expression: `format!("{}", q)`. |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 292 | pub Macro(ExprMacro #full { |
| 293 | pub attrs: Vec<Attribute>, |
| 294 | pub mac: Macro, |
| 295 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 296 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 297 | /// A struct literal expression: `Point { x: 1, y: 1 }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 298 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 299 | /// The `rest` provides the value of the remaining fields as in `S { a: |
| 300 | /// 1, b: 1, ..rest }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 301 | pub Struct(ExprStruct #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 302 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 303 | pub path: Path, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 304 | pub brace_token: token::Brace, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 305 | pub fields: Punctuated<FieldValue, Token![,]>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 306 | pub dot2_token: Option<Token![..]>, |
| 307 | pub rest: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 308 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 309 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 310 | /// An array literal constructed from one repeated element: `[0u8; N]`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 311 | pub Repeat(ExprRepeat #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 312 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 313 | pub bracket_token: token::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 314 | pub expr: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 315 | pub semi_token: Token![;], |
David Tolnay | 84d8044 | 2018-01-07 01:03:20 -0800 | [diff] [blame] | 316 | pub len: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 317 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 318 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 319 | /// A parenthesized expression: `(a + b)`. |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 320 | pub Paren(ExprParen #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 321 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 322 | pub paren_token: token::Paren, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 323 | pub expr: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 324 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 325 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 326 | /// An expression contained within invisible delimiters. |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 327 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 328 | /// This variant is important for faithfully representing the precedence |
| 329 | /// of expressions and is related to `None`-delimited spans in a |
| 330 | /// `TokenStream`. |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 331 | pub Group(ExprGroup #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 332 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 333 | pub group_token: token::Group, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 334 | pub expr: Box<Expr>, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 335 | }), |
| 336 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 337 | /// A try-expression: `expr?`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 338 | pub Try(ExprTry #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 339 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 340 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 341 | pub question_token: Token![?], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 342 | }), |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 343 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 344 | /// A catch expression: `do catch { ... }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 345 | pub Catch(ExprCatch #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 346 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 347 | pub do_token: Token![do], |
| 348 | pub catch_token: Token![catch], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 349 | pub block: Block, |
| 350 | }), |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 351 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 352 | /// A yield expression: `yield expr`. |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 353 | pub Yield(ExprYield #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 354 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 355 | pub yield_token: Token![yield], |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 356 | pub expr: Option<Box<Expr>>, |
| 357 | }), |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 358 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 359 | /// Tokens in expression position not interpreted by Syn. |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 360 | pub Verbatim(ExprVerbatim #manual_extra_traits { |
| 361 | pub tts: TokenStream, |
| 362 | }), |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | #[cfg(feature = "extra-traits")] |
| 367 | impl Eq for ExprVerbatim {} |
| 368 | |
| 369 | #[cfg(feature = "extra-traits")] |
| 370 | impl PartialEq for ExprVerbatim { |
| 371 | fn eq(&self, other: &Self) -> bool { |
| 372 | TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | #[cfg(feature = "extra-traits")] |
| 377 | impl Hash for ExprVerbatim { |
| 378 | fn hash<H>(&self, state: &mut H) |
| 379 | where |
| 380 | H: Hasher, |
| 381 | { |
| 382 | TokenStreamHelper(&self.tts).hash(state); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 383 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 384 | } |
| 385 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 386 | impl Expr { |
| 387 | // Not public API. |
| 388 | #[doc(hidden)] |
David Tolnay | 096d498 | 2017-12-28 23:18:18 -0500 | [diff] [blame] | 389 | #[cfg(feature = "full")] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 390 | pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 391 | match *self { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 392 | Expr::Box(ExprBox { ref mut attrs, .. }) |
| 393 | | Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
| 394 | | Expr::Array(ExprArray { ref mut attrs, .. }) |
| 395 | | Expr::Call(ExprCall { ref mut attrs, .. }) |
| 396 | | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
| 397 | | Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
| 398 | | Expr::Binary(ExprBinary { ref mut attrs, .. }) |
| 399 | | Expr::Unary(ExprUnary { ref mut attrs, .. }) |
| 400 | | Expr::Lit(ExprLit { ref mut attrs, .. }) |
| 401 | | Expr::Cast(ExprCast { ref mut attrs, .. }) |
| 402 | | Expr::Type(ExprType { ref mut attrs, .. }) |
| 403 | | Expr::If(ExprIf { ref mut attrs, .. }) |
| 404 | | Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
| 405 | | Expr::While(ExprWhile { ref mut attrs, .. }) |
| 406 | | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
| 407 | | Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
| 408 | | Expr::Loop(ExprLoop { ref mut attrs, .. }) |
| 409 | | Expr::Match(ExprMatch { ref mut attrs, .. }) |
| 410 | | Expr::Closure(ExprClosure { ref mut attrs, .. }) |
| 411 | | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
| 412 | | Expr::Block(ExprBlock { ref mut attrs, .. }) |
| 413 | | Expr::Assign(ExprAssign { ref mut attrs, .. }) |
| 414 | | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
| 415 | | Expr::Field(ExprField { ref mut attrs, .. }) |
| 416 | | Expr::Index(ExprIndex { ref mut attrs, .. }) |
| 417 | | Expr::Range(ExprRange { ref mut attrs, .. }) |
| 418 | | Expr::Path(ExprPath { ref mut attrs, .. }) |
| 419 | | Expr::AddrOf(ExprAddrOf { ref mut attrs, .. }) |
| 420 | | Expr::Break(ExprBreak { ref mut attrs, .. }) |
| 421 | | Expr::Continue(ExprContinue { ref mut attrs, .. }) |
| 422 | | Expr::Return(ExprReturn { ref mut attrs, .. }) |
| 423 | | Expr::Macro(ExprMacro { ref mut attrs, .. }) |
| 424 | | Expr::Struct(ExprStruct { ref mut attrs, .. }) |
| 425 | | Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
| 426 | | Expr::Paren(ExprParen { ref mut attrs, .. }) |
| 427 | | Expr::Group(ExprGroup { ref mut attrs, .. }) |
| 428 | | Expr::Try(ExprTry { ref mut attrs, .. }) |
| 429 | | Expr::Catch(ExprCatch { ref mut attrs, .. }) |
| 430 | | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new), |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 431 | Expr::Verbatim(_) => { |
| 432 | // TODO |
| 433 | Vec::new() |
| 434 | } |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 439 | ast_enum! { |
| 440 | /// A struct or tuple struct field accessed in a struct literal or field |
| 441 | /// expression. |
| 442 | pub enum Member { |
| 443 | /// A named field like `self.x`. |
| 444 | Named(Ident), |
| 445 | /// An unnamed field like `self.0`. |
| 446 | Unnamed(Index), |
| 447 | } |
| 448 | } |
| 449 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 450 | ast_struct! { |
| 451 | /// The index of an unnamed tuple struct field. |
| 452 | pub struct Index #manual_extra_traits { |
| 453 | pub index: u32, |
| 454 | pub span: Span, |
| 455 | } |
| 456 | } |
| 457 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 458 | impl From<usize> for Index { |
| 459 | fn from(index: usize) -> Index { |
| 460 | assert!(index < std::u32::MAX as usize); |
| 461 | Index { |
| 462 | index: index as u32, |
| 463 | span: Span::default(), |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 469 | impl Eq for Index {} |
| 470 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 471 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 472 | impl PartialEq for Index { |
| 473 | fn eq(&self, other: &Self) -> bool { |
| 474 | self.index == other.index |
| 475 | } |
| 476 | } |
| 477 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 478 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 479 | impl Hash for Index { |
| 480 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 481 | self.index.hash(state); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 486 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 487 | /// The `::<>` explicit type parameters passed to a method call: |
| 488 | /// `parse::<u64>()`. |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 489 | pub struct MethodTurbofish { |
| 490 | pub colon2_token: Token![::], |
| 491 | pub lt_token: Token![<], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 492 | pub args: Punctuated<GenericMethodArgument, Token![,]>, |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 493 | pub gt_token: Token![>], |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | #[cfg(feature = "full")] |
| 498 | ast_enum! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 499 | /// An individual generic argument to a method, like `T`. |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 500 | pub enum GenericMethodArgument { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 501 | /// A type argument. |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 502 | Type(Type), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 503 | /// A const expression. Must be inside of a block. |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 504 | /// |
| 505 | /// NOTE: Identity expressions are represented as Type arguments, as |
| 506 | /// they are indistinguishable syntactically. |
| 507 | Const(Expr), |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | #[cfg(feature = "full")] |
| 512 | ast_struct! { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 513 | /// A field-value pair in a struct literal. |
| 514 | pub struct FieldValue { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 515 | /// Attributes tagged on the field. |
| 516 | pub attrs: Vec<Attribute>, |
| 517 | |
| 518 | /// Name or index of the field. |
| 519 | pub member: Member, |
| 520 | |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 521 | /// The colon in `Struct { x: x }`. If written in shorthand like |
| 522 | /// `Struct { x }`, there is no colon. |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 523 | pub colon_token: Option<Token![:]>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 524 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 525 | /// Value of the field. |
| 526 | pub expr: Expr, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 527 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 530 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 531 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 532 | /// A lifetime labeling a `for`, `while`, or `loop`. |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 533 | pub struct Label { |
| 534 | pub name: Lifetime, |
| 535 | pub colon_token: Token![:], |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | #[cfg(feature = "full")] |
| 540 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 541 | /// A braced block containing Rust statements. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 542 | pub struct Block { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 543 | pub brace_token: token::Brace, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 544 | /// Statements in a block |
| 545 | pub stmts: Vec<Stmt>, |
| 546 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 549 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 550 | ast_enum! { |
| 551 | /// A statement, usually ending in a semicolon. |
| 552 | pub enum Stmt { |
| 553 | /// A local (let) binding. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 554 | Local(Local), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 555 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 556 | /// An item definition. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 557 | Item(Item), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 558 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 559 | /// Expr without trailing semicolon. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 560 | Expr(Expr), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 561 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 562 | /// Expression with trailing semicolon. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 563 | Semi(Expr, Token![;]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 564 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 567 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 568 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 569 | /// A local `let` binding: `let x: u64 = s.parse()?`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 570 | pub struct Local { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 571 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 572 | pub let_token: Token![let], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 573 | pub pat: Box<Pat>, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 574 | pub ty: Option<(Token![:], Box<Type>)>, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 575 | pub init: Option<(Token![=], Box<Expr>)>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 576 | pub semi_token: Token![;], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 577 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 580 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 581 | ast_enum_of_structs! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 582 | /// A pattern in a local binding, function signature, match expression, or |
| 583 | /// various other places. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 584 | // Clippy false positive |
| 585 | // https://github.com/Manishearth/rust-clippy/issues/1241 |
| 586 | #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))] |
| 587 | pub enum Pat { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 588 | /// A pattern that matches any value: `_`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 589 | pub Wild(PatWild { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 590 | pub underscore_token: Token![_], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 591 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 592 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 593 | /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 594 | pub Ident(PatIdent { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 595 | pub by_ref: Option<Token![ref]>, |
| 596 | pub mutability: Option<Token![mut]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 597 | pub ident: Ident, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 598 | pub subpat: Option<(Token![@], Box<Pat>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 599 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 600 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 601 | /// A struct or struct variant pattern: `Variant { x, y, .. }`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 602 | pub Struct(PatStruct { |
| 603 | pub path: Path, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 604 | pub brace_token: token::Brace, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 605 | pub fields: Punctuated<FieldPat, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 606 | pub dot2_token: Option<Token![..]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 607 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 608 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 609 | /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 610 | pub TupleStruct(PatTupleStruct { |
| 611 | pub path: Path, |
| 612 | pub pat: PatTuple, |
| 613 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 614 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 615 | /// A path pattern like `Color::Red`, optionally qualified with a |
| 616 | /// self-type. |
| 617 | /// |
| 618 | /// Unquailfied path patterns can legally refer to variants, structs, |
| 619 | /// constants or associated constants. Quailfied path patterns like |
| 620 | /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to |
| 621 | /// associated constants. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 622 | pub Path(PatPath { |
| 623 | pub qself: Option<QSelf>, |
| 624 | pub path: Path, |
| 625 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 626 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 627 | /// A tuple pattern: `(a, b)`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 628 | pub Tuple(PatTuple { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 629 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 630 | pub front: Punctuated<Pat, Token![,]>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 631 | pub dot2_token: Option<Token![..]>, |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 632 | pub comma_token: Option<Token![,]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 633 | pub back: Punctuated<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 634 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 635 | |
| 636 | /// A box pattern: `box v`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 637 | pub Box(PatBox { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 638 | pub box_token: Token![box], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 639 | pub pat: Box<Pat>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 640 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 641 | |
| 642 | /// A reference pattern: `&mut (first, second)`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 643 | pub Ref(PatRef { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 644 | pub and_token: Token![&], |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 645 | pub mutability: Option<Token![mut]>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 646 | pub pat: Box<Pat>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 647 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 648 | |
| 649 | /// A literal pattern: `0`. |
| 650 | /// |
| 651 | /// This holds an `Expr` rather than a `Lit` because negative numbers |
| 652 | /// are represented as an `Expr::Unary`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 653 | pub Lit(PatLit { |
| 654 | pub expr: Box<Expr>, |
| 655 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 656 | |
| 657 | /// A range pattern: `1..=2`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 658 | pub Range(PatRange { |
| 659 | pub lo: Box<Expr>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 660 | pub limits: RangeLimits, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 661 | pub hi: Box<Expr>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 662 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 663 | |
| 664 | /// A dynamically sized slice pattern: `[a, b, i.., y, z]`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 665 | pub Slice(PatSlice { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 666 | pub bracket_token: token::Bracket, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 667 | pub front: Punctuated<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 668 | pub middle: Option<Box<Pat>>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 669 | pub dot2_token: Option<Token![..]>, |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 670 | pub comma_token: Option<Token![,]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 671 | pub back: Punctuated<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 672 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 673 | |
| 674 | /// A macro in expression position. |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 675 | pub Macro(PatMacro { |
| 676 | pub mac: Macro, |
| 677 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 678 | |
| 679 | /// Tokens in pattern position not interpreted by Syn. |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 680 | pub Verbatim(PatVerbatim #manual_extra_traits { |
| 681 | pub tts: TokenStream, |
| 682 | }), |
| 683 | } |
| 684 | } |
| 685 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 686 | #[cfg(all(feature = "full", feature = "extra-traits"))] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 687 | impl Eq for PatVerbatim {} |
| 688 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 689 | #[cfg(all(feature = "full", feature = "extra-traits"))] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 690 | impl PartialEq for PatVerbatim { |
| 691 | fn eq(&self, other: &Self) -> bool { |
| 692 | TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
| 693 | } |
| 694 | } |
| 695 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 696 | #[cfg(all(feature = "full", feature = "extra-traits"))] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 697 | impl Hash for PatVerbatim { |
| 698 | fn hash<H>(&self, state: &mut H) |
| 699 | where |
| 700 | H: Hasher, |
| 701 | { |
| 702 | TokenStreamHelper(&self.tts).hash(state); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 703 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 706 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 707 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 708 | /// One arm of a `match` expression: `0...10 => { return true; }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 709 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 710 | /// As in: |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 711 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 712 | /// ```rust |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 713 | /// # fn f() -> bool { |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 714 | /// # let n = 0; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 715 | /// match n { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 716 | /// 0...10 => { |
| 717 | /// return true; |
| 718 | /// } |
| 719 | /// // ... |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 720 | /// # _ => {} |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 721 | /// } |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 722 | /// # false |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 723 | /// # } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 724 | /// ``` |
| 725 | pub struct Arm { |
| 726 | pub attrs: Vec<Attribute>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 727 | pub pats: Punctuated<Pat, Token![|]>, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 728 | pub guard: Option<(Token![if], Box<Expr>)>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 729 | pub rocket_token: Token![=>], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 730 | pub body: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 731 | pub comma: Option<Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 732 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 735 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 736 | ast_enum! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 737 | /// Limit types of a range, inclusive or exclusive. |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 738 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 739 | pub enum RangeLimits { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 740 | /// Inclusive at the beginning, exclusive at the end. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 741 | HalfOpen(Token![..]), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 742 | /// Inclusive at the beginning and end. |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 743 | Closed(Token![..=]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 744 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 747 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 748 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 749 | /// A single field in a struct pattern. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 750 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 751 | /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated |
| 752 | /// the same as `x: x, y: ref y, z: ref mut z` but there is no colon token. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 753 | pub struct FieldPat { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 754 | pub attrs: Vec<Attribute>, |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 755 | pub member: Member, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 756 | pub colon_token: Option<Token![:]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 757 | pub pat: Box<Pat>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 758 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 761 | #[cfg(any(feature = "parsing", feature = "printing"))] |
| 762 | #[cfg(feature = "full")] |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 763 | fn arm_expr_requires_comma(expr: &Expr) -> bool { |
| 764 | // see https://github.com/rust-lang/rust/blob/eb8f2586e |
| 765 | // /src/libsyntax/parse/classify.rs#L17-L37 |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 766 | match *expr { |
| 767 | Expr::Unsafe(..) |
| 768 | | Expr::Block(..) |
| 769 | | Expr::If(..) |
| 770 | | Expr::IfLet(..) |
| 771 | | Expr::Match(..) |
| 772 | | Expr::While(..) |
| 773 | | Expr::WhileLet(..) |
| 774 | | Expr::Loop(..) |
| 775 | | Expr::ForLoop(..) |
| 776 | | Expr::Catch(..) => false, |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 777 | _ => true, |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 781 | #[cfg(feature = "parsing")] |
| 782 | pub mod parsing { |
| 783 | use super::*; |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 784 | use path::parsing::qpath; |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 785 | #[cfg(feature = "full")] |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 786 | use path::parsing::ty_no_eq_after; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 787 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 788 | #[cfg(feature = "full")] |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 789 | use proc_macro2::TokenStream; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 790 | use synom::Synom; |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 791 | use buffer::Cursor; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 792 | #[cfg(feature = "full")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 793 | use parse_error; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 794 | use synom::PResult; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 795 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 796 | // When we're parsing expressions which occur before blocks, like in an if |
| 797 | // statement's condition, we cannot parse a struct literal. |
| 798 | // |
| 799 | // Struct literals are ambiguous in certain positions |
| 800 | // https://github.com/rust-lang/rfcs/pull/92 |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 801 | macro_rules! ambiguous_expr { |
| 802 | ($i:expr, $allow_struct:ident) => { |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 803 | ambiguous_expr($i, $allow_struct, true) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 804 | }; |
| 805 | } |
| 806 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 807 | // When we are parsing an optional suffix expression, we cannot allow blocks |
| 808 | // if structs are not allowed. |
| 809 | // |
| 810 | // Example: |
| 811 | // |
| 812 | // if break {} {} |
| 813 | // |
| 814 | // is ambiguous between: |
| 815 | // |
| 816 | // if (break {}) {} |
| 817 | // if (break) {} {} |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 818 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 819 | macro_rules! opt_ambiguous_expr { |
| 820 | ($i:expr, $allow_struct:ident) => { |
| 821 | option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct)) |
| 822 | }; |
| 823 | } |
| 824 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 825 | impl Synom for Expr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 826 | named!(parse -> Self, ambiguous_expr!(true)); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 827 | |
| 828 | fn description() -> Option<&'static str> { |
| 829 | Some("expression") |
| 830 | } |
| 831 | } |
| 832 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 833 | #[cfg(feature = "full")] |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 834 | named!(expr_no_struct -> Expr, ambiguous_expr!(false)); |
| 835 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 836 | // Parse an arbitrary expression. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 837 | #[cfg(feature = "full")] |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 838 | fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 839 | call!(i, assign_expr, allow_struct, allow_block) |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 840 | } |
| 841 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 842 | #[cfg(not(feature = "full"))] |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 843 | fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 844 | // NOTE: We intentionally skip assign_expr, placement_expr, and |
| 845 | // range_expr, as they are not parsed in non-full mode. |
| 846 | call!(i, or_expr, allow_struct, allow_block) |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 847 | } |
| 848 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 849 | // Parse a left-associative binary operator. |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 850 | macro_rules! binop { |
| 851 | ( |
| 852 | $name: ident, |
| 853 | $next: ident, |
| 854 | $submac: ident!( $($args:tt)* ) |
| 855 | ) => { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 856 | named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 857 | mut e: call!($next, allow_struct, allow_block) >> |
| 858 | many0!(do_parse!( |
| 859 | op: $submac!($($args)*) >> |
| 860 | rhs: call!($next, allow_struct, true) >> |
| 861 | ({ |
| 862 | e = ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 863 | attrs: Vec::new(), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 864 | left: Box::new(e.into()), |
| 865 | op: op, |
| 866 | right: Box::new(rhs.into()), |
| 867 | }.into(); |
| 868 | }) |
| 869 | )) >> |
| 870 | (e) |
| 871 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 872 | } |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 873 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 874 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 875 | // <placement> = <placement> .. |
| 876 | // <placement> += <placement> .. |
| 877 | // <placement> -= <placement> .. |
| 878 | // <placement> *= <placement> .. |
| 879 | // <placement> /= <placement> .. |
| 880 | // <placement> %= <placement> .. |
| 881 | // <placement> ^= <placement> .. |
| 882 | // <placement> &= <placement> .. |
| 883 | // <placement> |= <placement> .. |
| 884 | // <placement> <<= <placement> .. |
| 885 | // <placement> >>= <placement> .. |
| 886 | // |
| 887 | // NOTE: This operator is right-associative. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 888 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 889 | named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 890 | mut e: call!(placement_expr, allow_struct, allow_block) >> |
| 891 | alt!( |
| 892 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 893 | eq: punct!(=) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 894 | // Recurse into self to parse right-associative operator. |
| 895 | rhs: call!(assign_expr, allow_struct, true) >> |
| 896 | ({ |
| 897 | e = ExprAssign { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 898 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 899 | left: Box::new(e), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 900 | eq_token: eq, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 901 | right: Box::new(rhs), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 902 | }.into(); |
| 903 | }) |
| 904 | ) |
| 905 | | |
| 906 | do_parse!( |
| 907 | op: call!(BinOp::parse_assign_op) >> |
| 908 | // Recurse into self to parse right-associative operator. |
| 909 | rhs: call!(assign_expr, allow_struct, true) >> |
| 910 | ({ |
| 911 | e = ExprAssignOp { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 912 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 913 | left: Box::new(e), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 914 | op: op, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 915 | right: Box::new(rhs), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 916 | }.into(); |
| 917 | }) |
| 918 | ) |
| 919 | | |
| 920 | epsilon!() |
| 921 | ) >> |
| 922 | (e) |
| 923 | )); |
| 924 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 925 | // <range> <- <range> .. |
| 926 | // |
| 927 | // NOTE: The `in place { expr }` version of this syntax is parsed in |
| 928 | // `atom_expr`, not here. |
| 929 | // |
| 930 | // NOTE: This operator is right-associative. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 931 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 932 | named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 933 | mut e: call!(range_expr, allow_struct, allow_block) >> |
| 934 | alt!( |
| 935 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 936 | arrow: punct!(<-) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 937 | // Recurse into self to parse right-associative operator. |
| 938 | rhs: call!(placement_expr, allow_struct, true) >> |
| 939 | ({ |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 940 | e = ExprInPlace { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 941 | attrs: Vec::new(), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 942 | // op: BinOp::Place(larrow), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 943 | place: Box::new(e), |
David Tolnay | 8701a5c | 2017-12-28 23:31:10 -0500 | [diff] [blame] | 944 | arrow_token: arrow, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 945 | value: Box::new(rhs), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 946 | }.into(); |
| 947 | }) |
| 948 | ) |
| 949 | | |
| 950 | epsilon!() |
| 951 | ) >> |
| 952 | (e) |
| 953 | )); |
| 954 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 955 | // <or> ... <or> .. |
| 956 | // <or> .. <or> .. |
| 957 | // <or> .. |
| 958 | // |
| 959 | // NOTE: This is currently parsed oddly - I'm not sure of what the exact |
| 960 | // rules are for parsing these expressions are, but this is not correct. |
| 961 | // For example, `a .. b .. c` is not a legal expression. It should not |
| 962 | // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently. |
| 963 | // |
| 964 | // NOTE: The form of ranges which don't include a preceding expression are |
| 965 | // parsed by `atom_expr`, rather than by this function. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 966 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 967 | named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 968 | mut e: call!(or_expr, allow_struct, allow_block) >> |
| 969 | many0!(do_parse!( |
| 970 | limits: syn!(RangeLimits) >> |
| 971 | // We don't want to allow blocks here if we don't allow structs. See |
| 972 | // the reasoning for `opt_ambiguous_expr!` above. |
| 973 | hi: option!(call!(or_expr, allow_struct, allow_struct)) >> |
| 974 | ({ |
| 975 | e = ExprRange { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 976 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 977 | from: Some(Box::new(e)), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 978 | limits: limits, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 979 | to: hi.map(|e| Box::new(e)), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 980 | }.into(); |
| 981 | }) |
| 982 | )) >> |
| 983 | (e) |
| 984 | )); |
| 985 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 986 | // <and> || <and> ... |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 987 | binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or)); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 988 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 989 | // <compare> && <compare> ... |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 990 | binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And)); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 991 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 992 | // <bitor> == <bitor> ... |
| 993 | // <bitor> != <bitor> ... |
| 994 | // <bitor> >= <bitor> ... |
| 995 | // <bitor> <= <bitor> ... |
| 996 | // <bitor> > <bitor> ... |
| 997 | // <bitor> < <bitor> ... |
| 998 | // |
| 999 | // NOTE: This operator appears to be parsed as left-associative, but errors |
| 1000 | // if it is used in a non-associative manner. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1001 | binop!( |
| 1002 | compare_expr, |
| 1003 | bitor_expr, |
| 1004 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1005 | punct!(==) => { BinOp::Eq } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1006 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1007 | punct!(!=) => { BinOp::Ne } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1008 | | |
| 1009 | // must be above Lt |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1010 | punct!(<=) => { BinOp::Le } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1011 | | |
| 1012 | // must be above Gt |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1013 | punct!(>=) => { BinOp::Ge } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1014 | | |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 1015 | do_parse!( |
| 1016 | // Make sure that we don't eat the < part of a <- operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1017 | not!(punct!(<-)) >> |
| 1018 | t: punct!(<) >> |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 1019 | (BinOp::Lt(t)) |
| 1020 | ) |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1021 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1022 | punct!(>) => { BinOp::Gt } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1023 | ) |
| 1024 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1025 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1026 | // <bitxor> | <bitxor> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1027 | binop!( |
| 1028 | bitor_expr, |
| 1029 | bitxor_expr, |
| 1030 | do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t))) |
| 1031 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1032 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1033 | // <bitand> ^ <bitand> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1034 | binop!( |
| 1035 | bitxor_expr, |
| 1036 | bitand_expr, |
| 1037 | do_parse!( |
| 1038 | // NOTE: Make sure we aren't looking at ^=. |
| 1039 | not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t)) |
| 1040 | ) |
| 1041 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1042 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1043 | // <shift> & <shift> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1044 | binop!( |
| 1045 | bitand_expr, |
| 1046 | shift_expr, |
| 1047 | do_parse!( |
| 1048 | // NOTE: Make sure we aren't looking at && or &=. |
| 1049 | not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t)) |
| 1050 | ) |
| 1051 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1052 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1053 | // <arith> << <arith> ... |
| 1054 | // <arith> >> <arith> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1055 | binop!( |
| 1056 | shift_expr, |
| 1057 | arith_expr, |
| 1058 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1059 | punct!(<<) => { BinOp::Shl } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1060 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1061 | punct!(>>) => { BinOp::Shr } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1062 | ) |
| 1063 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1064 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1065 | // <term> + <term> ... |
| 1066 | // <term> - <term> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1067 | binop!( |
| 1068 | arith_expr, |
| 1069 | term_expr, |
| 1070 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1071 | punct!(+) => { BinOp::Add } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1072 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1073 | punct!(-) => { BinOp::Sub } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1074 | ) |
| 1075 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1076 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1077 | // <cast> * <cast> ... |
| 1078 | // <cast> / <cast> ... |
| 1079 | // <cast> % <cast> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1080 | binop!( |
| 1081 | term_expr, |
| 1082 | cast_expr, |
| 1083 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1084 | punct!(*) => { BinOp::Mul } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1085 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1086 | punct!(/) => { BinOp::Div } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1087 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1088 | punct!(%) => { BinOp::Rem } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1089 | ) |
| 1090 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1091 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1092 | // <unary> as <ty> |
| 1093 | // <unary> : <ty> |
David Tolnay | 0cf94f2 | 2017-12-28 23:46:26 -0500 | [diff] [blame] | 1094 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1095 | named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1096 | mut e: call!(unary_expr, allow_struct, allow_block) >> |
| 1097 | many0!(alt!( |
| 1098 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1099 | as_: keyword!(as) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1100 | // We can't accept `A + B` in cast expressions, as it's |
| 1101 | // ambiguous with the + expression. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1102 | ty: call!(Type::without_plus) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1103 | ({ |
| 1104 | e = ExprCast { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1105 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1106 | expr: Box::new(e), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1107 | as_token: as_, |
| 1108 | ty: Box::new(ty), |
| 1109 | }.into(); |
| 1110 | }) |
| 1111 | ) |
| 1112 | | |
| 1113 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1114 | colon: punct!(:) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1115 | // We can't accept `A + B` in cast expressions, as it's |
| 1116 | // ambiguous with the + expression. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1117 | ty: call!(Type::without_plus) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1118 | ({ |
| 1119 | e = ExprType { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1120 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1121 | expr: Box::new(e), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1122 | colon_token: colon, |
| 1123 | ty: Box::new(ty), |
| 1124 | }.into(); |
| 1125 | }) |
| 1126 | ) |
| 1127 | )) >> |
| 1128 | (e) |
| 1129 | )); |
| 1130 | |
David Tolnay | 0cf94f2 | 2017-12-28 23:46:26 -0500 | [diff] [blame] | 1131 | // <unary> as <ty> |
| 1132 | #[cfg(not(feature = "full"))] |
| 1133 | named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
| 1134 | mut e: call!(unary_expr, allow_struct, allow_block) >> |
| 1135 | many0!(do_parse!( |
| 1136 | as_: keyword!(as) >> |
| 1137 | // We can't accept `A + B` in cast expressions, as it's |
| 1138 | // ambiguous with the + expression. |
| 1139 | ty: call!(Type::without_plus) >> |
| 1140 | ({ |
| 1141 | e = ExprCast { |
| 1142 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1143 | expr: Box::new(e), |
David Tolnay | 0cf94f2 | 2017-12-28 23:46:26 -0500 | [diff] [blame] | 1144 | as_token: as_, |
| 1145 | ty: Box::new(ty), |
| 1146 | }.into(); |
| 1147 | }) |
| 1148 | )) >> |
| 1149 | (e) |
| 1150 | )); |
| 1151 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1152 | // <UnOp> <trailer> |
| 1153 | // & <trailer> |
| 1154 | // &mut <trailer> |
| 1155 | // box <trailer> |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1156 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1157 | named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!( |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1158 | do_parse!( |
| 1159 | op: syn!(UnOp) >> |
| 1160 | expr: call!(unary_expr, allow_struct, true) >> |
| 1161 | (ExprUnary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1162 | attrs: Vec::new(), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1163 | op: op, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1164 | expr: Box::new(expr), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1165 | }.into()) |
| 1166 | ) |
| 1167 | | |
| 1168 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1169 | and: punct!(&) >> |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 1170 | mutability: option!(keyword!(mut)) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1171 | expr: call!(unary_expr, allow_struct, true) >> |
| 1172 | (ExprAddrOf { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1173 | attrs: Vec::new(), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1174 | and_token: and, |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 1175 | mutability: mutability, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1176 | expr: Box::new(expr), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1177 | }.into()) |
| 1178 | ) |
| 1179 | | |
| 1180 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1181 | box_: keyword!(box) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1182 | expr: call!(unary_expr, allow_struct, true) >> |
| 1183 | (ExprBox { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1184 | attrs: Vec::new(), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1185 | box_token: box_, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1186 | expr: Box::new(expr), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1187 | }.into()) |
| 1188 | ) |
| 1189 | | |
| 1190 | call!(trailer_expr, allow_struct, allow_block) |
| 1191 | )); |
| 1192 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1193 | // XXX: This duplication is ugly |
| 1194 | #[cfg(not(feature = "full"))] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1195 | named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!( |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1196 | do_parse!( |
| 1197 | op: syn!(UnOp) >> |
| 1198 | expr: call!(unary_expr, allow_struct, true) >> |
| 1199 | (ExprUnary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1200 | attrs: Vec::new(), |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1201 | op: op, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1202 | expr: Box::new(expr), |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1203 | }.into()) |
| 1204 | ) |
| 1205 | | |
| 1206 | call!(trailer_expr, allow_struct, allow_block) |
| 1207 | )); |
| 1208 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1209 | // <atom> (..<args>) ... |
| 1210 | // <atom> . <ident> (..<args>) ... |
| 1211 | // <atom> . <ident> ... |
| 1212 | // <atom> . <lit> ... |
| 1213 | // <atom> [ <expr> ] ... |
| 1214 | // <atom> ? ... |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1215 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1216 | named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1217 | mut e: call!(atom_expr, allow_struct, allow_block) >> |
| 1218 | many0!(alt!( |
| 1219 | tap!(args: and_call => { |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1220 | let (paren, args) = args; |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1221 | e = ExprCall { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1222 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1223 | func: Box::new(e), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1224 | args: args, |
| 1225 | paren_token: paren, |
| 1226 | }.into(); |
| 1227 | }) |
| 1228 | | |
| 1229 | tap!(more: and_method_call => { |
| 1230 | let mut call = more; |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1231 | call.receiver = Box::new(e); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1232 | e = call.into(); |
| 1233 | }) |
| 1234 | | |
| 1235 | tap!(field: and_field => { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1236 | let (token, member) = field; |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1237 | e = ExprField { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1238 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1239 | base: Box::new(e), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1240 | dot_token: token, |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1241 | member: member, |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1242 | }.into(); |
| 1243 | }) |
| 1244 | | |
| 1245 | tap!(i: and_index => { |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1246 | let (bracket, i) = i; |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1247 | e = ExprIndex { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1248 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1249 | expr: Box::new(e), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1250 | bracket_token: bracket, |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1251 | index: Box::new(i), |
| 1252 | }.into(); |
| 1253 | }) |
| 1254 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1255 | tap!(question: punct!(?) => { |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1256 | e = ExprTry { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1257 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1258 | expr: Box::new(e), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1259 | question_token: question, |
| 1260 | }.into(); |
| 1261 | }) |
| 1262 | )) >> |
| 1263 | (e) |
| 1264 | )); |
| 1265 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1266 | // XXX: Duplication == ugly |
| 1267 | #[cfg(not(feature = "full"))] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1268 | named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!( |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1269 | mut e: call!(atom_expr, allow_struct, allow_block) >> |
| 1270 | many0!(alt!( |
| 1271 | tap!(args: and_call => { |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1272 | e = ExprCall { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1273 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1274 | func: Box::new(e), |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 1275 | paren_token: args.0, |
| 1276 | args: args.1, |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1277 | }.into(); |
| 1278 | }) |
| 1279 | | |
| 1280 | tap!(i: and_index => { |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1281 | e = ExprIndex { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1282 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1283 | expr: Box::new(e), |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 1284 | bracket_token: i.0, |
| 1285 | index: Box::new(i.1), |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1286 | }.into(); |
| 1287 | }) |
| 1288 | )) >> |
| 1289 | (e) |
| 1290 | )); |
| 1291 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 1292 | // Parse all atomic expressions which don't have to worry about precedence |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1293 | // interactions, as they are fully contained. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1294 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1295 | named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!( |
| 1296 | syn!(ExprGroup) => { Expr::Group } // must be placed first |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 1297 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1298 | syn!(ExprLit) => { Expr::Lit } // must be before expr_struct |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1299 | | |
| 1300 | // must be before expr_path |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 1301 | cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1302 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1303 | syn!(ExprParen) => { Expr::Paren } // must be before expr_tup |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1304 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1305 | syn!(ExprMacro) => { Expr::Macro } // must be before expr_path |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1306 | | |
| 1307 | call!(expr_break, allow_struct) // must be before expr_path |
| 1308 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1309 | syn!(ExprContinue) => { Expr::Continue } // must be before expr_path |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1310 | | |
| 1311 | call!(expr_ret, allow_struct) // must be before expr_path |
| 1312 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1313 | syn!(ExprArray) => { Expr::Array } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1314 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1315 | syn!(ExprTuple) => { Expr::Tuple } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1316 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1317 | syn!(ExprIf) => { Expr::If } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1318 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1319 | syn!(ExprIfLet) => { Expr::IfLet } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1320 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1321 | syn!(ExprWhile) => { Expr::While } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1322 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1323 | syn!(ExprWhileLet) => { Expr::WhileLet } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1324 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1325 | syn!(ExprForLoop) => { Expr::ForLoop } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1326 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1327 | syn!(ExprLoop) => { Expr::Loop } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1328 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1329 | syn!(ExprMatch) => { Expr::Match } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1330 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1331 | syn!(ExprCatch) => { Expr::Catch } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1332 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1333 | syn!(ExprYield) => { Expr::Yield } |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1334 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1335 | syn!(ExprUnsafe) => { Expr::Unsafe } |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1336 | | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1337 | call!(expr_closure, allow_struct) |
| 1338 | | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 1339 | cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1340 | | |
| 1341 | // NOTE: This is the prefix-form of range |
| 1342 | call!(expr_range, allow_struct) |
| 1343 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1344 | syn!(ExprPath) => { Expr::Path } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1345 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1346 | syn!(ExprRepeat) => { Expr::Repeat } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1347 | )); |
| 1348 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1349 | #[cfg(not(feature = "full"))] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1350 | named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!( |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 1351 | syn!(ExprLit) => { Expr::Lit } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1352 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1353 | syn!(ExprPath) => { Expr::Path } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1354 | )); |
| 1355 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1356 | #[cfg(feature = "full")] |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1357 | named!(expr_nosemi -> Expr, map!(alt!( |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1358 | syn!(ExprIf) => { Expr::If } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1359 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1360 | syn!(ExprIfLet) => { Expr::IfLet } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1361 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1362 | syn!(ExprWhile) => { Expr::While } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1363 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1364 | syn!(ExprWhileLet) => { Expr::WhileLet } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1365 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1366 | syn!(ExprForLoop) => { Expr::ForLoop } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1367 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1368 | syn!(ExprLoop) => { Expr::Loop } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1369 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1370 | syn!(ExprMatch) => { Expr::Match } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1371 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1372 | syn!(ExprCatch) => { Expr::Catch } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1373 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1374 | syn!(ExprYield) => { Expr::Yield } |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1375 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1376 | syn!(ExprUnsafe) => { Expr::Unsafe } |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1377 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1378 | syn!(ExprBlock) => { Expr::Block } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1379 | ), Expr::from)); |
| 1380 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1381 | impl Synom for ExprLit { |
| 1382 | named!(parse -> Self, do_parse!( |
| 1383 | lit: syn!(Lit) >> |
| 1384 | (ExprLit { |
| 1385 | attrs: Vec::new(), |
| 1386 | lit: lit, |
| 1387 | }) |
| 1388 | )); |
| 1389 | } |
| 1390 | |
| 1391 | #[cfg(feature = "full")] |
| 1392 | impl Synom for ExprMacro { |
| 1393 | named!(parse -> Self, do_parse!( |
| 1394 | mac: syn!(Macro) >> |
| 1395 | (ExprMacro { |
| 1396 | attrs: Vec::new(), |
| 1397 | mac: mac, |
| 1398 | }) |
| 1399 | )); |
| 1400 | } |
| 1401 | |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 1402 | #[cfg(feature = "full")] |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 1403 | impl Synom for ExprGroup { |
| 1404 | named!(parse -> Self, do_parse!( |
| 1405 | e: grouped!(syn!(Expr)) >> |
| 1406 | (ExprGroup { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1407 | attrs: Vec::new(), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1408 | expr: Box::new(e.1), |
| 1409 | group_token: e.0, |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1410 | }) |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 1411 | )); |
| 1412 | } |
| 1413 | |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 1414 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1415 | impl Synom for ExprParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1416 | named!(parse -> Self, do_parse!( |
| 1417 | e: parens!(syn!(Expr)) >> |
| 1418 | (ExprParen { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1419 | attrs: Vec::new(), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1420 | paren_token: e.0, |
| 1421 | expr: Box::new(e.1), |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1422 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1423 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1424 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1425 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1426 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1427 | impl Synom for ExprArray { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1428 | named!(parse -> Self, do_parse!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1429 | elems: brackets!(Punctuated::parse_terminated) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1430 | (ExprArray { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1431 | attrs: Vec::new(), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1432 | bracket_token: elems.0, |
| 1433 | elems: elems.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1434 | }) |
| 1435 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1436 | |
| 1437 | fn description() -> Option<&'static str> { |
| 1438 | Some("array") |
| 1439 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1440 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1441 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1442 | named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>), |
| 1443 | parens!(Punctuated::parse_terminated)); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1444 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1445 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1446 | named!(and_method_call -> ExprMethodCall, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1447 | dot: punct!(.) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1448 | method: syn!(Ident) >> |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 1449 | turbofish: option!(tuple!( |
| 1450 | punct!(::), |
| 1451 | punct!(<), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1452 | call!(Punctuated::parse_terminated), |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 1453 | punct!(>) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1454 | )) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1455 | args: parens!(Punctuated::parse_terminated) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1456 | ({ |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1457 | ExprMethodCall { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1458 | attrs: Vec::new(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1459 | // this expr will get overwritten after being returned |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 1460 | receiver: Box::new(Expr::Verbatim(ExprVerbatim { |
| 1461 | tts: TokenStream::empty(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1462 | })), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1463 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1464 | method: method, |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 1465 | turbofish: turbofish.map(|fish| MethodTurbofish { |
| 1466 | colon2_token: fish.0, |
| 1467 | lt_token: fish.1, |
| 1468 | args: fish.2, |
| 1469 | gt_token: fish.3, |
| 1470 | }), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1471 | args: args.1, |
| 1472 | paren_token: args.0, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1473 | dot_token: dot, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1474 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1475 | }) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1476 | )); |
| 1477 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1478 | #[cfg(feature = "full")] |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 1479 | impl Synom for GenericMethodArgument { |
| 1480 | // TODO parse const generics as well |
| 1481 | named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type)); |
| 1482 | } |
| 1483 | |
| 1484 | #[cfg(feature = "full")] |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 1485 | impl Synom for ExprTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1486 | named!(parse -> Self, do_parse!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1487 | elems: parens!(Punctuated::parse_terminated) >> |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 1488 | (ExprTuple { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1489 | attrs: Vec::new(), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1490 | elems: elems.1, |
| 1491 | paren_token: elems.0, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1492 | }) |
| 1493 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1494 | |
| 1495 | fn description() -> Option<&'static str> { |
| 1496 | Some("tuple") |
| 1497 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1498 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1499 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1500 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1501 | impl Synom for ExprIfLet { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1502 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1503 | if_: keyword!(if) >> |
| 1504 | let_: keyword!(let) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1505 | pat: syn!(Pat) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1506 | eq: punct!(=) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1507 | cond: expr_no_struct >> |
David Tolnay | e64213b | 2017-12-30 00:24:20 -0500 | [diff] [blame] | 1508 | then_block: braces!(Block::parse_within) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1509 | else_block: option!(else_block) >> |
| 1510 | (ExprIfLet { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1511 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1512 | pat: Box::new(pat), |
| 1513 | let_token: let_, |
| 1514 | eq_token: eq, |
| 1515 | expr: Box::new(cond), |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 1516 | then_branch: Block { |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1517 | brace_token: then_block.0, |
| 1518 | stmts: then_block.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1519 | }, |
| 1520 | if_token: if_, |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 1521 | else_branch: else_block, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1522 | }) |
| 1523 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1524 | |
| 1525 | fn description() -> Option<&'static str> { |
| 1526 | Some("`if let` expression") |
| 1527 | } |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1530 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1531 | impl Synom for ExprIf { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1532 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1533 | if_: keyword!(if) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1534 | cond: expr_no_struct >> |
David Tolnay | e64213b | 2017-12-30 00:24:20 -0500 | [diff] [blame] | 1535 | then_block: braces!(Block::parse_within) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1536 | else_block: option!(else_block) >> |
| 1537 | (ExprIf { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1538 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1539 | cond: Box::new(cond), |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 1540 | then_branch: Block { |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1541 | brace_token: then_block.0, |
| 1542 | stmts: then_block.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1543 | }, |
| 1544 | if_token: if_, |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 1545 | else_branch: else_block, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1546 | }) |
| 1547 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1548 | |
| 1549 | fn description() -> Option<&'static str> { |
| 1550 | Some("`if` expression") |
| 1551 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1552 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1553 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1554 | #[cfg(feature = "full")] |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 1555 | named!(else_block -> (Token![else], Box<Expr>), do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1556 | else_: keyword!(else) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1557 | expr: alt!( |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1558 | syn!(ExprIf) => { Expr::If } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1559 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1560 | syn!(ExprIfLet) => { Expr::IfLet } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1561 | | |
| 1562 | do_parse!( |
David Tolnay | e64213b | 2017-12-30 00:24:20 -0500 | [diff] [blame] | 1563 | else_block: braces!(Block::parse_within) >> |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1564 | (Expr::Block(ExprBlock { |
| 1565 | attrs: Vec::new(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1566 | block: Block { |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1567 | brace_token: else_block.0, |
| 1568 | stmts: else_block.1, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1569 | }, |
| 1570 | })) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1571 | ) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1572 | ) >> |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 1573 | (else_, Box::new(expr)) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1574 | )); |
| 1575 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1576 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1577 | impl Synom for ExprForLoop { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1578 | named!(parse -> Self, do_parse!( |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1579 | label: option!(syn!(Label)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1580 | for_: keyword!(for) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1581 | pat: syn!(Pat) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1582 | in_: keyword!(in) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1583 | expr: expr_no_struct >> |
| 1584 | loop_block: syn!(Block) >> |
| 1585 | (ExprForLoop { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1586 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1587 | for_token: for_, |
| 1588 | in_token: in_, |
| 1589 | pat: Box::new(pat), |
| 1590 | expr: Box::new(expr), |
| 1591 | body: loop_block, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1592 | label: label, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1593 | }) |
| 1594 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1595 | |
| 1596 | fn description() -> Option<&'static str> { |
| 1597 | Some("`for` loop") |
| 1598 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1599 | } |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 1600 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1601 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1602 | impl Synom for ExprLoop { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1603 | named!(parse -> Self, do_parse!( |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1604 | label: option!(syn!(Label)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1605 | loop_: keyword!(loop) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1606 | loop_block: syn!(Block) >> |
| 1607 | (ExprLoop { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1608 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1609 | loop_token: loop_, |
| 1610 | body: loop_block, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1611 | label: label, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1612 | }) |
| 1613 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1614 | |
| 1615 | fn description() -> Option<&'static str> { |
| 1616 | Some("`loop`") |
| 1617 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1620 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1621 | impl Synom for ExprMatch { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1622 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1623 | match_: keyword!(match) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1624 | obj: expr_no_struct >> |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1625 | res: braces!(many0!(Arm::parse)) >> |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1626 | (ExprMatch { |
| 1627 | attrs: Vec::new(), |
| 1628 | expr: Box::new(obj), |
| 1629 | match_token: match_, |
| 1630 | brace_token: res.0, |
| 1631 | arms: res.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1632 | }) |
| 1633 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1634 | |
| 1635 | fn description() -> Option<&'static str> { |
| 1636 | Some("`match` expression") |
| 1637 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1638 | } |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1639 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1640 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1641 | impl Synom for ExprCatch { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1642 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1643 | do_: keyword!(do) >> |
| 1644 | catch_: keyword!(catch) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1645 | catch_block: syn!(Block) >> |
| 1646 | (ExprCatch { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1647 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1648 | block: catch_block, |
| 1649 | do_token: do_, |
| 1650 | catch_token: catch_, |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1651 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1652 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1653 | |
| 1654 | fn description() -> Option<&'static str> { |
| 1655 | Some("`catch` expression") |
| 1656 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1657 | } |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 1658 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1659 | #[cfg(feature = "full")] |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1660 | impl Synom for ExprYield { |
| 1661 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1662 | yield_: keyword!(yield) >> |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1663 | expr: option!(syn!(Expr)) >> |
| 1664 | (ExprYield { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1665 | attrs: Vec::new(), |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1666 | yield_token: yield_, |
| 1667 | expr: expr.map(Box::new), |
| 1668 | }) |
| 1669 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1670 | |
| 1671 | fn description() -> Option<&'static str> { |
| 1672 | Some("`yield` expression") |
| 1673 | } |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1677 | impl Synom for Arm { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1678 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1679 | attrs: many0!(Attribute::parse_outer) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1680 | pats: call!(Punctuated::parse_separated_nonempty) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1681 | guard: option!(tuple!(keyword!(if), syn!(Expr))) >> |
| 1682 | rocket: punct!(=>) >> |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1683 | body: do_parse!( |
| 1684 | expr: alt!(expr_nosemi | syn!(Expr)) >> |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 1685 | comma: switch!(value!(arm_expr_requires_comma(&expr)), |
| 1686 | true => alt!( |
| 1687 | input_end!() => { |_| None } |
| 1688 | | |
| 1689 | punct!(,) => { Some } |
| 1690 | ) |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1691 | | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 1692 | false => option!(punct!(,)) |
| 1693 | ) >> |
| 1694 | (expr, comma) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1695 | ) >> |
| 1696 | (Arm { |
| 1697 | rocket_token: rocket, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1698 | attrs: attrs, |
| 1699 | pats: pats, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 1700 | guard: guard.map(|(if_, guard)| (if_, Box::new(guard))), |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1701 | body: Box::new(body.0), |
| 1702 | comma: body.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1703 | }) |
| 1704 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1705 | |
| 1706 | fn description() -> Option<&'static str> { |
| 1707 | Some("`match` arm") |
| 1708 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1709 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1710 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1711 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1712 | named!(expr_closure(allow_struct: bool) -> Expr, do_parse!( |
David Tolnay | efc96fb | 2017-12-29 02:03:15 -0500 | [diff] [blame] | 1713 | capture: option!(keyword!(move)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1714 | or1: punct!(|) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1715 | inputs: call!(Punctuated::parse_terminated_with, fn_arg) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1716 | or2: punct!(|) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1717 | ret_and_body: alt!( |
| 1718 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1719 | arrow: punct!(->) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1720 | ty: syn!(Type) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1721 | body: syn!(Block) >> |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 1722 | (ReturnType::Type(arrow, Box::new(ty)), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1723 | Expr::Block(ExprBlock { |
| 1724 | attrs: Vec::new(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1725 | block: body, |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1726 | })) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1727 | ) |
| 1728 | | |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 1729 | map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e)) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1730 | ) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1731 | (ExprClosure { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1732 | attrs: Vec::new(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1733 | capture: capture, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1734 | or1_token: or1, |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame] | 1735 | inputs: inputs, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1736 | or2_token: or2, |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame] | 1737 | output: ret_and_body.0, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1738 | body: Box::new(ret_and_body.1), |
| 1739 | }.into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1740 | )); |
| 1741 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1742 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1743 | named!(fn_arg -> FnArg, do_parse!( |
| 1744 | pat: syn!(Pat) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1745 | ty: option!(tuple!(punct!(:), syn!(Type))) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1746 | ({ |
David Tolnay | 80ed55f | 2017-12-27 22:54:40 -0500 | [diff] [blame] | 1747 | if let Some((colon, ty)) = ty { |
| 1748 | FnArg::Captured(ArgCaptured { |
| 1749 | pat: pat, |
| 1750 | colon_token: colon, |
| 1751 | ty: ty, |
| 1752 | }) |
| 1753 | } else { |
| 1754 | FnArg::Inferred(pat) |
| 1755 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1756 | }) |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 1757 | )); |
| 1758 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1759 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1760 | impl Synom for ExprWhile { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1761 | named!(parse -> Self, do_parse!( |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1762 | label: option!(syn!(Label)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1763 | while_: keyword!(while) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1764 | cond: expr_no_struct >> |
| 1765 | while_block: syn!(Block) >> |
| 1766 | (ExprWhile { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1767 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1768 | while_token: while_, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1769 | cond: Box::new(cond), |
| 1770 | body: while_block, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1771 | label: label, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1772 | }) |
| 1773 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1774 | |
| 1775 | fn description() -> Option<&'static str> { |
| 1776 | Some("`while` expression") |
| 1777 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1780 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1781 | impl Synom for ExprWhileLet { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1782 | named!(parse -> Self, do_parse!( |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1783 | label: option!(syn!(Label)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1784 | while_: keyword!(while) >> |
| 1785 | let_: keyword!(let) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1786 | pat: syn!(Pat) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1787 | eq: punct!(=) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1788 | value: expr_no_struct >> |
| 1789 | while_block: syn!(Block) >> |
| 1790 | (ExprWhileLet { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1791 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1792 | eq_token: eq, |
| 1793 | let_token: let_, |
| 1794 | while_token: while_, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1795 | pat: Box::new(pat), |
| 1796 | expr: Box::new(value), |
| 1797 | body: while_block, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1798 | label: label, |
| 1799 | }) |
| 1800 | )); |
| 1801 | } |
| 1802 | |
| 1803 | #[cfg(feature = "full")] |
| 1804 | impl Synom for Label { |
| 1805 | named!(parse -> Self, do_parse!( |
| 1806 | name: syn!(Lifetime) >> |
| 1807 | colon: punct!(:) >> |
| 1808 | (Label { |
| 1809 | name: name, |
| 1810 | colon_token: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1811 | }) |
| 1812 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1813 | |
| 1814 | fn description() -> Option<&'static str> { |
| 1815 | Some("`while let` expression") |
| 1816 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1819 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1820 | impl Synom for ExprContinue { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1821 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1822 | cont: keyword!(continue) >> |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1823 | label: option!(syn!(Lifetime)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1824 | (ExprContinue { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1825 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1826 | continue_token: cont, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1827 | label: label, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1828 | }) |
| 1829 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1830 | |
| 1831 | fn description() -> Option<&'static str> { |
| 1832 | Some("`continue`") |
| 1833 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1834 | } |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1835 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1836 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1837 | named!(expr_break(allow_struct: bool) -> Expr, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1838 | break_: keyword!(break) >> |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1839 | label: option!(syn!(Lifetime)) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1840 | // We can't allow blocks after a `break` expression when we wouldn't |
| 1841 | // allow structs, as this expression is ambiguous. |
| 1842 | val: opt_ambiguous_expr!(allow_struct) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1843 | (ExprBreak { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1844 | attrs: Vec::new(), |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 1845 | label: label, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1846 | expr: val.map(Box::new), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1847 | break_token: break_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1848 | }.into()) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1849 | )); |
| 1850 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1851 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1852 | named!(expr_ret(allow_struct: bool) -> Expr, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1853 | return_: keyword!(return) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1854 | // NOTE: return is greedy and eats blocks after it even when in a |
| 1855 | // position where structs are not allowed, such as in if statement |
| 1856 | // conditions. For example: |
| 1857 | // |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1858 | // if return { println!("A") } {} // Prints "A" |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1859 | ret_value: option!(ambiguous_expr!(allow_struct)) >> |
David Tolnay | c246cd3 | 2017-12-28 23:14:32 -0500 | [diff] [blame] | 1860 | (ExprReturn { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1861 | attrs: Vec::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1862 | expr: ret_value.map(Box::new), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1863 | return_token: return_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1864 | }.into()) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1865 | )); |
| 1866 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1867 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1868 | impl Synom for ExprStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1869 | named!(parse -> Self, do_parse!( |
| 1870 | path: syn!(Path) >> |
| 1871 | data: braces!(do_parse!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1872 | fields: call!(Punctuated::parse_terminated) >> |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 1873 | base: option!(cond!(fields.empty_or_trailing(), do_parse!( |
| 1874 | dots: punct!(..) >> |
| 1875 | base: syn!(Expr) >> |
| 1876 | (dots, base) |
| 1877 | ))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1878 | (fields, base) |
| 1879 | )) >> |
| 1880 | ({ |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1881 | let (brace, (fields, base)) = data; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1882 | let (dots, rest) = match base.and_then(|b| b) { |
| 1883 | Some((dots, base)) => (Some(dots), Some(base)), |
| 1884 | None => (None, None), |
| 1885 | }; |
| 1886 | ExprStruct { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1887 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1888 | brace_token: brace, |
| 1889 | path: path, |
| 1890 | fields: fields, |
| 1891 | dot2_token: dots, |
| 1892 | rest: rest.map(Box::new), |
| 1893 | } |
| 1894 | }) |
| 1895 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1896 | |
| 1897 | fn description() -> Option<&'static str> { |
| 1898 | Some("struct literal expression") |
| 1899 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1902 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1903 | impl Synom for FieldValue { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1904 | named!(parse -> Self, alt!( |
| 1905 | do_parse!( |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1906 | member: syn!(Member) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1907 | colon: punct!(:) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1908 | value: syn!(Expr) >> |
| 1909 | (FieldValue { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1910 | member: member, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1911 | expr: value, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1912 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1913 | colon_token: Some(colon), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1914 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1915 | ) |
| 1916 | | |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 1917 | map!(syn!(Ident), |name| FieldValue { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1918 | member: Member::Named(name), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1919 | expr: Expr::Path(ExprPath { |
| 1920 | attrs: Vec::new(), |
| 1921 | qself: None, |
| 1922 | path: name.into(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1923 | }), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1924 | attrs: Vec::new(), |
| 1925 | colon_token: None, |
| 1926 | }) |
| 1927 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1928 | |
| 1929 | fn description() -> Option<&'static str> { |
| 1930 | Some("field-value pair: `field: value`") |
| 1931 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1932 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1933 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1934 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1935 | impl Synom for ExprRepeat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1936 | named!(parse -> Self, do_parse!( |
| 1937 | data: brackets!(do_parse!( |
| 1938 | value: syn!(Expr) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1939 | semi: punct!(;) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1940 | times: syn!(Expr) >> |
| 1941 | (value, semi, times) |
| 1942 | )) >> |
| 1943 | (ExprRepeat { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1944 | attrs: Vec::new(), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1945 | expr: Box::new((data.1).0), |
David Tolnay | 84d8044 | 2018-01-07 01:03:20 -0800 | [diff] [blame] | 1946 | len: Box::new((data.1).2), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 1947 | bracket_token: data.0, |
| 1948 | semi_token: (data.1).1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1949 | }) |
| 1950 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1951 | |
| 1952 | fn description() -> Option<&'static str> { |
| 1953 | Some("repeated array literal: `[val; N]`") |
| 1954 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1955 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1956 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1957 | #[cfg(feature = "full")] |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1958 | impl Synom for ExprUnsafe { |
| 1959 | named!(parse -> Self, do_parse!( |
| 1960 | unsafe_: keyword!(unsafe) >> |
| 1961 | b: syn!(Block) >> |
| 1962 | (ExprUnsafe { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1963 | attrs: Vec::new(), |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1964 | unsafe_token: unsafe_, |
| 1965 | block: b, |
| 1966 | }) |
| 1967 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1968 | |
| 1969 | fn description() -> Option<&'static str> { |
| 1970 | Some("unsafe block: `unsafe { .. }`") |
| 1971 | } |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1975 | impl Synom for ExprBlock { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1976 | named!(parse -> Self, do_parse!( |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1977 | b: syn!(Block) >> |
| 1978 | (ExprBlock { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1979 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1980 | block: b, |
| 1981 | }) |
| 1982 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 1983 | |
| 1984 | fn description() -> Option<&'static str> { |
| 1985 | Some("block: `{ .. }`") |
| 1986 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1987 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1988 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1989 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1990 | named!(expr_range(allow_struct: bool) -> Expr, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1991 | limits: syn!(RangeLimits) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1992 | hi: opt_ambiguous_expr!(allow_struct) >> |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1993 | (ExprRange { |
| 1994 | attrs: Vec::new(), |
| 1995 | from: None, |
| 1996 | to: hi.map(Box::new), |
| 1997 | limits: limits, |
| 1998 | }.into()) |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1999 | )); |
| 2000 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2001 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2002 | impl Synom for RangeLimits { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2003 | named!(parse -> Self, alt!( |
| 2004 | // Must come before Dot2 |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 2005 | punct!(..=) => { RangeLimits::Closed } |
| 2006 | | |
| 2007 | // Must come before Dot2 |
David Tolnay | 995bff2 | 2017-12-17 23:44:43 -0800 | [diff] [blame] | 2008 | punct!(...) => { |dot3| RangeLimits::Closed(Token) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2009 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2010 | punct!(..) => { RangeLimits::HalfOpen } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2011 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2012 | |
| 2013 | fn description() -> Option<&'static str> { |
| 2014 | Some("range limit: `..`, `...` or `..=`") |
| 2015 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2016 | } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 2017 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2018 | impl Synom for ExprPath { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2019 | named!(parse -> Self, do_parse!( |
| 2020 | pair: qpath >> |
| 2021 | (ExprPath { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2022 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2023 | qself: pair.0, |
| 2024 | path: pair.1, |
| 2025 | }) |
| 2026 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2027 | |
| 2028 | fn description() -> Option<&'static str> { |
| 2029 | Some("path: `a::b::c`") |
| 2030 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2031 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2032 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2033 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2034 | named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 2035 | |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 2036 | named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 2037 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2038 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2039 | impl Synom for Block { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2040 | named!(parse -> Self, do_parse!( |
David Tolnay | e64213b | 2017-12-30 00:24:20 -0500 | [diff] [blame] | 2041 | stmts: braces!(Block::parse_within) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2042 | (Block { |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 2043 | brace_token: stmts.0, |
| 2044 | stmts: stmts.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2045 | }) |
| 2046 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2047 | |
| 2048 | fn description() -> Option<&'static str> { |
| 2049 | Some("block: `{ .. }`") |
| 2050 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2051 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 2052 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2053 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2054 | impl Block { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2055 | named!(pub parse_within -> Vec<Stmt>, do_parse!( |
David Tolnay | 4699a31 | 2017-12-27 14:39:22 -0500 | [diff] [blame] | 2056 | many0!(punct!(;)) >> |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 2057 | mut standalone: many0!(do_parse!( |
| 2058 | stmt: syn!(Stmt) >> |
| 2059 | many0!(punct!(;)) >> |
| 2060 | (stmt) |
| 2061 | )) >> |
Alex Crichton | 70bbd59 | 2017-08-27 10:40:03 -0700 | [diff] [blame] | 2062 | last: option!(do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 2063 | attrs: many0!(Attribute::parse_outer) >> |
Alex Crichton | 70bbd59 | 2017-08-27 10:40:03 -0700 | [diff] [blame] | 2064 | mut e: syn!(Expr) >> |
| 2065 | ({ |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 2066 | e.replace_attrs(attrs); |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2067 | Stmt::Expr(e) |
Alex Crichton | 70bbd59 | 2017-08-27 10:40:03 -0700 | [diff] [blame] | 2068 | }) |
| 2069 | )) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2070 | (match last { |
| 2071 | None => standalone, |
| 2072 | Some(last) => { |
Alex Crichton | 70bbd59 | 2017-08-27 10:40:03 -0700 | [diff] [blame] | 2073 | standalone.push(last); |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2074 | standalone |
| 2075 | } |
| 2076 | }) |
| 2077 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2080 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2081 | impl Synom for Stmt { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2082 | named!(parse -> Self, alt!( |
| 2083 | stmt_mac |
| 2084 | | |
| 2085 | stmt_local |
| 2086 | | |
| 2087 | stmt_item |
| 2088 | | |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 2089 | stmt_blockexpr |
| 2090 | | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2091 | stmt_expr |
| 2092 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2093 | |
| 2094 | fn description() -> Option<&'static str> { |
| 2095 | Some("statement") |
| 2096 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2097 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 2098 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2099 | #[cfg(feature = "full")] |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2100 | named!(stmt_mac -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 2101 | attrs: many0!(Attribute::parse_outer) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2102 | what: syn!(Path) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2103 | bang: punct!(!) >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 2104 | // Only parse braces here; paren and bracket will get parsed as |
| 2105 | // expression statements |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2106 | data: braces!(syn!(TokenStream)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2107 | semi: option!(punct!(;)) >> |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2108 | (Stmt::Item(Item::Macro(ItemMacro { |
David Tolnay | 57b52bc | 2017-12-28 18:06:38 -0500 | [diff] [blame] | 2109 | attrs: attrs, |
| 2110 | ident: None, |
| 2111 | mac: Macro { |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 2112 | path: what, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2113 | bang_token: bang, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 2114 | delimiter: MacroDelimiter::Brace(data.0), |
| 2115 | tts: data.1, |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 2116 | }, |
David Tolnay | 57b52bc | 2017-12-28 18:06:38 -0500 | [diff] [blame] | 2117 | semi_token: semi, |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2118 | }))) |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2119 | )); |
| 2120 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2121 | #[cfg(feature = "full")] |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2122 | named!(stmt_local -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 2123 | attrs: many0!(Attribute::parse_outer) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2124 | let_: keyword!(let) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2125 | pat: syn!(Pat) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 2126 | ty: option!(tuple!(punct!(:), syn!(Type))) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2127 | init: option!(tuple!(punct!(=), syn!(Expr))) >> |
| 2128 | semi: punct!(;) >> |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2129 | (Stmt::Local(Local { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2130 | attrs: attrs, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 2131 | let_token: let_, |
| 2132 | pat: Box::new(pat), |
| 2133 | ty: ty.map(|(colon, ty)| (colon, Box::new(ty))), |
| 2134 | init: init.map(|(eq, expr)| (eq, Box::new(expr))), |
| 2135 | semi_token: semi, |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2136 | })) |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2137 | )); |
| 2138 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2139 | #[cfg(feature = "full")] |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2140 | named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i))); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2141 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2142 | #[cfg(feature = "full")] |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 2143 | named!(stmt_blockexpr -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 2144 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 2145 | mut e: expr_nosemi >> |
| 2146 | // If the next token is a `.` or a `?` it is special-cased to parse as |
| 2147 | // an expression instead of a blockexpression. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2148 | not!(punct!(.)) >> |
| 2149 | not!(punct!(?)) >> |
| 2150 | semi: option!(punct!(;)) >> |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 2151 | ({ |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 2152 | e.replace_attrs(attrs); |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 2153 | if let Some(semi) = semi { |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2154 | Stmt::Semi(e, semi) |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 2155 | } else { |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2156 | Stmt::Expr(e) |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 2157 | } |
| 2158 | }) |
| 2159 | )); |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 2160 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2161 | #[cfg(feature = "full")] |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 2162 | named!(stmt_expr -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 2163 | attrs: many0!(Attribute::parse_outer) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2164 | mut e: syn!(Expr) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2165 | semi: punct!(;) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 2166 | ({ |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 2167 | e.replace_attrs(attrs); |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 2168 | Stmt::Semi(e, semi) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 2169 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 2170 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 2171 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2172 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2173 | impl Synom for Pat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2174 | named!(parse -> Self, alt!( |
| 2175 | syn!(PatWild) => { Pat::Wild } // must be before pat_ident |
| 2176 | | |
| 2177 | syn!(PatBox) => { Pat::Box } // must be before pat_ident |
| 2178 | | |
| 2179 | syn!(PatRange) => { Pat::Range } // must be before pat_lit |
| 2180 | | |
| 2181 | syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident |
| 2182 | | |
| 2183 | syn!(PatStruct) => { Pat::Struct } // must be before pat_ident |
| 2184 | | |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 2185 | syn!(PatMacro) => { Pat::Macro } // must be before pat_ident |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2186 | | |
| 2187 | syn!(PatLit) => { Pat::Lit } // must be before pat_ident |
| 2188 | | |
| 2189 | syn!(PatIdent) => { Pat::Ident } // must be before pat_path |
| 2190 | | |
| 2191 | syn!(PatPath) => { Pat::Path } |
| 2192 | | |
| 2193 | syn!(PatTuple) => { Pat::Tuple } |
| 2194 | | |
| 2195 | syn!(PatRef) => { Pat::Ref } |
| 2196 | | |
| 2197 | syn!(PatSlice) => { Pat::Slice } |
| 2198 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2199 | |
| 2200 | fn description() -> Option<&'static str> { |
| 2201 | Some("pattern") |
| 2202 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2203 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2204 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2205 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2206 | impl Synom for PatWild { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2207 | named!(parse -> Self, map!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2208 | punct!(_), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2209 | |u| PatWild { underscore_token: u } |
| 2210 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2211 | |
| 2212 | fn description() -> Option<&'static str> { |
| 2213 | Some("wild pattern: `_`") |
| 2214 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2215 | } |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 2216 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2217 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2218 | impl Synom for PatBox { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2219 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2220 | boxed: keyword!(box) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2221 | pat: syn!(Pat) >> |
| 2222 | (PatBox { |
| 2223 | pat: Box::new(pat), |
| 2224 | box_token: boxed, |
| 2225 | }) |
| 2226 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2227 | |
| 2228 | fn description() -> Option<&'static str> { |
| 2229 | Some("box pattern") |
| 2230 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2233 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2234 | impl Synom for PatIdent { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2235 | named!(parse -> Self, do_parse!( |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 2236 | by_ref: option!(keyword!(ref)) >> |
| 2237 | mutability: option!(keyword!(mut)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2238 | name: alt!( |
| 2239 | syn!(Ident) |
| 2240 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2241 | keyword!(self) => { Into::into } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2242 | ) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2243 | not!(punct!(<)) >> |
| 2244 | not!(punct!(::)) >> |
| 2245 | subpat: option!(tuple!(punct!(@), syn!(Pat))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2246 | (PatIdent { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 2247 | by_ref: by_ref, |
David Tolnay | efc96fb | 2017-12-29 02:03:15 -0500 | [diff] [blame] | 2248 | mutability: mutability, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2249 | ident: name, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 2250 | subpat: subpat.map(|(at, pat)| (at, Box::new(pat))), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2251 | }) |
| 2252 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2253 | |
| 2254 | fn description() -> Option<&'static str> { |
| 2255 | Some("pattern identifier binding") |
| 2256 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2259 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2260 | impl Synom for PatTupleStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2261 | named!(parse -> Self, do_parse!( |
| 2262 | path: syn!(Path) >> |
| 2263 | tuple: syn!(PatTuple) >> |
| 2264 | (PatTupleStruct { |
| 2265 | path: path, |
| 2266 | pat: tuple, |
| 2267 | }) |
| 2268 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2269 | |
| 2270 | fn description() -> Option<&'static str> { |
| 2271 | Some("tuple struct pattern") |
| 2272 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2273 | } |
| 2274 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2275 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2276 | impl Synom for PatStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2277 | named!(parse -> Self, do_parse!( |
| 2278 | path: syn!(Path) >> |
| 2279 | data: braces!(do_parse!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 2280 | fields: call!(Punctuated::parse_terminated) >> |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 2281 | base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2282 | (fields, base) |
| 2283 | )) >> |
| 2284 | (PatStruct { |
| 2285 | path: path, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 2286 | fields: (data.1).0, |
| 2287 | brace_token: data.0, |
| 2288 | dot2_token: (data.1).1.and_then(|m| m), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2289 | }) |
| 2290 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2291 | |
| 2292 | fn description() -> Option<&'static str> { |
| 2293 | Some("struct pattern") |
| 2294 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2297 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2298 | impl Synom for FieldPat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2299 | named!(parse -> Self, alt!( |
| 2300 | do_parse!( |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2301 | member: syn!(Member) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2302 | colon: punct!(:) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2303 | pat: syn!(Pat) >> |
| 2304 | (FieldPat { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2305 | member: member, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2306 | pat: Box::new(pat), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2307 | attrs: Vec::new(), |
| 2308 | colon_token: Some(colon), |
| 2309 | }) |
| 2310 | ) |
| 2311 | | |
| 2312 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2313 | boxed: option!(keyword!(box)) >> |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 2314 | by_ref: option!(keyword!(ref)) >> |
| 2315 | mutability: option!(keyword!(mut)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2316 | ident: syn!(Ident) >> |
| 2317 | ({ |
| 2318 | let mut pat: Pat = PatIdent { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 2319 | by_ref: by_ref, |
David Tolnay | efc96fb | 2017-12-29 02:03:15 -0500 | [diff] [blame] | 2320 | mutability: mutability, |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 2321 | ident: ident, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2322 | subpat: None, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2323 | }.into(); |
| 2324 | if let Some(boxed) = boxed { |
| 2325 | pat = PatBox { |
| 2326 | pat: Box::new(pat), |
| 2327 | box_token: boxed, |
| 2328 | }.into(); |
| 2329 | } |
| 2330 | FieldPat { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2331 | member: Member::Named(ident), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2332 | pat: Box::new(pat), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2333 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2334 | colon_token: None, |
| 2335 | } |
| 2336 | }) |
| 2337 | ) |
| 2338 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2339 | |
| 2340 | fn description() -> Option<&'static str> { |
| 2341 | Some("field pattern") |
| 2342 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2343 | } |
| 2344 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2345 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2346 | impl Synom for Member { |
| 2347 | named!(parse -> Self, alt!( |
| 2348 | syn!(Ident) => { Member::Named } |
| 2349 | | |
| 2350 | syn!(Index) => { Member::Unnamed } |
| 2351 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2352 | |
| 2353 | fn description() -> Option<&'static str> { |
| 2354 | Some("field member") |
| 2355 | } |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | #[cfg(feature = "full")] |
| 2359 | impl Synom for Index { |
| 2360 | named!(parse -> Self, do_parse!( |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 2361 | lit: syn!(LitInt) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2362 | ({ |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 2363 | if let IntSuffix::None = lit.suffix() { |
| 2364 | Index { index: lit.value() as u32, span: lit.span } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2365 | } else { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2366 | return parse_error(); |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 2367 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2368 | }) |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2369 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2370 | |
| 2371 | fn description() -> Option<&'static str> { |
| 2372 | Some("field index") |
| 2373 | } |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2374 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2375 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2376 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2377 | impl Synom for PatPath { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2378 | named!(parse -> Self, map!( |
| 2379 | syn!(ExprPath), |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 2380 | |p| PatPath { qself: p.qself, path: p.path } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2381 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2382 | |
| 2383 | fn description() -> Option<&'static str> { |
| 2384 | Some("path pattern") |
| 2385 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2386 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 2387 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2388 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2389 | impl Synom for PatTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2390 | named!(parse -> Self, do_parse!( |
| 2391 | data: parens!(do_parse!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 2392 | front: call!(Punctuated::parse_terminated) >> |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 2393 | dotdot: option!(cond_reduce!(front.empty_or_trailing(), |
| 2394 | tuple!(punct!(..), option!(punct!(,))) |
| 2395 | )) >> |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 2396 | back: cond!(match dotdot { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2397 | Some((_, Some(_))) => true, |
| 2398 | _ => false, |
| 2399 | }, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 2400 | Punctuated::parse_terminated) >> |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 2401 | (front, dotdot, back) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2402 | )) >> |
| 2403 | ({ |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 2404 | let (parens, (front, dotdot, back)) = data; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2405 | let (dotdot, trailing) = match dotdot { |
| 2406 | Some((a, b)) => (Some(a), Some(b)), |
| 2407 | None => (None, None), |
| 2408 | }; |
| 2409 | PatTuple { |
| 2410 | paren_token: parens, |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 2411 | front: front, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2412 | dot2_token: dotdot, |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 2413 | comma_token: trailing.unwrap_or_default(), |
| 2414 | back: back.unwrap_or_default(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2415 | } |
| 2416 | }) |
| 2417 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2418 | |
| 2419 | fn description() -> Option<&'static str> { |
| 2420 | Some("tuple pattern") |
| 2421 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2422 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 2423 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2424 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2425 | impl Synom for PatRef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2426 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2427 | and: punct!(&) >> |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 2428 | mutability: option!(keyword!(mut)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2429 | pat: syn!(Pat) >> |
| 2430 | (PatRef { |
| 2431 | pat: Box::new(pat), |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 2432 | mutability: mutability, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2433 | and_token: and, |
| 2434 | }) |
| 2435 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2436 | |
| 2437 | fn description() -> Option<&'static str> { |
| 2438 | Some("reference pattern") |
| 2439 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2440 | } |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 2441 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2442 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2443 | impl Synom for PatLit { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2444 | named!(parse -> Self, do_parse!( |
| 2445 | lit: pat_lit_expr >> |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2446 | (if let Expr::Path(_) = lit { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2447 | return parse_error(); // these need to be parsed by pat_path |
| 2448 | } else { |
| 2449 | PatLit { |
| 2450 | expr: Box::new(lit), |
| 2451 | } |
| 2452 | }) |
| 2453 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2454 | |
| 2455 | fn description() -> Option<&'static str> { |
| 2456 | Some("literal pattern") |
| 2457 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2458 | } |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 2459 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2460 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2461 | impl Synom for PatRange { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2462 | named!(parse -> Self, do_parse!( |
| 2463 | lo: pat_lit_expr >> |
| 2464 | limits: syn!(RangeLimits) >> |
| 2465 | hi: pat_lit_expr >> |
| 2466 | (PatRange { |
| 2467 | lo: Box::new(lo), |
| 2468 | hi: Box::new(hi), |
| 2469 | limits: limits, |
| 2470 | }) |
| 2471 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2472 | |
| 2473 | fn description() -> Option<&'static str> { |
| 2474 | Some("range pattern") |
| 2475 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2476 | } |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 2477 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2478 | #[cfg(feature = "full")] |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2479 | named!(pat_lit_expr -> Expr, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2480 | neg: option!(punct!(-)) >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2481 | v: alt!( |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2482 | syn!(ExprLit) => { Expr::Lit } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2483 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2484 | syn!(ExprPath) => { Expr::Path } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2485 | ) >> |
David Tolnay | c29b989 | 2017-12-27 22:58:14 -0500 | [diff] [blame] | 2486 | (if let Some(neg) = neg { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2487 | Expr::Unary(ExprUnary { |
| 2488 | attrs: Vec::new(), |
David Tolnay | c29b989 | 2017-12-27 22:58:14 -0500 | [diff] [blame] | 2489 | op: UnOp::Neg(neg), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 2490 | expr: Box::new(v) |
| 2491 | }) |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2492 | } else { |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 2493 | v |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2494 | }) |
| 2495 | )); |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 2496 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2497 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2498 | impl Synom for PatSlice { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2499 | named!(parse -> Self, map!( |
| 2500 | brackets!(do_parse!( |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 2501 | before: call!(Punctuated::parse_terminated) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2502 | middle: option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2503 | dots: punct!(..) >> |
| 2504 | trailing: option!(punct!(,)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2505 | (dots, trailing) |
| 2506 | )) >> |
| 2507 | after: cond!( |
| 2508 | match middle { |
| 2509 | Some((_, ref trailing)) => trailing.is_some(), |
| 2510 | _ => false, |
| 2511 | }, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 2512 | Punctuated::parse_terminated |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2513 | ) >> |
| 2514 | (before, middle, after) |
| 2515 | )), |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 2516 | |(brackets, (before, middle, after))| { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 2517 | let mut before: Punctuated<Pat, Token![,]> = before; |
| 2518 | let after: Option<Punctuated<Pat, Token![,]>> = after; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2519 | let middle: Option<(Token![..], Option<Token![,]>)> = middle; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2520 | PatSlice { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2521 | dot2_token: middle.as_ref().map(|m| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2522 | comma_token: middle.as_ref().and_then(|m| { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2523 | m.1.as_ref().map(|m| Token) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2524 | }), |
| 2525 | bracket_token: brackets, |
| 2526 | middle: middle.and_then(|_| { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 2527 | if before.empty_or_trailing() { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2528 | None |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame] | 2529 | } else { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 2530 | Some(Box::new(before.pop().unwrap().into_value())) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2531 | } |
| 2532 | }), |
| 2533 | front: before, |
| 2534 | back: after.unwrap_or_default(), |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 2535 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2536 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2537 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2538 | |
| 2539 | fn description() -> Option<&'static str> { |
| 2540 | Some("slice pattern") |
| 2541 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2542 | } |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 2543 | |
| 2544 | #[cfg(feature = "full")] |
| 2545 | impl Synom for PatMacro { |
| 2546 | named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac })); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 2547 | |
| 2548 | fn description() -> Option<&'static str> { |
| 2549 | Some("macro pattern") |
| 2550 | } |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 2551 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2554 | #[cfg(feature = "printing")] |
| 2555 | mod printing { |
| 2556 | use super::*; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2557 | #[cfg(feature = "full")] |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2558 | use attr::FilterAttrs; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2559 | use quote::{ToTokens, Tokens}; |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 2560 | use proc_macro2::{Literal, TokenNode, TokenTree}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2561 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 2562 | // If the given expression is a bare `ExprStruct`, wraps it in parenthesis |
| 2563 | // before appending it to `Tokens`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2564 | #[cfg(feature = "full")] |
| 2565 | fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2566 | if let Expr::Struct(_) = *e { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 2567 | token::Paren::default().surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2568 | e.to_tokens(tokens); |
| 2569 | }); |
| 2570 | } else { |
| 2571 | e.to_tokens(tokens); |
| 2572 | } |
| 2573 | } |
| 2574 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2575 | #[cfg(feature = "full")] |
| 2576 | fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) { |
| 2577 | tokens.append_all(attrs.outer()); |
| 2578 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2579 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2580 | #[cfg(not(feature = "full"))] |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 2581 | fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {} |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2582 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2583 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2584 | impl ToTokens for ExprBox { |
| 2585 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2586 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2587 | self.box_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2588 | self.expr.to_tokens(tokens); |
| 2589 | } |
| 2590 | } |
| 2591 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2592 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2593 | impl ToTokens for ExprInPlace { |
| 2594 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2595 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 8701a5c | 2017-12-28 23:31:10 -0500 | [diff] [blame] | 2596 | self.place.to_tokens(tokens); |
| 2597 | self.arrow_token.to_tokens(tokens); |
| 2598 | self.value.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2602 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2603 | impl ToTokens for ExprArray { |
| 2604 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2605 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2606 | self.bracket_token.surround(tokens, |tokens| { |
David Tolnay | 2a86fdd | 2017-12-28 23:34:28 -0500 | [diff] [blame] | 2607 | self.elems.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2608 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | impl ToTokens for ExprCall { |
| 2613 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2614 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2615 | self.func.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2616 | self.paren_token.surround(tokens, |tokens| { |
| 2617 | self.args.to_tokens(tokens); |
| 2618 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2619 | } |
| 2620 | } |
| 2621 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2622 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2623 | impl ToTokens for ExprMethodCall { |
| 2624 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2625 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 7641851 | 2017-12-28 23:47:47 -0500 | [diff] [blame] | 2626 | self.receiver.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2627 | self.dot_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2628 | self.method.to_tokens(tokens); |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 2629 | self.turbofish.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2630 | self.paren_token.surround(tokens, |tokens| { |
| 2631 | self.args.to_tokens(tokens); |
| 2632 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2636 | #[cfg(feature = "full")] |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 2637 | impl ToTokens for MethodTurbofish { |
| 2638 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2639 | self.colon2_token.to_tokens(tokens); |
| 2640 | self.lt_token.to_tokens(tokens); |
| 2641 | self.args.to_tokens(tokens); |
| 2642 | self.gt_token.to_tokens(tokens); |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | #[cfg(feature = "full")] |
| 2647 | impl ToTokens for GenericMethodArgument { |
| 2648 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2649 | match *self { |
| 2650 | GenericMethodArgument::Type(ref t) => t.to_tokens(tokens), |
| 2651 | GenericMethodArgument::Const(ref c) => c.to_tokens(tokens), |
| 2652 | } |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | #[cfg(feature = "full")] |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 2657 | impl ToTokens for ExprTuple { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2658 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2659 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2660 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | 2a86fdd | 2017-12-28 23:34:28 -0500 | [diff] [blame] | 2661 | self.elems.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2662 | // If we only have one argument, we need a trailing comma to |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 2663 | // distinguish ExprTuple from ExprParen. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 2664 | if self.elems.len() == 1 && !self.elems.trailing_punct() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2665 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2666 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2667 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | impl ToTokens for ExprBinary { |
| 2672 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2673 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2674 | self.left.to_tokens(tokens); |
| 2675 | self.op.to_tokens(tokens); |
| 2676 | self.right.to_tokens(tokens); |
| 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | impl ToTokens for ExprUnary { |
| 2681 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2682 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2683 | self.op.to_tokens(tokens); |
| 2684 | self.expr.to_tokens(tokens); |
| 2685 | } |
| 2686 | } |
| 2687 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2688 | impl ToTokens for ExprLit { |
| 2689 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2690 | attrs_to_tokens(&self.attrs, tokens); |
| 2691 | self.lit.to_tokens(tokens); |
| 2692 | } |
| 2693 | } |
| 2694 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2695 | impl ToTokens for ExprCast { |
| 2696 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2697 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2698 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2699 | self.as_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2700 | self.ty.to_tokens(tokens); |
| 2701 | } |
| 2702 | } |
| 2703 | |
David Tolnay | 0cf94f2 | 2017-12-28 23:46:26 -0500 | [diff] [blame] | 2704 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2705 | impl ToTokens for ExprType { |
| 2706 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2707 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2708 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2709 | self.colon_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2710 | self.ty.to_tokens(tokens); |
| 2711 | } |
| 2712 | } |
| 2713 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2714 | #[cfg(feature = "full")] |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 2715 | fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) { |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 2716 | if let Some((ref else_token, ref else_)) = *else_ { |
| 2717 | else_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2718 | |
| 2719 | // If we are not one of the valid expressions to exist in an else |
| 2720 | // clause, wrap ourselves in a block. |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 2721 | match **else_ { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2722 | Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => { |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 2723 | else_.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2724 | } |
| 2725 | _ => { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 2726 | token::Brace::default().surround(tokens, |tokens| { |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 2727 | else_.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2728 | }); |
| 2729 | } |
| 2730 | } |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2735 | impl ToTokens for ExprIf { |
| 2736 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2737 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2738 | self.if_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2739 | wrap_bare_struct(tokens, &self.cond); |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 2740 | self.then_branch.to_tokens(tokens); |
| 2741 | maybe_wrap_else(tokens, &self.else_branch); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2745 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2746 | impl ToTokens for ExprIfLet { |
| 2747 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2748 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2749 | self.if_token.to_tokens(tokens); |
| 2750 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2751 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2752 | self.eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2753 | wrap_bare_struct(tokens, &self.expr); |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 2754 | self.then_branch.to_tokens(tokens); |
| 2755 | maybe_wrap_else(tokens, &self.else_branch); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2759 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2760 | impl ToTokens for ExprWhile { |
| 2761 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2762 | tokens.append_all(self.attrs.outer()); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 2763 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2764 | self.while_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2765 | wrap_bare_struct(tokens, &self.cond); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2766 | self.body.to_tokens(tokens); |
| 2767 | } |
| 2768 | } |
| 2769 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2770 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2771 | impl ToTokens for ExprWhileLet { |
| 2772 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2773 | tokens.append_all(self.attrs.outer()); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 2774 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2775 | self.while_token.to_tokens(tokens); |
| 2776 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2777 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2778 | self.eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2779 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2780 | self.body.to_tokens(tokens); |
| 2781 | } |
| 2782 | } |
| 2783 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2784 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2785 | impl ToTokens for ExprForLoop { |
| 2786 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2787 | tokens.append_all(self.attrs.outer()); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 2788 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2789 | self.for_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2790 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2791 | self.in_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2792 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2793 | self.body.to_tokens(tokens); |
| 2794 | } |
| 2795 | } |
| 2796 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2797 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2798 | impl ToTokens for ExprLoop { |
| 2799 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2800 | tokens.append_all(self.attrs.outer()); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 2801 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2802 | self.loop_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2803 | self.body.to_tokens(tokens); |
| 2804 | } |
| 2805 | } |
| 2806 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2807 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2808 | impl ToTokens for ExprMatch { |
| 2809 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2810 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2811 | self.match_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2812 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2813 | self.brace_token.surround(tokens, |tokens| { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2814 | for (i, arm) in self.arms.iter().enumerate() { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2815 | arm.to_tokens(tokens); |
| 2816 | // Ensure that we have a comma after a non-block arm, except |
| 2817 | // for the last one. |
| 2818 | let is_last = i == self.arms.len() - 1; |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 2819 | if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2820 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2821 | } |
| 2822 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2823 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2827 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2828 | impl ToTokens for ExprCatch { |
| 2829 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2830 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2831 | self.do_token.to_tokens(tokens); |
| 2832 | self.catch_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2833 | self.block.to_tokens(tokens); |
| 2834 | } |
| 2835 | } |
| 2836 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2837 | #[cfg(feature = "full")] |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 2838 | impl ToTokens for ExprYield { |
| 2839 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2840 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 2841 | self.yield_token.to_tokens(tokens); |
| 2842 | self.expr.to_tokens(tokens); |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2847 | impl ToTokens for ExprClosure { |
| 2848 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2849 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2850 | self.capture.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2851 | self.or1_token.to_tokens(tokens); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 2852 | for input in self.inputs.pairs() { |
| 2853 | match **input.value() { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2854 | FnArg::Captured(ArgCaptured { |
| 2855 | ref pat, |
| 2856 | ty: Type::Infer(_), |
| 2857 | .. |
| 2858 | }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2859 | pat.to_tokens(tokens); |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 2860 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 2861 | _ => input.value().to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 2862 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 2863 | input.punct().to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2864 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2865 | self.or2_token.to_tokens(tokens); |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame] | 2866 | self.output.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2867 | self.body.to_tokens(tokens); |
| 2868 | } |
| 2869 | } |
| 2870 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2871 | #[cfg(feature = "full")] |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 2872 | impl ToTokens for ExprUnsafe { |
| 2873 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2874 | tokens.append_all(self.attrs.outer()); |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 2875 | self.unsafe_token.to_tokens(tokens); |
| 2876 | self.block.to_tokens(tokens); |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2881 | impl ToTokens for ExprBlock { |
| 2882 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2883 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2884 | self.block.to_tokens(tokens); |
| 2885 | } |
| 2886 | } |
| 2887 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2888 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2889 | impl ToTokens for ExprAssign { |
| 2890 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2891 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2892 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2893 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2894 | self.right.to_tokens(tokens); |
| 2895 | } |
| 2896 | } |
| 2897 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2898 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2899 | impl ToTokens for ExprAssignOp { |
| 2900 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2901 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2902 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2903 | self.op.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2904 | self.right.to_tokens(tokens); |
| 2905 | } |
| 2906 | } |
| 2907 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2908 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2909 | impl ToTokens for ExprField { |
| 2910 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2911 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2912 | self.base.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2913 | self.dot_token.to_tokens(tokens); |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2914 | self.member.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2918 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2919 | impl ToTokens for Member { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2920 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2921 | match *self { |
| 2922 | Member::Named(ident) => ident.to_tokens(tokens), |
| 2923 | Member::Unnamed(ref index) => index.to_tokens(tokens), |
| 2924 | } |
| 2925 | } |
| 2926 | } |
| 2927 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2928 | impl ToTokens for Index { |
| 2929 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2930 | tokens.append(TokenTree { |
| 2931 | span: self.span, |
David Tolnay | 9bce057 | 2017-12-27 22:24:09 -0500 | [diff] [blame] | 2932 | kind: TokenNode::Literal(Literal::integer(i64::from(self.index))), |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2933 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | impl ToTokens for ExprIndex { |
| 2938 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2939 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2940 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2941 | self.bracket_token.surround(tokens, |tokens| { |
| 2942 | self.index.to_tokens(tokens); |
| 2943 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2944 | } |
| 2945 | } |
| 2946 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2947 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2948 | impl ToTokens for ExprRange { |
| 2949 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2950 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2951 | self.from.to_tokens(tokens); |
David Tolnay | 475288a | 2017-12-19 22:59:44 -0800 | [diff] [blame] | 2952 | match self.limits { |
| 2953 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
| 2954 | RangeLimits::Closed(ref t) => t.to_tokens(tokens), |
| 2955 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2956 | self.to.to_tokens(tokens); |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | impl ToTokens for ExprPath { |
| 2961 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2962 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2963 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2967 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2968 | impl ToTokens for ExprAddrOf { |
| 2969 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2970 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2971 | self.and_token.to_tokens(tokens); |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 2972 | self.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2973 | self.expr.to_tokens(tokens); |
| 2974 | } |
| 2975 | } |
| 2976 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2977 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2978 | impl ToTokens for ExprBreak { |
| 2979 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2980 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2981 | self.break_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2982 | self.label.to_tokens(tokens); |
| 2983 | self.expr.to_tokens(tokens); |
| 2984 | } |
| 2985 | } |
| 2986 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2987 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2988 | impl ToTokens for ExprContinue { |
| 2989 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2990 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2991 | self.continue_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2992 | self.label.to_tokens(tokens); |
| 2993 | } |
| 2994 | } |
| 2995 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2996 | #[cfg(feature = "full")] |
David Tolnay | c246cd3 | 2017-12-28 23:14:32 -0500 | [diff] [blame] | 2997 | impl ToTokens for ExprReturn { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2998 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2999 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3000 | self.return_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3001 | self.expr.to_tokens(tokens); |
| 3002 | } |
| 3003 | } |
| 3004 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3005 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3006 | impl ToTokens for ExprMacro { |
| 3007 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3008 | tokens.append_all(self.attrs.outer()); |
| 3009 | self.mac.to_tokens(tokens); |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3014 | impl ToTokens for ExprStruct { |
| 3015 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3016 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3017 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3018 | self.brace_token.surround(tokens, |tokens| { |
| 3019 | self.fields.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3020 | if self.rest.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3021 | TokensOrDefault(&self.dot2_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3022 | self.rest.to_tokens(tokens); |
| 3023 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3024 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3025 | } |
| 3026 | } |
| 3027 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3028 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3029 | impl ToTokens for ExprRepeat { |
| 3030 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3031 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3032 | self.bracket_token.surround(tokens, |tokens| { |
| 3033 | self.expr.to_tokens(tokens); |
| 3034 | self.semi_token.to_tokens(tokens); |
David Tolnay | 84d8044 | 2018-01-07 01:03:20 -0800 | [diff] [blame] | 3035 | self.len.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3036 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3037 | } |
| 3038 | } |
| 3039 | |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 3040 | #[cfg(feature = "full")] |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 3041 | impl ToTokens for ExprGroup { |
| 3042 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3043 | attrs_to_tokens(&self.attrs, tokens); |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 3044 | self.group_token.surround(tokens, |tokens| { |
| 3045 | self.expr.to_tokens(tokens); |
| 3046 | }); |
| 3047 | } |
| 3048 | } |
| 3049 | |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 3050 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3051 | impl ToTokens for ExprParen { |
| 3052 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3053 | attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3054 | self.paren_token.surround(tokens, |tokens| { |
| 3055 | self.expr.to_tokens(tokens); |
| 3056 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3060 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3061 | impl ToTokens for ExprTry { |
| 3062 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3063 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3064 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3065 | self.question_token.to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3066 | } |
| 3067 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3068 | |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 3069 | impl ToTokens for ExprVerbatim { |
| 3070 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3071 | self.tts.to_tokens(tokens); |
| 3072 | } |
| 3073 | } |
| 3074 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3075 | #[cfg(feature = "full")] |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 3076 | impl ToTokens for Label { |
| 3077 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3078 | self.name.to_tokens(tokens); |
| 3079 | self.colon_token.to_tokens(tokens); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | #[cfg(feature = "full")] |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 3084 | impl ToTokens for FieldValue { |
| 3085 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3086 | self.member.to_tokens(tokens); |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 3087 | if let Some(ref colon_token) = self.colon_token { |
| 3088 | colon_token.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 3089 | self.expr.to_tokens(tokens); |
| 3090 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3094 | #[cfg(feature = "full")] |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3095 | impl ToTokens for Arm { |
| 3096 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3097 | tokens.append_all(&self.attrs); |
| 3098 | self.pats.to_tokens(tokens); |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3099 | if let Some((ref if_token, ref guard)) = self.guard { |
| 3100 | if_token.to_tokens(tokens); |
| 3101 | guard.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3102 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3103 | self.rocket_token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3104 | self.body.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3105 | self.comma.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3109 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3110 | impl ToTokens for PatWild { |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3111 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3112 | self.underscore_token.to_tokens(tokens); |
| 3113 | } |
| 3114 | } |
| 3115 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3116 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3117 | impl ToTokens for PatIdent { |
| 3118 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 3119 | self.by_ref.to_tokens(tokens); |
David Tolnay | efc96fb | 2017-12-29 02:03:15 -0500 | [diff] [blame] | 3120 | self.mutability.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3121 | self.ident.to_tokens(tokens); |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3122 | if let Some((ref at_token, ref subpat)) = self.subpat { |
| 3123 | at_token.to_tokens(tokens); |
| 3124 | subpat.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3125 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3129 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3130 | impl ToTokens for PatStruct { |
| 3131 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3132 | self.path.to_tokens(tokens); |
| 3133 | self.brace_token.surround(tokens, |tokens| { |
| 3134 | self.fields.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3135 | // NOTE: We need a comma before the dot2 token if it is present. |
| 3136 | if !self.fields.empty_or_trailing() && self.dot2_token.is_some() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3137 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3138 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3139 | self.dot2_token.to_tokens(tokens); |
| 3140 | }); |
| 3141 | } |
| 3142 | } |
| 3143 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3144 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3145 | impl ToTokens for PatTupleStruct { |
| 3146 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3147 | self.path.to_tokens(tokens); |
| 3148 | self.pat.to_tokens(tokens); |
| 3149 | } |
| 3150 | } |
| 3151 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3152 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3153 | impl ToTokens for PatPath { |
| 3154 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3155 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 3156 | } |
| 3157 | } |
| 3158 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3159 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3160 | impl ToTokens for PatTuple { |
| 3161 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3162 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 3163 | self.front.to_tokens(tokens); |
| 3164 | if let Some(ref dot2_token) = self.dot2_token { |
| 3165 | if !self.front.empty_or_trailing() { |
| 3166 | // Ensure there is a comma before the .. token. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3167 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3168 | } |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 3169 | dot2_token.to_tokens(tokens); |
| 3170 | self.comma_token.to_tokens(tokens); |
| 3171 | if self.comma_token.is_none() && !self.back.is_empty() { |
| 3172 | // Ensure there is a comma after the .. token. |
| 3173 | <Token![,]>::default().to_tokens(tokens); |
| 3174 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 3175 | } |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 3176 | self.back.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3177 | }); |
| 3178 | } |
| 3179 | } |
| 3180 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3181 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3182 | impl ToTokens for PatBox { |
| 3183 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3184 | self.box_token.to_tokens(tokens); |
| 3185 | self.pat.to_tokens(tokens); |
| 3186 | } |
| 3187 | } |
| 3188 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3189 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3190 | impl ToTokens for PatRef { |
| 3191 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3192 | self.and_token.to_tokens(tokens); |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 3193 | self.mutability.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3194 | self.pat.to_tokens(tokens); |
| 3195 | } |
| 3196 | } |
| 3197 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3198 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3199 | impl ToTokens for PatLit { |
| 3200 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3201 | self.expr.to_tokens(tokens); |
| 3202 | } |
| 3203 | } |
| 3204 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3205 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3206 | impl ToTokens for PatRange { |
| 3207 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3208 | self.lo.to_tokens(tokens); |
David Tolnay | 475288a | 2017-12-19 22:59:44 -0800 | [diff] [blame] | 3209 | match self.limits { |
| 3210 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
| 3211 | RangeLimits::Closed(ref t) => Token.to_tokens(tokens), |
| 3212 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3213 | self.hi.to_tokens(tokens); |
| 3214 | } |
| 3215 | } |
| 3216 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3217 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3218 | impl ToTokens for PatSlice { |
| 3219 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3220 | // XXX: This is a mess, and it will be so easy to screw it up. How |
| 3221 | // do we make this correct itself better? |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3222 | self.bracket_token.surround(tokens, |tokens| { |
| 3223 | self.front.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3224 | |
| 3225 | // If we need a comma before the middle or standalone .. token, |
| 3226 | // then make sure it's present. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 3227 | if !self.front.empty_or_trailing() |
| 3228 | && (self.middle.is_some() || self.dot2_token.is_some()) |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3229 | { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3230 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | // If we have an identifier, we always need a .. token. |
| 3234 | if self.middle.is_some() { |
| 3235 | self.middle.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3236 | TokensOrDefault(&self.dot2_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3237 | } else if self.dot2_token.is_some() { |
| 3238 | self.dot2_token.to_tokens(tokens); |
| 3239 | } |
| 3240 | |
| 3241 | // Make sure we have a comma before the back half. |
| 3242 | if !self.back.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3243 | TokensOrDefault(&self.comma_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3244 | self.back.to_tokens(tokens); |
| 3245 | } else { |
| 3246 | self.comma_token.to_tokens(tokens); |
| 3247 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3248 | }) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3249 | } |
| 3250 | } |
| 3251 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3252 | #[cfg(feature = "full")] |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 3253 | impl ToTokens for PatMacro { |
| 3254 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3255 | self.mac.to_tokens(tokens); |
| 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | #[cfg(feature = "full")] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 3260 | impl ToTokens for PatVerbatim { |
| 3261 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3262 | self.tts.to_tokens(tokens); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | #[cfg(feature = "full")] |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 3267 | impl ToTokens for FieldPat { |
| 3268 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 3269 | if let Some(ref colon_token) = self.colon_token { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3270 | self.member.to_tokens(tokens); |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 3271 | colon_token.to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 3272 | } |
| 3273 | self.pat.to_tokens(tokens); |
| 3274 | } |
| 3275 | } |
| 3276 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3277 | #[cfg(feature = "full")] |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3278 | impl ToTokens for Block { |
| 3279 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3280 | self.brace_token.surround(tokens, |tokens| { |
| 3281 | tokens.append_all(&self.stmts); |
| 3282 | }); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3283 | } |
| 3284 | } |
| 3285 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3286 | #[cfg(feature = "full")] |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3287 | impl ToTokens for Stmt { |
| 3288 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 3289 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3290 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3291 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 3292 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3293 | Stmt::Semi(ref expr, ref semi) => { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3294 | expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3295 | semi.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3296 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3297 | } |
| 3298 | } |
| 3299 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3300 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3301 | #[cfg(feature = "full")] |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3302 | impl ToTokens for Local { |
| 3303 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4e3158d | 2016-10-30 00:30:01 -0700 | [diff] [blame] | 3304 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3305 | self.let_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3306 | self.pat.to_tokens(tokens); |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3307 | if let Some((ref colon_token, ref ty)) = self.ty { |
| 3308 | colon_token.to_tokens(tokens); |
| 3309 | ty.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3310 | } |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3311 | if let Some((ref eq_token, ref init)) = self.init { |
| 3312 | eq_token.to_tokens(tokens); |
| 3313 | init.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3314 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3315 | self.semi_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3316 | } |
| 3317 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3318 | } |