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::*; |
| 80 | |
| 81 | named!(pub binop -> BinOp, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 82 | punct!("&&") => { |_| BinOp::And(tokens::AndAnd::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 83 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 84 | punct!("||") => { |_| BinOp::Or(tokens::OrOr::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 85 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 86 | punct!("<<") => { |_| BinOp::Shl(tokens::Shl::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 87 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 88 | punct!(">>") => { |_| BinOp::Shr(tokens::Shr::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 89 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 90 | punct!("==") => { |_| BinOp::Eq(tokens::EqEq::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 91 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 92 | punct!("<=") => { |_| BinOp::Le(tokens::Le::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 93 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 94 | punct!("!=") => { |_| BinOp::Ne(tokens::Ne::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 95 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 96 | punct!(">=") => { |_| BinOp::Ge(tokens::Ge::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 97 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 98 | punct!("+") => { |_| BinOp::Add(tokens::Add::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 99 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 100 | punct!("-") => { |_| BinOp::Sub(tokens::Sub::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 101 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 102 | punct!("*") => { |_| BinOp::Mul(tokens::Star::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 103 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 104 | punct!("/") => { |_| BinOp::Div(tokens::Div::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 105 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 106 | punct!("%") => { |_| BinOp::Rem(tokens::Rem::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 107 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 108 | punct!("^") => { |_| BinOp::BitXor(tokens::Caret::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 109 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 110 | punct!("&") => { |_| BinOp::BitAnd(tokens::And::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 111 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 112 | punct!("|") => { |_| BinOp::BitOr(tokens::Or::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 113 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 114 | punct!("<") => { |_| BinOp::Lt(tokens::Lt::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 115 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 116 | punct!(">") => { |_| BinOp::Gt(tokens::Gt::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 117 | )); |
| 118 | |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 119 | #[cfg(feature = "full")] |
| 120 | named!(pub assign_op -> BinOp, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 121 | punct!("+=") => { |_| BinOp::AddEq(tokens::AddEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 122 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 123 | punct!("-=") => { |_| BinOp::SubEq(tokens::SubEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 124 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 125 | punct!("*=") => { |_| BinOp::MulEq(tokens::MulEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 126 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 127 | punct!("/=") => { |_| BinOp::DivEq(tokens::DivEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 128 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 129 | punct!("%=") => { |_| BinOp::RemEq(tokens::RemEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 130 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 131 | punct!("^=") => { |_| BinOp::BitXorEq(tokens::CaretEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 132 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 133 | punct!("&=") => { |_| BinOp::BitAndEq(tokens::AndEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 134 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 135 | punct!("|=") => { |_| BinOp::BitOrEq(tokens::OrEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 136 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 137 | punct!("<<=") => { |_| BinOp::ShlEq(tokens::ShlEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 138 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 139 | punct!(">>=") => { |_| BinOp::ShrEq(tokens::ShrEq::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 140 | )); |
| 141 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 142 | named!(pub unop -> UnOp, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 143 | punct!("*") => { |_| UnOp::Deref(tokens::Star::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 144 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 145 | punct!("!") => { |_| UnOp::Not(tokens::Bang::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 146 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 147 | punct!("-") => { |_| UnOp::Neg(tokens::Sub::default()) } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 148 | )); |
| 149 | } |
| 150 | |
| 151 | #[cfg(feature = "printing")] |
| 152 | mod printing { |
| 153 | use super::*; |
| 154 | use quote::{Tokens, ToTokens}; |
| 155 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 156 | impl ToTokens for BinOp { |
| 157 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 158 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 159 | BinOp::Add(ref t) => t.to_tokens(tokens), |
| 160 | BinOp::Sub(ref t) => t.to_tokens(tokens), |
| 161 | BinOp::Mul(ref t) => t.to_tokens(tokens), |
| 162 | BinOp::Div(ref t) => t.to_tokens(tokens), |
| 163 | BinOp::Rem(ref t) => t.to_tokens(tokens), |
| 164 | BinOp::And(ref t) => t.to_tokens(tokens), |
| 165 | BinOp::Or(ref t) => t.to_tokens(tokens), |
| 166 | BinOp::BitXor(ref t) => t.to_tokens(tokens), |
| 167 | BinOp::BitAnd(ref t) => t.to_tokens(tokens), |
| 168 | BinOp::BitOr(ref t) => t.to_tokens(tokens), |
| 169 | BinOp::Shl(ref t) => t.to_tokens(tokens), |
| 170 | BinOp::Shr(ref t) => t.to_tokens(tokens), |
| 171 | BinOp::Eq(ref t) => t.to_tokens(tokens), |
| 172 | BinOp::Lt(ref t) => t.to_tokens(tokens), |
| 173 | BinOp::Le(ref t) => t.to_tokens(tokens), |
| 174 | BinOp::Ne(ref t) => t.to_tokens(tokens), |
| 175 | BinOp::Ge(ref t) => t.to_tokens(tokens), |
| 176 | BinOp::Gt(ref t) => t.to_tokens(tokens), |
| 177 | BinOp::AddEq(ref t) => t.to_tokens(tokens), |
| 178 | BinOp::SubEq(ref t) => t.to_tokens(tokens), |
| 179 | BinOp::MulEq(ref t) => t.to_tokens(tokens), |
| 180 | BinOp::DivEq(ref t) => t.to_tokens(tokens), |
| 181 | BinOp::RemEq(ref t) => t.to_tokens(tokens), |
| 182 | BinOp::BitXorEq(ref t) => t.to_tokens(tokens), |
| 183 | BinOp::BitAndEq(ref t) => t.to_tokens(tokens), |
| 184 | BinOp::BitOrEq(ref t) => t.to_tokens(tokens), |
| 185 | BinOp::ShlEq(ref t) => t.to_tokens(tokens), |
| 186 | BinOp::ShrEq(ref t) => t.to_tokens(tokens), |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl ToTokens for UnOp { |
| 192 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 193 | match *self { |
| 194 | UnOp::Deref(ref t) => t.to_tokens(tokens), |
| 195 | UnOp::Not(ref t) => t.to_tokens(tokens), |
| 196 | UnOp::Neg(ref t) => t.to_tokens(tokens), |
| 197 | } |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |