Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1 | ast_enum! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 2 | /// A binary operator: `+`, `+=`, `&`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 3 | /// |
| 4 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 5 | /// feature.* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 6 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 7 | pub enum BinOp { |
| 8 | /// The `+` operator (addition) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 9 | Add(Token![+]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 10 | /// The `-` operator (subtraction) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 11 | Sub(Token![-]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 12 | /// The `*` operator (multiplication) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 13 | Mul(Token![*]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 14 | /// The `/` operator (division) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 15 | Div(Token![/]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 16 | /// The `%` operator (modulus) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 17 | Rem(Token![%]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | /// The `&&` operator (logical and) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 19 | And(Token![&&]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 20 | /// The `||` operator (logical or) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 21 | Or(Token![||]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 22 | /// The `^` operator (bitwise xor) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 23 | BitXor(Token![^]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 24 | /// The `&` operator (bitwise and) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 25 | BitAnd(Token![&]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 26 | /// The `|` operator (bitwise or) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 27 | BitOr(Token![|]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 28 | /// The `<<` operator (shift left) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 29 | Shl(Token![<<]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 30 | /// The `>>` operator (shift right) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 31 | Shr(Token![>>]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 32 | /// The `==` operator (equality) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 33 | Eq(Token![==]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 34 | /// The `<` operator (less than) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 35 | Lt(Token![<]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 36 | /// The `<=` operator (less than or equal to) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 37 | Le(Token![<=]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 38 | /// The `!=` operator (not equal to) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 39 | Ne(Token![!=]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 40 | /// The `>=` operator (greater than or equal to) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 41 | Ge(Token![>=]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 42 | /// The `>` operator (greater than) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 43 | Gt(Token![>]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 44 | /// The `+=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 45 | AddEq(Token![+=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 46 | /// The `-=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 47 | SubEq(Token![-=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 48 | /// The `*=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 49 | MulEq(Token![*=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 50 | /// The `/=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 51 | DivEq(Token![/=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 52 | /// The `%=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 53 | RemEq(Token![%=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 54 | /// The `^=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 55 | BitXorEq(Token![^=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 56 | /// The `&=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 57 | BitAndEq(Token![&=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 58 | /// The `|=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 59 | BitOrEq(Token![|=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 60 | /// The `<<=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 61 | ShlEq(Token![<<=]), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 62 | /// The `>>=` operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 63 | ShrEq(Token![>>=]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 64 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 67 | ast_enum! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 68 | /// A unary operator: `*`, `!`, `-`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 69 | /// |
| 70 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 71 | /// feature.* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 72 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 73 | pub enum UnOp { |
| 74 | /// The `*` operator for dereferencing |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 75 | Deref(Token![*]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 76 | /// The `!` operator for logical inversion |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 77 | Not(Token![!]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 78 | /// The `-` operator for negation |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 79 | Neg(Token![-]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 80 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #[cfg(feature = "parsing")] |
| 84 | pub mod parsing { |
| 85 | use super::*; |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 86 | |
| 87 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 88 | |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 89 | fn parse_binop(input: ParseStream) -> Result<BinOp> { |
| 90 | if input.peek(Token![&&]) { |
| 91 | input.parse().map(BinOp::And) |
| 92 | } else if input.peek(Token![||]) { |
| 93 | input.parse().map(BinOp::Or) |
| 94 | } else if input.peek(Token![<<]) { |
| 95 | input.parse().map(BinOp::Shl) |
| 96 | } else if input.peek(Token![>>]) { |
| 97 | input.parse().map(BinOp::Shr) |
| 98 | } else if input.peek(Token![==]) { |
| 99 | input.parse().map(BinOp::Eq) |
| 100 | } else if input.peek(Token![<=]) { |
| 101 | input.parse().map(BinOp::Le) |
| 102 | } else if input.peek(Token![!=]) { |
| 103 | input.parse().map(BinOp::Ne) |
| 104 | } else if input.peek(Token![>=]) { |
| 105 | input.parse().map(BinOp::Ge) |
| 106 | } else if input.peek(Token![+]) { |
| 107 | input.parse().map(BinOp::Add) |
| 108 | } else if input.peek(Token![-]) { |
| 109 | input.parse().map(BinOp::Sub) |
| 110 | } else if input.peek(Token![*]) { |
| 111 | input.parse().map(BinOp::Mul) |
| 112 | } else if input.peek(Token![/]) { |
| 113 | input.parse().map(BinOp::Div) |
| 114 | } else if input.peek(Token![%]) { |
| 115 | input.parse().map(BinOp::Rem) |
| 116 | } else if input.peek(Token![^]) { |
| 117 | input.parse().map(BinOp::BitXor) |
| 118 | } else if input.peek(Token![&]) { |
| 119 | input.parse().map(BinOp::BitAnd) |
| 120 | } else if input.peek(Token![|]) { |
| 121 | input.parse().map(BinOp::BitOr) |
| 122 | } else if input.peek(Token![<]) { |
| 123 | input.parse().map(BinOp::Lt) |
| 124 | } else if input.peek(Token![>]) { |
| 125 | input.parse().map(BinOp::Gt) |
| 126 | } else { |
| 127 | Err(input.error("expected binary operator")) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | impl Parse for BinOp { |
| 132 | #[cfg(not(feature = "full"))] |
| 133 | fn parse(input: ParseStream) -> Result<Self> { |
| 134 | parse_binop(input) |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 135 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 136 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 137 | #[cfg(feature = "full")] |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 138 | fn parse(input: ParseStream) -> Result<Self> { |
| 139 | if input.peek(Token![+=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 140 | input.parse().map(BinOp::AddEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 141 | } else if input.peek(Token![-=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 142 | input.parse().map(BinOp::SubEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 143 | } else if input.peek(Token![*=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 144 | input.parse().map(BinOp::MulEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 145 | } else if input.peek(Token![/=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 146 | input.parse().map(BinOp::DivEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 147 | } else if input.peek(Token![%=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 148 | input.parse().map(BinOp::RemEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 149 | } else if input.peek(Token![^=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 150 | input.parse().map(BinOp::BitXorEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 151 | } else if input.peek(Token![&=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 152 | input.parse().map(BinOp::BitAndEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 153 | } else if input.peek(Token![|=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 154 | input.parse().map(BinOp::BitOrEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 155 | } else if input.peek(Token![<<=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 156 | input.parse().map(BinOp::ShlEq) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 157 | } else if input.peek(Token![>>=]) { |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 158 | input.parse().map(BinOp::ShrEq) |
| 159 | } else { |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 160 | parse_binop(input) |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 161 | } |
| 162 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 163 | } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 164 | |
David Tolnay | 2a54cfb | 2018-08-26 18:54:19 -0400 | [diff] [blame] | 165 | impl Parse for UnOp { |
| 166 | fn parse(input: ParseStream) -> Result<Self> { |
| 167 | let lookahead = input.lookahead1(); |
| 168 | if lookahead.peek(Token![*]) { |
| 169 | input.parse().map(UnOp::Deref) |
| 170 | } else if lookahead.peek(Token![!]) { |
| 171 | input.parse().map(UnOp::Not) |
| 172 | } else if lookahead.peek(Token![-]) { |
| 173 | input.parse().map(UnOp::Neg) |
| 174 | } else { |
| 175 | Err(lookahead.error()) |
| 176 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 177 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 178 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | #[cfg(feature = "printing")] |
| 182 | mod printing { |
| 183 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 184 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 185 | use quote::ToTokens; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 186 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 187 | impl ToTokens for BinOp { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 188 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 189 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 190 | BinOp::Add(ref t) => t.to_tokens(tokens), |
| 191 | BinOp::Sub(ref t) => t.to_tokens(tokens), |
| 192 | BinOp::Mul(ref t) => t.to_tokens(tokens), |
| 193 | BinOp::Div(ref t) => t.to_tokens(tokens), |
| 194 | BinOp::Rem(ref t) => t.to_tokens(tokens), |
| 195 | BinOp::And(ref t) => t.to_tokens(tokens), |
| 196 | BinOp::Or(ref t) => t.to_tokens(tokens), |
| 197 | BinOp::BitXor(ref t) => t.to_tokens(tokens), |
| 198 | BinOp::BitAnd(ref t) => t.to_tokens(tokens), |
| 199 | BinOp::BitOr(ref t) => t.to_tokens(tokens), |
| 200 | BinOp::Shl(ref t) => t.to_tokens(tokens), |
| 201 | BinOp::Shr(ref t) => t.to_tokens(tokens), |
| 202 | BinOp::Eq(ref t) => t.to_tokens(tokens), |
| 203 | BinOp::Lt(ref t) => t.to_tokens(tokens), |
| 204 | BinOp::Le(ref t) => t.to_tokens(tokens), |
| 205 | BinOp::Ne(ref t) => t.to_tokens(tokens), |
| 206 | BinOp::Ge(ref t) => t.to_tokens(tokens), |
| 207 | BinOp::Gt(ref t) => t.to_tokens(tokens), |
| 208 | BinOp::AddEq(ref t) => t.to_tokens(tokens), |
| 209 | BinOp::SubEq(ref t) => t.to_tokens(tokens), |
| 210 | BinOp::MulEq(ref t) => t.to_tokens(tokens), |
| 211 | BinOp::DivEq(ref t) => t.to_tokens(tokens), |
| 212 | BinOp::RemEq(ref t) => t.to_tokens(tokens), |
| 213 | BinOp::BitXorEq(ref t) => t.to_tokens(tokens), |
| 214 | BinOp::BitAndEq(ref t) => t.to_tokens(tokens), |
| 215 | BinOp::BitOrEq(ref t) => t.to_tokens(tokens), |
| 216 | BinOp::ShlEq(ref t) => t.to_tokens(tokens), |
| 217 | BinOp::ShrEq(ref t) => t.to_tokens(tokens), |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | impl ToTokens for UnOp { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 223 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 224 | match *self { |
| 225 | UnOp::Deref(ref t) => t.to_tokens(tokens), |
| 226 | UnOp::Not(ref t) => t.to_tokens(tokens), |
| 227 | UnOp::Neg(ref t) => t.to_tokens(tokens), |
| 228 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |