blob: a044359f60eb6204092bc5e11331e07b1019ae63 [file] [log] [blame]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001use tokens;
2
Alex Crichton62a0a592017-05-22 13:58:53 -07003ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -07004 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -07005 pub enum BinOp {
6 /// The `+` operator (addition)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07007 Add(tokens::Add),
Alex Crichton62a0a592017-05-22 13:58:53 -07008 /// The `-` operator (subtraction)
Alex Crichtonccbb45d2017-05-23 10:58:24 -07009 Sub(tokens::Sub),
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// The `*` operator (multiplication)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070011 Mul(tokens::Star),
Alex Crichton62a0a592017-05-22 13:58:53 -070012 /// The `/` operator (division)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070013 Div(tokens::Div),
Alex Crichton62a0a592017-05-22 13:58:53 -070014 /// The `%` operator (modulus)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070015 Rem(tokens::Rem),
Alex Crichton62a0a592017-05-22 13:58:53 -070016 /// The `&&` operator (logical and)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070017 And(tokens::AndAnd),
Alex Crichton62a0a592017-05-22 13:58:53 -070018 /// The `||` operator (logical or)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070019 Or(tokens::OrOr),
Alex Crichton62a0a592017-05-22 13:58:53 -070020 /// The `^` operator (bitwise xor)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070021 BitXor(tokens::Caret),
Alex Crichton62a0a592017-05-22 13:58:53 -070022 /// The `&` operator (bitwise and)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070023 BitAnd(tokens::And),
Alex Crichton62a0a592017-05-22 13:58:53 -070024 /// The `|` operator (bitwise or)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070025 BitOr(tokens::Or),
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// The `<<` operator (shift left)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070027 Shl(tokens::Shl),
Alex Crichton62a0a592017-05-22 13:58:53 -070028 /// The `>>` operator (shift right)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070029 Shr(tokens::Shr),
Alex Crichton62a0a592017-05-22 13:58:53 -070030 /// The `==` operator (equality)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070031 Eq(tokens::EqEq),
Alex Crichton62a0a592017-05-22 13:58:53 -070032 /// The `<` operator (less than)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070033 Lt(tokens::Lt),
Alex Crichton62a0a592017-05-22 13:58:53 -070034 /// The `<=` operator (less than or equal to)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070035 Le(tokens::Le),
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// The `!=` operator (not equal to)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070037 Ne(tokens::Ne),
Alex Crichton62a0a592017-05-22 13:58:53 -070038 /// The `>=` operator (greater than or equal to)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070039 Ge(tokens::Ge),
Alex Crichton62a0a592017-05-22 13:58:53 -070040 /// The `>` operator (greater than)
Alex Crichtonccbb45d2017-05-23 10:58:24 -070041 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 Crichton62a0a592017-05-22 13:58:53 -070062 }
David Tolnay3cb23a92016-10-07 23:02:21 -070063}
64
Alex Crichton62a0a592017-05-22 13:58:53 -070065ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -070066 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070067 pub enum UnOp {
68 /// The `*` operator for dereferencing
Alex Crichtonccbb45d2017-05-23 10:58:24 -070069 Deref(tokens::Star),
Alex Crichton62a0a592017-05-22 13:58:53 -070070 /// The `!` operator for logical inversion
Alex Crichtonccbb45d2017-05-23 10:58:24 -070071 Not(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -070072 /// The `-` operator for negation
Alex Crichtonccbb45d2017-05-23 10:58:24 -070073 Neg(tokens::Sub),
Alex Crichton62a0a592017-05-22 13:58:53 -070074 }
David Tolnay3cb23a92016-10-07 23:02:21 -070075}
76
77#[cfg(feature = "parsing")]
78pub mod parsing {
79 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -040080 use synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -070081 use synom::tokens::*;
David Tolnay3cb23a92016-10-07 23:02:21 -070082
Alex Crichton954046c2017-05-30 21:49:42 -070083 impl BinOp {
Michael Layzell92639a52017-06-01 00:07:44 -040084 named!(pub parse_binop -> Self, alt!(
85 syn!(AndAnd) => { BinOp::And }
86 |
87 syn!(OrOr) => { BinOp::Or }
88 |
89 syn!(Shl) => { BinOp::Shl }
90 |
91 syn!(Shr) => { BinOp::Shr }
92 |
93 syn!(EqEq) => { BinOp::Eq }
94 |
95 syn!(Le) => { BinOp::Le }
96 |
97 syn!(Ne) => { BinOp::Ne }
98 |
99 syn!(Ge) => { BinOp::Ge }
100 |
101 syn!(Add) => { BinOp::Add }
102 |
103 syn!(Sub) => { BinOp::Sub }
104 |
105 syn!(Star) => { BinOp::Mul }
106 |
107 syn!(Div) => { BinOp::Div }
108 |
109 syn!(Rem) => { BinOp::Rem }
110 |
111 syn!(Caret) => { BinOp::BitXor }
112 |
113 syn!(And) => { BinOp::BitAnd }
114 |
115 syn!(Or) => { BinOp::BitOr }
116 |
117 syn!(Lt) => { BinOp::Lt }
118 |
119 syn!(Gt) => { BinOp::Gt }
120 ));
David Tolnay3cb23a92016-10-07 23:02:21 -0700121
Alex Crichton954046c2017-05-30 21:49:42 -0700122 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -0400123 named!(pub parse_assign_op -> Self, alt!(
124 syn!(AddEq) => { BinOp::AddEq }
125 |
126 syn!(SubEq) => { BinOp::SubEq }
127 |
128 syn!(MulEq) => { BinOp::MulEq }
129 |
130 syn!(DivEq) => { BinOp::DivEq }
131 |
132 syn!(RemEq) => { BinOp::RemEq }
133 |
134 syn!(CaretEq) => { BinOp::BitXorEq }
135 |
136 syn!(AndEq) => { BinOp::BitAndEq }
137 |
138 syn!(OrEq) => { BinOp::BitOrEq }
139 |
140 syn!(ShlEq) => { BinOp::ShlEq }
141 |
142 syn!(ShrEq) => { BinOp::ShrEq }
143 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700144 }
David Tolnay438c9052016-10-07 23:24:48 -0700145
Alex Crichton954046c2017-05-30 21:49:42 -0700146 impl Synom for UnOp {
Michael Layzell92639a52017-06-01 00:07:44 -0400147 named!(parse -> Self, alt!(
148 syn!(Star) => { UnOp::Deref }
149 |
150 syn!(Bang) => { UnOp::Not }
151 |
152 syn!(Sub) => { UnOp::Neg }
153 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700154 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700155}
156
157#[cfg(feature = "printing")]
158mod printing {
159 use super::*;
160 use quote::{Tokens, ToTokens};
161
David Tolnay3cb23a92016-10-07 23:02:21 -0700162 impl ToTokens for BinOp {
163 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay3cb23a92016-10-07 23:02:21 -0700164 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700165 BinOp::Add(ref t) => t.to_tokens(tokens),
166 BinOp::Sub(ref t) => t.to_tokens(tokens),
167 BinOp::Mul(ref t) => t.to_tokens(tokens),
168 BinOp::Div(ref t) => t.to_tokens(tokens),
169 BinOp::Rem(ref t) => t.to_tokens(tokens),
170 BinOp::And(ref t) => t.to_tokens(tokens),
171 BinOp::Or(ref t) => t.to_tokens(tokens),
172 BinOp::BitXor(ref t) => t.to_tokens(tokens),
173 BinOp::BitAnd(ref t) => t.to_tokens(tokens),
174 BinOp::BitOr(ref t) => t.to_tokens(tokens),
175 BinOp::Shl(ref t) => t.to_tokens(tokens),
176 BinOp::Shr(ref t) => t.to_tokens(tokens),
177 BinOp::Eq(ref t) => t.to_tokens(tokens),
178 BinOp::Lt(ref t) => t.to_tokens(tokens),
179 BinOp::Le(ref t) => t.to_tokens(tokens),
180 BinOp::Ne(ref t) => t.to_tokens(tokens),
181 BinOp::Ge(ref t) => t.to_tokens(tokens),
182 BinOp::Gt(ref t) => t.to_tokens(tokens),
183 BinOp::AddEq(ref t) => t.to_tokens(tokens),
184 BinOp::SubEq(ref t) => t.to_tokens(tokens),
185 BinOp::MulEq(ref t) => t.to_tokens(tokens),
186 BinOp::DivEq(ref t) => t.to_tokens(tokens),
187 BinOp::RemEq(ref t) => t.to_tokens(tokens),
188 BinOp::BitXorEq(ref t) => t.to_tokens(tokens),
189 BinOp::BitAndEq(ref t) => t.to_tokens(tokens),
190 BinOp::BitOrEq(ref t) => t.to_tokens(tokens),
191 BinOp::ShlEq(ref t) => t.to_tokens(tokens),
192 BinOp::ShrEq(ref t) => t.to_tokens(tokens),
David Tolnay3cb23a92016-10-07 23:02:21 -0700193 }
194 }
195 }
196
197 impl ToTokens for UnOp {
198 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700199 match *self {
200 UnOp::Deref(ref t) => t.to_tokens(tokens),
201 UnOp::Not(ref t) => t.to_tokens(tokens),
202 UnOp::Neg(ref t) => t.to_tokens(tokens),
203 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700204 }
205 }
206}