David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame^] | 1 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 2 | pub enum BinOp { |
| 3 | /// The `+` operator (addition) |
| 4 | Add, |
| 5 | /// The `-` operator (subtraction) |
| 6 | Sub, |
| 7 | /// The `*` operator (multiplication) |
| 8 | Mul, |
| 9 | /// The `/` operator (division) |
| 10 | Div, |
| 11 | /// The `%` operator (modulus) |
| 12 | Rem, |
| 13 | /// The `&&` operator (logical and) |
| 14 | And, |
| 15 | /// The `||` operator (logical or) |
| 16 | Or, |
| 17 | /// The `^` operator (bitwise xor) |
| 18 | BitXor, |
| 19 | /// The `&` operator (bitwise and) |
| 20 | BitAnd, |
| 21 | /// The `|` operator (bitwise or) |
| 22 | BitOr, |
| 23 | /// The `<<` operator (shift left) |
| 24 | Shl, |
| 25 | /// The `>>` operator (shift right) |
| 26 | Shr, |
| 27 | /// The `==` operator (equality) |
| 28 | Eq, |
| 29 | /// The `<` operator (less than) |
| 30 | Lt, |
| 31 | /// The `<=` operator (less than or equal to) |
| 32 | Le, |
| 33 | /// The `!=` operator (not equal to) |
| 34 | Ne, |
| 35 | /// The `>=` operator (greater than or equal to) |
| 36 | Ge, |
| 37 | /// The `>` operator (greater than) |
| 38 | Gt, |
| 39 | } |
| 40 | |
| 41 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 42 | pub enum UnOp { |
| 43 | /// The `*` operator for dereferencing |
| 44 | Deref, |
| 45 | /// The `!` operator for logical inversion |
| 46 | Not, |
| 47 | /// The `-` operator for negation |
| 48 | Neg, |
| 49 | } |
| 50 | |
| 51 | #[cfg(feature = "parsing")] |
| 52 | pub mod parsing { |
| 53 | use super::*; |
| 54 | |
| 55 | named!(pub binop -> BinOp, alt!( |
| 56 | punct!("&&") => { |_| BinOp::And } |
| 57 | | |
| 58 | punct!("||") => { |_| BinOp::Or } |
| 59 | | |
| 60 | punct!("<<") => { |_| BinOp::Shl } |
| 61 | | |
| 62 | punct!(">>") => { |_| BinOp::Shr } |
| 63 | | |
| 64 | punct!("==") => { |_| BinOp::Eq } |
| 65 | | |
| 66 | punct!("<=") => { |_| BinOp::Le } |
| 67 | | |
| 68 | punct!("!=") => { |_| BinOp::Ne } |
| 69 | | |
| 70 | punct!(">=") => { |_| BinOp::Ge } |
| 71 | | |
| 72 | punct!("+") => { |_| BinOp::Add } |
| 73 | | |
| 74 | punct!("-") => { |_| BinOp::Sub } |
| 75 | | |
| 76 | punct!("*") => { |_| BinOp::Mul } |
| 77 | | |
| 78 | punct!("/") => { |_| BinOp::Div } |
| 79 | | |
| 80 | punct!("%") => { |_| BinOp::Rem } |
| 81 | | |
| 82 | punct!("^") => { |_| BinOp::BitXor } |
| 83 | | |
| 84 | punct!("&") => { |_| BinOp::BitAnd } |
| 85 | | |
| 86 | punct!("|") => { |_| BinOp::BitOr } |
| 87 | | |
| 88 | punct!("<") => { |_| BinOp::Lt } |
| 89 | | |
| 90 | punct!(">") => { |_| BinOp::Gt } |
| 91 | )); |
| 92 | |
| 93 | named!(pub unop -> UnOp, alt!( |
| 94 | punct!("*") => { |_| UnOp::Deref } |
| 95 | | |
| 96 | punct!("!") => { |_| UnOp::Not } |
| 97 | | |
| 98 | punct!("-") => { |_| UnOp::Neg } |
| 99 | )); |
| 100 | } |
| 101 | |
| 102 | #[cfg(feature = "printing")] |
| 103 | mod printing { |
| 104 | use super::*; |
| 105 | use quote::{Tokens, ToTokens}; |
| 106 | |
| 107 | impl BinOp { |
| 108 | pub fn op(&self) -> &'static str { |
| 109 | match *self { |
| 110 | BinOp::Add => "+", |
| 111 | BinOp::Sub => "-", |
| 112 | BinOp::Mul => "*", |
| 113 | BinOp::Div => "/", |
| 114 | BinOp::Rem => "%", |
| 115 | BinOp::And => "&&", |
| 116 | BinOp::Or => "||", |
| 117 | BinOp::BitXor => "^", |
| 118 | BinOp::BitAnd => "&", |
| 119 | BinOp::BitOr => "|", |
| 120 | BinOp::Shl => "<<", |
| 121 | BinOp::Shr => ">>", |
| 122 | BinOp::Eq => "==", |
| 123 | BinOp::Lt => "<", |
| 124 | BinOp::Le => "<=", |
| 125 | BinOp::Ne => "!=", |
| 126 | BinOp::Ge => ">=", |
| 127 | BinOp::Gt => ">", |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pub fn assign_op(&self) -> Option<&'static str> { |
| 132 | match *self { |
| 133 | BinOp::Add => Some("+="), |
| 134 | BinOp::Sub => Some("-="), |
| 135 | BinOp::Mul => Some("*="), |
| 136 | BinOp::Div => Some("/="), |
| 137 | BinOp::Rem => Some("%="), |
| 138 | BinOp::BitXor => Some("^="), |
| 139 | BinOp::BitAnd => Some("&="), |
| 140 | BinOp::BitOr => Some("|="), |
| 141 | BinOp::Shl => Some("<<="), |
| 142 | BinOp::Shr => Some(">>="), |
| 143 | _ => None, |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl ToTokens for BinOp { |
| 149 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 150 | tokens.append(self.op()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl UnOp { |
| 155 | pub fn op(&self) -> &'static str { |
| 156 | match *self { |
| 157 | UnOp::Deref => "*", |
| 158 | UnOp::Not => "!", |
| 159 | UnOp::Neg => "-", |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | impl ToTokens for UnOp { |
| 165 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 166 | tokens.append(self.op()); |
| 167 | } |
| 168 | } |
| 169 | } |