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