blob: e0599299a5489af3c202707dbaa60b22a83f23e1 [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::*;
Alex Crichton954046c2017-05-30 21:49:42 -070080 use synom::{TokenTree, IResult, Synom};
81 use synom::tokens::*;
David Tolnay3cb23a92016-10-07 23:02:21 -070082
Alex Crichton954046c2017-05-30 21:49:42 -070083 impl BinOp {
84 pub fn parse_binop(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
85 alt! {
86 input,
87 syn!(AndAnd) => { BinOp::And }
88 |
89 syn!(OrOr) => { BinOp::Or }
90 |
91 syn!(Shl) => { BinOp::Shl }
92 |
93 syn!(Shr) => { BinOp::Shr }
94 |
95 syn!(EqEq) => { BinOp::Eq }
96 |
97 syn!(Le) => { BinOp::Le }
98 |
99 syn!(Ne) => { BinOp::Ne }
100 |
101 syn!(Ge) => { BinOp::Ge }
102 |
103 syn!(Add) => { BinOp::Add }
104 |
105 syn!(Sub) => { BinOp::Sub }
106 |
107 syn!(Star) => { BinOp::Mul }
108 |
109 syn!(Div) => { BinOp::Div }
110 |
111 syn!(Rem) => { BinOp::Rem }
112 |
113 syn!(Caret) => { BinOp::BitXor }
114 |
115 syn!(And) => { BinOp::BitAnd }
116 |
117 syn!(Or) => { BinOp::BitOr }
118 |
119 syn!(Lt) => { BinOp::Lt }
120 |
121 syn!(Gt) => { BinOp::Gt }
122 }
123 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700124
Alex Crichton954046c2017-05-30 21:49:42 -0700125 #[cfg(feature = "full")]
126 pub fn parse_assign_op(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
127 alt! {
128 input,
129 syn!(AddEq) => { BinOp::AddEq }
130 |
131 syn!(SubEq) => { BinOp::SubEq }
132 |
133 syn!(MulEq) => { BinOp::MulEq }
134 |
135 syn!(DivEq) => { BinOp::DivEq }
136 |
137 syn!(RemEq) => { BinOp::RemEq }
138 |
139 syn!(CaretEq) => { BinOp::BitXorEq }
140 |
141 syn!(AndEq) => { BinOp::BitAndEq }
142 |
143 syn!(OrEq) => { BinOp::BitOrEq }
144 |
145 syn!(ShlEq) => { BinOp::ShlEq }
146 |
147 syn!(ShrEq) => { BinOp::ShrEq }
148 }
149 }
150 }
David Tolnay438c9052016-10-07 23:24:48 -0700151
Alex Crichton954046c2017-05-30 21:49:42 -0700152 impl Synom for UnOp {
153 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
154 alt! {
155 input,
156 syn!(Star) => { UnOp::Deref }
157 |
158 syn!(Bang) => { UnOp::Not }
159 |
160 syn!(Sub) => { UnOp::Neg }
161 }
162 }
163 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700164}
165
166#[cfg(feature = "printing")]
167mod printing {
168 use super::*;
169 use quote::{Tokens, ToTokens};
170
David Tolnay3cb23a92016-10-07 23:02:21 -0700171 impl ToTokens for BinOp {
172 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay3cb23a92016-10-07 23:02:21 -0700173 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700174 BinOp::Add(ref t) => t.to_tokens(tokens),
175 BinOp::Sub(ref t) => t.to_tokens(tokens),
176 BinOp::Mul(ref t) => t.to_tokens(tokens),
177 BinOp::Div(ref t) => t.to_tokens(tokens),
178 BinOp::Rem(ref t) => t.to_tokens(tokens),
179 BinOp::And(ref t) => t.to_tokens(tokens),
180 BinOp::Or(ref t) => t.to_tokens(tokens),
181 BinOp::BitXor(ref t) => t.to_tokens(tokens),
182 BinOp::BitAnd(ref t) => t.to_tokens(tokens),
183 BinOp::BitOr(ref t) => t.to_tokens(tokens),
184 BinOp::Shl(ref t) => t.to_tokens(tokens),
185 BinOp::Shr(ref t) => t.to_tokens(tokens),
186 BinOp::Eq(ref t) => t.to_tokens(tokens),
187 BinOp::Lt(ref t) => t.to_tokens(tokens),
188 BinOp::Le(ref t) => t.to_tokens(tokens),
189 BinOp::Ne(ref t) => t.to_tokens(tokens),
190 BinOp::Ge(ref t) => t.to_tokens(tokens),
191 BinOp::Gt(ref t) => t.to_tokens(tokens),
192 BinOp::AddEq(ref t) => t.to_tokens(tokens),
193 BinOp::SubEq(ref t) => t.to_tokens(tokens),
194 BinOp::MulEq(ref t) => t.to_tokens(tokens),
195 BinOp::DivEq(ref t) => t.to_tokens(tokens),
196 BinOp::RemEq(ref t) => t.to_tokens(tokens),
197 BinOp::BitXorEq(ref t) => t.to_tokens(tokens),
198 BinOp::BitAndEq(ref t) => t.to_tokens(tokens),
199 BinOp::BitOrEq(ref t) => t.to_tokens(tokens),
200 BinOp::ShlEq(ref t) => t.to_tokens(tokens),
201 BinOp::ShrEq(ref t) => t.to_tokens(tokens),
David Tolnay3cb23a92016-10-07 23:02:21 -0700202 }
203 }
204 }
205
206 impl ToTokens for UnOp {
207 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700208 match *self {
209 UnOp::Deref(ref t) => t.to_tokens(tokens),
210 UnOp::Not(ref t) => t.to_tokens(tokens),
211 UnOp::Neg(ref t) => t.to_tokens(tokens),
212 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700213 }
214 }
215}