blob: c0873a2b275cf75e5114e8d243b34995e93c7d6e [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
Alex Crichton62a0a592017-05-22 13:58:53 -07009ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -080010 /// A binary operator: `+`, `+=`, `&`.
Alex Crichton2e0229c2017-05-23 09:34:50 -070011 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070012 pub enum BinOp {
13 /// The `+` operator (addition)
David Tolnayf8db7ba2017-11-11 22:52:16 -080014 Add(Token![+]),
Alex Crichton62a0a592017-05-22 13:58:53 -070015 /// The `-` operator (subtraction)
David Tolnayf8db7ba2017-11-11 22:52:16 -080016 Sub(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -070017 /// The `*` operator (multiplication)
David Tolnayf8db7ba2017-11-11 22:52:16 -080018 Mul(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070019 /// The `/` operator (division)
David Tolnayf8db7ba2017-11-11 22:52:16 -080020 Div(Token![/]),
Alex Crichton62a0a592017-05-22 13:58:53 -070021 /// The `%` operator (modulus)
David Tolnayf8db7ba2017-11-11 22:52:16 -080022 Rem(Token![%]),
Alex Crichton62a0a592017-05-22 13:58:53 -070023 /// The `&&` operator (logical and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080024 And(Token![&&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070025 /// The `||` operator (logical or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080026 Or(Token![||]),
Alex Crichton62a0a592017-05-22 13:58:53 -070027 /// The `^` operator (bitwise xor)
David Tolnayf8db7ba2017-11-11 22:52:16 -080028 BitXor(Token![^]),
Alex Crichton62a0a592017-05-22 13:58:53 -070029 /// The `&` operator (bitwise and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080030 BitAnd(Token![&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070031 /// The `|` operator (bitwise or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080032 BitOr(Token![|]),
Alex Crichton62a0a592017-05-22 13:58:53 -070033 /// The `<<` operator (shift left)
David Tolnayf8db7ba2017-11-11 22:52:16 -080034 Shl(Token![<<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070035 /// The `>>` operator (shift right)
David Tolnayf8db7ba2017-11-11 22:52:16 -080036 Shr(Token![>>]),
Alex Crichton62a0a592017-05-22 13:58:53 -070037 /// The `==` operator (equality)
David Tolnayf8db7ba2017-11-11 22:52:16 -080038 Eq(Token![==]),
Alex Crichton62a0a592017-05-22 13:58:53 -070039 /// The `<` operator (less than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080040 Lt(Token![<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070041 /// The `<=` operator (less than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080042 Le(Token![<=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070043 /// The `!=` operator (not equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080044 Ne(Token![!=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070045 /// The `>=` operator (greater than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080046 Ge(Token![>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070047 /// The `>` operator (greater than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080048 Gt(Token![>]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070049 /// The `+=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080050 AddEq(Token![+=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070051 /// The `-=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 SubEq(Token![-=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070053 /// The `*=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 MulEq(Token![*=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070055 /// The `/=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 DivEq(Token![/=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070057 /// The `%=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080058 RemEq(Token![%=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070059 /// The `^=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080060 BitXorEq(Token![^=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070061 /// The `&=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080062 BitAndEq(Token![&=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070063 /// The `|=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080064 BitOrEq(Token![|=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 /// The `<<=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080066 ShlEq(Token![<<=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070067 /// The `>>=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080068 ShrEq(Token![>>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070069 }
David Tolnay3cb23a92016-10-07 23:02:21 -070070}
71
Alex Crichton62a0a592017-05-22 13:58:53 -070072ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -080073 /// A unary operator: `*`, `!`, `-`.
Alex Crichton2e0229c2017-05-23 09:34:50 -070074 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub enum UnOp {
76 /// The `*` operator for dereferencing
David Tolnayf8db7ba2017-11-11 22:52:16 -080077 Deref(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070078 /// The `!` operator for logical inversion
David Tolnayf8db7ba2017-11-11 22:52:16 -080079 Not(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -070080 /// The `-` operator for negation
David Tolnayf8db7ba2017-11-11 22:52:16 -080081 Neg(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -070082 }
David Tolnay3cb23a92016-10-07 23:02:21 -070083}
84
85#[cfg(feature = "parsing")]
86pub mod parsing {
87 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -040088 use synom::Synom;
David Tolnay3cb23a92016-10-07 23:02:21 -070089
Alex Crichton954046c2017-05-30 21:49:42 -070090 impl BinOp {
Michael Layzell92639a52017-06-01 00:07:44 -040091 named!(pub parse_binop -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -080092 punct!(&&) => { BinOp::And }
Michael Layzell92639a52017-06-01 00:07:44 -040093 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080094 punct!(||) => { BinOp::Or }
Michael Layzell92639a52017-06-01 00:07:44 -040095 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080096 punct!(<<) => { BinOp::Shl }
Michael Layzell92639a52017-06-01 00:07:44 -040097 |
David Tolnayf8db7ba2017-11-11 22:52:16 -080098 punct!(>>) => { BinOp::Shr }
Michael Layzell92639a52017-06-01 00:07:44 -040099 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800100 punct!(==) => { BinOp::Eq }
Michael Layzell92639a52017-06-01 00:07:44 -0400101 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800102 punct!(<=) => { BinOp::Le }
Michael Layzell92639a52017-06-01 00:07:44 -0400103 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800104 punct!(!=) => { BinOp::Ne }
Michael Layzell92639a52017-06-01 00:07:44 -0400105 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800106 punct!(>=) => { BinOp::Ge }
Michael Layzell92639a52017-06-01 00:07:44 -0400107 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 punct!(+) => { BinOp::Add }
Michael Layzell92639a52017-06-01 00:07:44 -0400109 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 punct!(-) => { BinOp::Sub }
Michael Layzell92639a52017-06-01 00:07:44 -0400111 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 punct!(*) => { BinOp::Mul }
Michael Layzell92639a52017-06-01 00:07:44 -0400113 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800114 punct!(/) => { BinOp::Div }
Michael Layzell92639a52017-06-01 00:07:44 -0400115 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800116 punct!(%) => { BinOp::Rem }
Michael Layzell92639a52017-06-01 00:07:44 -0400117 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800118 punct!(^) => { BinOp::BitXor }
Michael Layzell92639a52017-06-01 00:07:44 -0400119 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800120 punct!(&) => { BinOp::BitAnd }
Michael Layzell92639a52017-06-01 00:07:44 -0400121 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 punct!(|) => { BinOp::BitOr }
Michael Layzell92639a52017-06-01 00:07:44 -0400123 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800124 punct!(<) => { BinOp::Lt }
Michael Layzell92639a52017-06-01 00:07:44 -0400125 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800126 punct!(>) => { BinOp::Gt }
Michael Layzell92639a52017-06-01 00:07:44 -0400127 ));
David Tolnay3cb23a92016-10-07 23:02:21 -0700128
Alex Crichton954046c2017-05-30 21:49:42 -0700129 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -0400130 named!(pub parse_assign_op -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 punct!(+=) => { BinOp::AddEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400132 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800133 punct!(-=) => { BinOp::SubEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400134 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800135 punct!(*=) => { BinOp::MulEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400136 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800137 punct!(/=) => { BinOp::DivEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400138 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800139 punct!(%=) => { BinOp::RemEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400140 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800141 punct!(^=) => { BinOp::BitXorEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400142 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800143 punct!(&=) => { BinOp::BitAndEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400144 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800145 punct!(|=) => { BinOp::BitOrEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400146 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800147 punct!(<<=) => { BinOp::ShlEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400148 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800149 punct!(>>=) => { BinOp::ShrEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400150 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700151 }
David Tolnay438c9052016-10-07 23:24:48 -0700152
Alex Crichton954046c2017-05-30 21:49:42 -0700153 impl Synom for UnOp {
Michael Layzell92639a52017-06-01 00:07:44 -0400154 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800155 punct!(*) => { UnOp::Deref }
Michael Layzell92639a52017-06-01 00:07:44 -0400156 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800157 punct!(!) => { UnOp::Not }
Michael Layzell92639a52017-06-01 00:07:44 -0400158 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800159 punct!(-) => { UnOp::Neg }
Michael Layzell92639a52017-06-01 00:07:44 -0400160 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800161
162 fn description() -> Option<&'static str> {
163 Some("unary operator: `*`, `!`, or `-`")
164 }
Alex Crichton954046c2017-05-30 21:49:42 -0700165 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700166}
167
168#[cfg(feature = "printing")]
169mod printing {
170 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500171 use quote::{ToTokens, Tokens};
David Tolnay3cb23a92016-10-07 23:02:21 -0700172
David Tolnay3cb23a92016-10-07 23:02:21 -0700173 impl ToTokens for BinOp {
174 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay3cb23a92016-10-07 23:02:21 -0700175 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700176 BinOp::Add(ref t) => t.to_tokens(tokens),
177 BinOp::Sub(ref t) => t.to_tokens(tokens),
178 BinOp::Mul(ref t) => t.to_tokens(tokens),
179 BinOp::Div(ref t) => t.to_tokens(tokens),
180 BinOp::Rem(ref t) => t.to_tokens(tokens),
181 BinOp::And(ref t) => t.to_tokens(tokens),
182 BinOp::Or(ref t) => t.to_tokens(tokens),
183 BinOp::BitXor(ref t) => t.to_tokens(tokens),
184 BinOp::BitAnd(ref t) => t.to_tokens(tokens),
185 BinOp::BitOr(ref t) => t.to_tokens(tokens),
186 BinOp::Shl(ref t) => t.to_tokens(tokens),
187 BinOp::Shr(ref t) => t.to_tokens(tokens),
188 BinOp::Eq(ref t) => t.to_tokens(tokens),
189 BinOp::Lt(ref t) => t.to_tokens(tokens),
190 BinOp::Le(ref t) => t.to_tokens(tokens),
191 BinOp::Ne(ref t) => t.to_tokens(tokens),
192 BinOp::Ge(ref t) => t.to_tokens(tokens),
193 BinOp::Gt(ref t) => t.to_tokens(tokens),
194 BinOp::AddEq(ref t) => t.to_tokens(tokens),
195 BinOp::SubEq(ref t) => t.to_tokens(tokens),
196 BinOp::MulEq(ref t) => t.to_tokens(tokens),
197 BinOp::DivEq(ref t) => t.to_tokens(tokens),
198 BinOp::RemEq(ref t) => t.to_tokens(tokens),
199 BinOp::BitXorEq(ref t) => t.to_tokens(tokens),
200 BinOp::BitAndEq(ref t) => t.to_tokens(tokens),
201 BinOp::BitOrEq(ref t) => t.to_tokens(tokens),
202 BinOp::ShlEq(ref t) => t.to_tokens(tokens),
203 BinOp::ShrEq(ref t) => t.to_tokens(tokens),
David Tolnay3cb23a92016-10-07 23:02:21 -0700204 }
205 }
206 }
207
208 impl ToTokens for UnOp {
209 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700210 match *self {
211 UnOp::Deref(ref t) => t.to_tokens(tokens),
212 UnOp::Not(ref t) => t.to_tokens(tokens),
213 UnOp::Neg(ref t) => t.to_tokens(tokens),
214 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700215 }
216 }
217}