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