blob: 275657dafa3796f21446d5d5326a12423ba776f8 [file] [log] [blame]
Alex Crichton62a0a592017-05-22 13:58:53 -07001ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -07003 pub enum BinOp {
4 /// The `+` operator (addition)
David Tolnayf8db7ba2017-11-11 22:52:16 -08005 Add(Token![+]),
Alex Crichton62a0a592017-05-22 13:58:53 -07006 /// The `-` operator (subtraction)
David Tolnayf8db7ba2017-11-11 22:52:16 -08007 Sub(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -07008 /// The `*` operator (multiplication)
David Tolnayf8db7ba2017-11-11 22:52:16 -08009 Mul(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// The `/` operator (division)
David Tolnayf8db7ba2017-11-11 22:52:16 -080011 Div(Token![/]),
Alex Crichton62a0a592017-05-22 13:58:53 -070012 /// The `%` operator (modulus)
David Tolnayf8db7ba2017-11-11 22:52:16 -080013 Rem(Token![%]),
Alex Crichton62a0a592017-05-22 13:58:53 -070014 /// The `&&` operator (logical and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080015 And(Token![&&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070016 /// The `||` operator (logical or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080017 Or(Token![||]),
Alex Crichton62a0a592017-05-22 13:58:53 -070018 /// The `^` operator (bitwise xor)
David Tolnayf8db7ba2017-11-11 22:52:16 -080019 BitXor(Token![^]),
Alex Crichton62a0a592017-05-22 13:58:53 -070020 /// The `&` operator (bitwise and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080021 BitAnd(Token![&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070022 /// The `|` operator (bitwise or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080023 BitOr(Token![|]),
Alex Crichton62a0a592017-05-22 13:58:53 -070024 /// The `<<` operator (shift left)
David Tolnayf8db7ba2017-11-11 22:52:16 -080025 Shl(Token![<<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// The `>>` operator (shift right)
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 Shr(Token![>>]),
Alex Crichton62a0a592017-05-22 13:58:53 -070028 /// The `==` operator (equality)
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 Eq(Token![==]),
Alex Crichton62a0a592017-05-22 13:58:53 -070030 /// The `<` operator (less than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080031 Lt(Token![<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070032 /// The `<=` operator (less than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080033 Le(Token![<=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070034 /// The `!=` operator (not equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 Ne(Token![!=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// The `>=` operator (greater than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 Ge(Token![>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070038 /// The `>` operator (greater than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080039 Gt(Token![>]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070040 /// The `+=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080041 AddEq(Token![+=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070042 /// The `-=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080043 SubEq(Token![-=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070044 /// The `*=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 MulEq(Token![*=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070046 /// The `/=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080047 DivEq(Token![/=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070048 /// The `%=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 RemEq(Token![%=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070050 /// The `^=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 BitXorEq(Token![^=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 /// The `&=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 BitAndEq(Token![&=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070054 /// The `|=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080055 BitOrEq(Token![|=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070056 /// The `<<=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080057 ShlEq(Token![<<=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070058 /// The `>>=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080059 ShrEq(Token![>>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070060 }
David Tolnay3cb23a92016-10-07 23:02:21 -070061}
62
Alex Crichton62a0a592017-05-22 13:58:53 -070063ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -070064 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070065 pub enum UnOp {
66 /// The `*` operator for dereferencing
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 Deref(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070068 /// The `!` operator for logical inversion
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 Not(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -070070 /// The `-` operator for negation
David Tolnayf8db7ba2017-11-11 22:52:16 -080071 Neg(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -070072 }
David Tolnay3cb23a92016-10-07 23:02:21 -070073}
74
75#[cfg(feature = "parsing")]
76pub mod parsing {
77 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -040078 use synom::Synom;
David Tolnay3cb23a92016-10-07 23:02:21 -070079
Alex Crichton954046c2017-05-30 21:49:42 -070080 impl BinOp {
Michael Layzell92639a52017-06-01 00:07:44 -040081 named!(pub parse_binop -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -080082 punct!(&&) => { BinOp::And }
Michael Layzell92639a52017-06-01 00:07:44 -040083 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080084 punct!(||) => { BinOp::Or }
Michael Layzell92639a52017-06-01 00:07:44 -040085 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080086 punct!(<<) => { BinOp::Shl }
Michael Layzell92639a52017-06-01 00:07:44 -040087 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080088 punct!(>>) => { BinOp::Shr }
Michael Layzell92639a52017-06-01 00:07:44 -040089 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080090 punct!(==) => { BinOp::Eq }
Michael Layzell92639a52017-06-01 00:07:44 -040091 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080092 punct!(<=) => { BinOp::Le }
Michael Layzell92639a52017-06-01 00:07:44 -040093 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080094 punct!(!=) => { BinOp::Ne }
Michael Layzell92639a52017-06-01 00:07:44 -040095 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080096 punct!(>=) => { BinOp::Ge }
Michael Layzell92639a52017-06-01 00:07:44 -040097 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080098 punct!(+) => { BinOp::Add }
Michael Layzell92639a52017-06-01 00:07:44 -040099 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800100 punct!(-) => { BinOp::Sub }
Michael Layzell92639a52017-06-01 00:07:44 -0400101 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800102 punct!(*) => { BinOp::Mul }
Michael Layzell92639a52017-06-01 00:07:44 -0400103 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800104 punct!(/) => { BinOp::Div }
Michael Layzell92639a52017-06-01 00:07:44 -0400105 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800106 punct!(%) => { BinOp::Rem }
Michael Layzell92639a52017-06-01 00:07:44 -0400107 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 punct!(^) => { BinOp::BitXor }
Michael Layzell92639a52017-06-01 00:07:44 -0400109 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 punct!(&) => { BinOp::BitAnd }
Michael Layzell92639a52017-06-01 00:07:44 -0400111 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 punct!(|) => { BinOp::BitOr }
Michael Layzell92639a52017-06-01 00:07:44 -0400113 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800114 punct!(<) => { BinOp::Lt }
Michael Layzell92639a52017-06-01 00:07:44 -0400115 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800116 punct!(>) => { BinOp::Gt }
Michael Layzell92639a52017-06-01 00:07:44 -0400117 ));
David Tolnay3cb23a92016-10-07 23:02:21 -0700118
Alex Crichton954046c2017-05-30 21:49:42 -0700119 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -0400120 named!(pub parse_assign_op -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800121 punct!(+=) => { BinOp::AddEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400122 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800123 punct!(-=) => { BinOp::SubEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400124 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800125 punct!(*=) => { BinOp::MulEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400126 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800127 punct!(/=) => { BinOp::DivEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400128 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800129 punct!(%=) => { BinOp::RemEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400130 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 punct!(^=) => { BinOp::BitXorEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400132 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800133 punct!(&=) => { BinOp::BitAndEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400134 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800135 punct!(|=) => { BinOp::BitOrEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400136 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800137 punct!(<<=) => { BinOp::ShlEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400138 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800139 punct!(>>=) => { BinOp::ShrEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400140 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700141 }
David Tolnay438c9052016-10-07 23:24:48 -0700142
Alex Crichton954046c2017-05-30 21:49:42 -0700143 impl Synom for UnOp {
Michael Layzell92639a52017-06-01 00:07:44 -0400144 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800145 punct!(*) => { UnOp::Deref }
Michael Layzell92639a52017-06-01 00:07:44 -0400146 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800147 punct!(!) => { UnOp::Not }
Michael Layzell92639a52017-06-01 00:07:44 -0400148 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800149 punct!(-) => { UnOp::Neg }
Michael Layzell92639a52017-06-01 00:07:44 -0400150 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800151
152 fn description() -> Option<&'static str> {
153 Some("unary operator: `*`, `!`, or `-`")
154 }
Alex Crichton954046c2017-05-30 21:49:42 -0700155 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700156}
157
158#[cfg(feature = "printing")]
159mod printing {
160 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500161 use quote::{ToTokens, Tokens};
David Tolnay3cb23a92016-10-07 23:02:21 -0700162
David Tolnay3cb23a92016-10-07 23:02:21 -0700163 impl ToTokens for BinOp {
164 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay3cb23a92016-10-07 23:02:21 -0700165 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700166 BinOp::Add(ref t) => t.to_tokens(tokens),
167 BinOp::Sub(ref t) => t.to_tokens(tokens),
168 BinOp::Mul(ref t) => t.to_tokens(tokens),
169 BinOp::Div(ref t) => t.to_tokens(tokens),
170 BinOp::Rem(ref t) => t.to_tokens(tokens),
171 BinOp::And(ref t) => t.to_tokens(tokens),
172 BinOp::Or(ref t) => t.to_tokens(tokens),
173 BinOp::BitXor(ref t) => t.to_tokens(tokens),
174 BinOp::BitAnd(ref t) => t.to_tokens(tokens),
175 BinOp::BitOr(ref t) => t.to_tokens(tokens),
176 BinOp::Shl(ref t) => t.to_tokens(tokens),
177 BinOp::Shr(ref t) => t.to_tokens(tokens),
178 BinOp::Eq(ref t) => t.to_tokens(tokens),
179 BinOp::Lt(ref t) => t.to_tokens(tokens),
180 BinOp::Le(ref t) => t.to_tokens(tokens),
181 BinOp::Ne(ref t) => t.to_tokens(tokens),
182 BinOp::Ge(ref t) => t.to_tokens(tokens),
183 BinOp::Gt(ref t) => t.to_tokens(tokens),
184 BinOp::AddEq(ref t) => t.to_tokens(tokens),
185 BinOp::SubEq(ref t) => t.to_tokens(tokens),
186 BinOp::MulEq(ref t) => t.to_tokens(tokens),
187 BinOp::DivEq(ref t) => t.to_tokens(tokens),
188 BinOp::RemEq(ref t) => t.to_tokens(tokens),
189 BinOp::BitXorEq(ref t) => t.to_tokens(tokens),
190 BinOp::BitAndEq(ref t) => t.to_tokens(tokens),
191 BinOp::BitOrEq(ref t) => t.to_tokens(tokens),
192 BinOp::ShlEq(ref t) => t.to_tokens(tokens),
193 BinOp::ShrEq(ref t) => t.to_tokens(tokens),
David Tolnay3cb23a92016-10-07 23:02:21 -0700194 }
195 }
196 }
197
198 impl ToTokens for UnOp {
199 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700200 match *self {
201 UnOp::Deref(ref t) => t.to_tokens(tokens),
202 UnOp::Not(ref t) => t.to_tokens(tokens),
203 UnOp::Neg(ref t) => t.to_tokens(tokens),
204 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700205 }
206 }
207}