blob: b9a8ef7f908ac81d5a016a6fa66c95ca39f03202 [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: `+`, `+=`, `&`.
David Tolnay461d98e2018-01-07 11:07:19 -080011 ///
12 /// *This type is available if Syn is built with the `"derive"` or `"full"`
13 /// feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -070014 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070015 pub enum BinOp {
16 /// The `+` operator (addition)
David Tolnayf8db7ba2017-11-11 22:52:16 -080017 Add(Token![+]),
Alex Crichton62a0a592017-05-22 13:58:53 -070018 /// The `-` operator (subtraction)
David Tolnayf8db7ba2017-11-11 22:52:16 -080019 Sub(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -070020 /// The `*` operator (multiplication)
David Tolnayf8db7ba2017-11-11 22:52:16 -080021 Mul(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070022 /// The `/` operator (division)
David Tolnayf8db7ba2017-11-11 22:52:16 -080023 Div(Token![/]),
Alex Crichton62a0a592017-05-22 13:58:53 -070024 /// The `%` operator (modulus)
David Tolnayf8db7ba2017-11-11 22:52:16 -080025 Rem(Token![%]),
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// The `&&` operator (logical and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 And(Token![&&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070028 /// The `||` operator (logical or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 Or(Token![||]),
Alex Crichton62a0a592017-05-22 13:58:53 -070030 /// The `^` operator (bitwise xor)
David Tolnayf8db7ba2017-11-11 22:52:16 -080031 BitXor(Token![^]),
Alex Crichton62a0a592017-05-22 13:58:53 -070032 /// The `&` operator (bitwise and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080033 BitAnd(Token![&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070034 /// The `|` operator (bitwise or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 BitOr(Token![|]),
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// The `<<` operator (shift left)
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 Shl(Token![<<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070038 /// The `>>` operator (shift right)
David Tolnayf8db7ba2017-11-11 22:52:16 -080039 Shr(Token![>>]),
Alex Crichton62a0a592017-05-22 13:58:53 -070040 /// The `==` operator (equality)
David Tolnayf8db7ba2017-11-11 22:52:16 -080041 Eq(Token![==]),
Alex Crichton62a0a592017-05-22 13:58:53 -070042 /// The `<` operator (less than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080043 Lt(Token![<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070044 /// The `<=` operator (less than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 Le(Token![<=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070046 /// The `!=` operator (not equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080047 Ne(Token![!=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070048 /// The `>=` operator (greater than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 Ge(Token![>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070050 /// The `>` operator (greater than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 Gt(Token![>]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 /// The `+=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 AddEq(Token![+=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070054 /// The `-=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080055 SubEq(Token![-=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070056 /// The `*=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080057 MulEq(Token![*=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070058 /// The `/=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080059 DivEq(Token![/=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070060 /// The `%=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080061 RemEq(Token![%=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070062 /// The `^=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080063 BitXorEq(Token![^=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070064 /// The `&=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 BitAndEq(Token![&=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 /// The `|=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 BitOrEq(Token![|=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070068 /// The `<<=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080069 ShlEq(Token![<<=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070070 /// The `>>=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080071 ShrEq(Token![>>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070072 }
David Tolnay3cb23a92016-10-07 23:02:21 -070073}
74
Alex Crichton62a0a592017-05-22 13:58:53 -070075ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -080076 /// A unary operator: `*`, `!`, `-`.
David Tolnay461d98e2018-01-07 11:07:19 -080077 ///
78 /// *This type is available if Syn is built with the `"derive"` or `"full"`
79 /// feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -070080 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070081 pub enum UnOp {
82 /// The `*` operator for dereferencing
David Tolnayf8db7ba2017-11-11 22:52:16 -080083 Deref(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070084 /// The `!` operator for logical inversion
David Tolnayf8db7ba2017-11-11 22:52:16 -080085 Not(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -070086 /// The `-` operator for negation
David Tolnayf8db7ba2017-11-11 22:52:16 -080087 Neg(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -070088 }
David Tolnay3cb23a92016-10-07 23:02:21 -070089}
90
91#[cfg(feature = "parsing")]
92pub mod parsing {
93 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -040094 use synom::Synom;
David Tolnay3cb23a92016-10-07 23:02:21 -070095
Alex Crichton954046c2017-05-30 21:49:42 -070096 impl BinOp {
Michael Layzell92639a52017-06-01 00:07:44 -040097 named!(pub parse_binop -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -080098 punct!(&&) => { BinOp::And }
Michael Layzell92639a52017-06-01 00:07:44 -040099 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800100 punct!(||) => { BinOp::Or }
Michael Layzell92639a52017-06-01 00:07:44 -0400101 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800102 punct!(<<) => { BinOp::Shl }
Michael Layzell92639a52017-06-01 00:07:44 -0400103 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800104 punct!(>>) => { BinOp::Shr }
Michael Layzell92639a52017-06-01 00:07:44 -0400105 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800106 punct!(==) => { BinOp::Eq }
Michael Layzell92639a52017-06-01 00:07:44 -0400107 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 punct!(<=) => { BinOp::Le }
Michael Layzell92639a52017-06-01 00:07:44 -0400109 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800110 punct!(!=) => { BinOp::Ne }
Michael Layzell92639a52017-06-01 00:07:44 -0400111 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 punct!(>=) => { BinOp::Ge }
Michael Layzell92639a52017-06-01 00:07:44 -0400113 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800114 punct!(+) => { BinOp::Add }
Michael Layzell92639a52017-06-01 00:07:44 -0400115 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800116 punct!(-) => { BinOp::Sub }
Michael Layzell92639a52017-06-01 00:07:44 -0400117 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800118 punct!(*) => { BinOp::Mul }
Michael Layzell92639a52017-06-01 00:07:44 -0400119 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800120 punct!(/) => { BinOp::Div }
Michael Layzell92639a52017-06-01 00:07:44 -0400121 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 punct!(%) => { BinOp::Rem }
Michael Layzell92639a52017-06-01 00:07:44 -0400123 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800124 punct!(^) => { BinOp::BitXor }
Michael Layzell92639a52017-06-01 00:07:44 -0400125 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800126 punct!(&) => { BinOp::BitAnd }
Michael Layzell92639a52017-06-01 00:07:44 -0400127 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800128 punct!(|) => { BinOp::BitOr }
Michael Layzell92639a52017-06-01 00:07:44 -0400129 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800130 punct!(<) => { BinOp::Lt }
Michael Layzell92639a52017-06-01 00:07:44 -0400131 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800132 punct!(>) => { BinOp::Gt }
Michael Layzell92639a52017-06-01 00:07:44 -0400133 ));
David Tolnay3cb23a92016-10-07 23:02:21 -0700134
Alex Crichton954046c2017-05-30 21:49:42 -0700135 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -0400136 named!(pub parse_assign_op -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800137 punct!(+=) => { BinOp::AddEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400138 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800139 punct!(-=) => { BinOp::SubEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400140 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800141 punct!(*=) => { BinOp::MulEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400142 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800143 punct!(/=) => { BinOp::DivEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400144 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800145 punct!(%=) => { BinOp::RemEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400146 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800147 punct!(^=) => { BinOp::BitXorEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400148 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800149 punct!(&=) => { BinOp::BitAndEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400150 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800151 punct!(|=) => { BinOp::BitOrEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400152 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800153 punct!(<<=) => { BinOp::ShlEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400154 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800155 punct!(>>=) => { BinOp::ShrEq }
Michael Layzell92639a52017-06-01 00:07:44 -0400156 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700157 }
David Tolnay438c9052016-10-07 23:24:48 -0700158
Alex Crichton954046c2017-05-30 21:49:42 -0700159 impl Synom for UnOp {
Michael Layzell92639a52017-06-01 00:07:44 -0400160 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800161 punct!(*) => { UnOp::Deref }
Michael Layzell92639a52017-06-01 00:07:44 -0400162 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800163 punct!(!) => { UnOp::Not }
Michael Layzell92639a52017-06-01 00:07:44 -0400164 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800165 punct!(-) => { UnOp::Neg }
Michael Layzell92639a52017-06-01 00:07:44 -0400166 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800167
168 fn description() -> Option<&'static str> {
169 Some("unary operator: `*`, `!`, or `-`")
170 }
Alex Crichton954046c2017-05-30 21:49:42 -0700171 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700172}
173
174#[cfg(feature = "printing")]
175mod printing {
176 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500177 use quote::{ToTokens, Tokens};
David Tolnay3cb23a92016-10-07 23:02:21 -0700178
David Tolnay3cb23a92016-10-07 23:02:21 -0700179 impl ToTokens for BinOp {
180 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay3cb23a92016-10-07 23:02:21 -0700181 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700182 BinOp::Add(ref t) => t.to_tokens(tokens),
183 BinOp::Sub(ref t) => t.to_tokens(tokens),
184 BinOp::Mul(ref t) => t.to_tokens(tokens),
185 BinOp::Div(ref t) => t.to_tokens(tokens),
186 BinOp::Rem(ref t) => t.to_tokens(tokens),
187 BinOp::And(ref t) => t.to_tokens(tokens),
188 BinOp::Or(ref t) => t.to_tokens(tokens),
189 BinOp::BitXor(ref t) => t.to_tokens(tokens),
190 BinOp::BitAnd(ref t) => t.to_tokens(tokens),
191 BinOp::BitOr(ref t) => t.to_tokens(tokens),
192 BinOp::Shl(ref t) => t.to_tokens(tokens),
193 BinOp::Shr(ref t) => t.to_tokens(tokens),
194 BinOp::Eq(ref t) => t.to_tokens(tokens),
195 BinOp::Lt(ref t) => t.to_tokens(tokens),
196 BinOp::Le(ref t) => t.to_tokens(tokens),
197 BinOp::Ne(ref t) => t.to_tokens(tokens),
198 BinOp::Ge(ref t) => t.to_tokens(tokens),
199 BinOp::Gt(ref t) => t.to_tokens(tokens),
200 BinOp::AddEq(ref t) => t.to_tokens(tokens),
201 BinOp::SubEq(ref t) => t.to_tokens(tokens),
202 BinOp::MulEq(ref t) => t.to_tokens(tokens),
203 BinOp::DivEq(ref t) => t.to_tokens(tokens),
204 BinOp::RemEq(ref t) => t.to_tokens(tokens),
205 BinOp::BitXorEq(ref t) => t.to_tokens(tokens),
206 BinOp::BitAndEq(ref t) => t.to_tokens(tokens),
207 BinOp::BitOrEq(ref t) => t.to_tokens(tokens),
208 BinOp::ShlEq(ref t) => t.to_tokens(tokens),
209 BinOp::ShrEq(ref t) => t.to_tokens(tokens),
David Tolnay3cb23a92016-10-07 23:02:21 -0700210 }
211 }
212 }
213
214 impl ToTokens for UnOp {
215 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700216 match *self {
217 UnOp::Deref(ref t) => t.to_tokens(tokens),
218 UnOp::Not(ref t) => t.to_tokens(tokens),
219 UnOp::Neg(ref t) => t.to_tokens(tokens),
220 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700221 }
222 }
223}