blob: ffe181f518ecaa002c4c17e8837ee918118510a1 [file] [log] [blame]
David Tolnay3cb23a92016-10-07 23:02:21 -07001#[derive(Debug, Copy, Clone, Eq, PartialEq)]
2pub 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)]
42pub 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")]
52pub 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
David Tolnay438c9052016-10-07 23:24:48 -070093 #[cfg(feature = "full")]
94 named!(pub assign_op -> BinOp, alt!(
95 punct!("+=") => { |_| BinOp::Add }
96 |
97 punct!("-=") => { |_| BinOp::Sub }
98 |
99 punct!("*=") => { |_| BinOp::Mul }
100 |
101 punct!("/=") => { |_| BinOp::Div }
102 |
103 punct!("%=") => { |_| BinOp::Rem }
104 |
105 punct!("^=") => { |_| BinOp::BitXor }
106 |
107 punct!("&=") => { |_| BinOp::BitAnd }
108 |
109 punct!("|=") => { |_| BinOp::BitOr }
110 |
111 punct!("<<=") => { |_| BinOp::Shl }
112 |
113 punct!(">>=") => { |_| BinOp::Shr }
114 ));
115
David Tolnay3cb23a92016-10-07 23:02:21 -0700116 named!(pub unop -> UnOp, alt!(
117 punct!("*") => { |_| UnOp::Deref }
118 |
119 punct!("!") => { |_| UnOp::Not }
120 |
121 punct!("-") => { |_| UnOp::Neg }
122 ));
123}
124
125#[cfg(feature = "printing")]
126mod printing {
127 use super::*;
128 use quote::{Tokens, ToTokens};
129
130 impl BinOp {
131 pub fn op(&self) -> &'static str {
132 match *self {
133 BinOp::Add => "+",
134 BinOp::Sub => "-",
135 BinOp::Mul => "*",
136 BinOp::Div => "/",
137 BinOp::Rem => "%",
138 BinOp::And => "&&",
139 BinOp::Or => "||",
140 BinOp::BitXor => "^",
141 BinOp::BitAnd => "&",
142 BinOp::BitOr => "|",
143 BinOp::Shl => "<<",
144 BinOp::Shr => ">>",
145 BinOp::Eq => "==",
146 BinOp::Lt => "<",
147 BinOp::Le => "<=",
148 BinOp::Ne => "!=",
149 BinOp::Ge => ">=",
150 BinOp::Gt => ">",
151 }
152 }
153
154 pub fn assign_op(&self) -> Option<&'static str> {
155 match *self {
156 BinOp::Add => Some("+="),
157 BinOp::Sub => Some("-="),
158 BinOp::Mul => Some("*="),
159 BinOp::Div => Some("/="),
160 BinOp::Rem => Some("%="),
161 BinOp::BitXor => Some("^="),
162 BinOp::BitAnd => Some("&="),
163 BinOp::BitOr => Some("|="),
164 BinOp::Shl => Some("<<="),
165 BinOp::Shr => Some(">>="),
166 _ => None,
167 }
168 }
169 }
170
171 impl ToTokens for BinOp {
172 fn to_tokens(&self, tokens: &mut Tokens) {
173 tokens.append(self.op());
174 }
175 }
176
177 impl UnOp {
178 pub fn op(&self) -> &'static str {
179 match *self {
180 UnOp::Deref => "*",
181 UnOp::Not => "!",
182 UnOp::Neg => "-",
183 }
184 }
185 }
186
187 impl ToTokens for UnOp {
188 fn to_tokens(&self, tokens: &mut Tokens) {
189 tokens.append(self.op());
190 }
191 }
192}