]>
Commit | Line | Data |
---|---|---|
83c7162d XL |
1 | // 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 | ||
9 | ast_enum! { | |
10 | /// A binary operator: `+`, `+=`, `&`. | |
11 | /// | |
12 | /// *This type is available if Syn is built with the `"derive"` or `"full"` | |
13 | /// feature.* | |
14 | #[cfg_attr(feature = "clone-impls", derive(Copy))] | |
15 | pub enum BinOp { | |
16 | /// The `+` operator (addition) | |
17 | Add(Token![+]), | |
18 | /// The `-` operator (subtraction) | |
19 | Sub(Token![-]), | |
20 | /// The `*` operator (multiplication) | |
21 | Mul(Token![*]), | |
22 | /// The `/` operator (division) | |
23 | Div(Token![/]), | |
24 | /// The `%` operator (modulus) | |
25 | Rem(Token![%]), | |
26 | /// The `&&` operator (logical and) | |
27 | And(Token![&&]), | |
28 | /// The `||` operator (logical or) | |
29 | Or(Token![||]), | |
30 | /// The `^` operator (bitwise xor) | |
31 | BitXor(Token![^]), | |
32 | /// The `&` operator (bitwise and) | |
33 | BitAnd(Token![&]), | |
34 | /// The `|` operator (bitwise or) | |
35 | BitOr(Token![|]), | |
36 | /// The `<<` operator (shift left) | |
37 | Shl(Token![<<]), | |
38 | /// The `>>` operator (shift right) | |
39 | Shr(Token![>>]), | |
40 | /// The `==` operator (equality) | |
41 | Eq(Token![==]), | |
42 | /// The `<` operator (less than) | |
43 | Lt(Token![<]), | |
44 | /// The `<=` operator (less than or equal to) | |
45 | Le(Token![<=]), | |
46 | /// The `!=` operator (not equal to) | |
47 | Ne(Token![!=]), | |
48 | /// The `>=` operator (greater than or equal to) | |
49 | Ge(Token![>=]), | |
50 | /// The `>` operator (greater than) | |
51 | Gt(Token![>]), | |
52 | /// The `+=` operator | |
53 | AddEq(Token![+=]), | |
54 | /// The `-=` operator | |
55 | SubEq(Token![-=]), | |
56 | /// The `*=` operator | |
57 | MulEq(Token![*=]), | |
58 | /// The `/=` operator | |
59 | DivEq(Token![/=]), | |
60 | /// The `%=` operator | |
61 | RemEq(Token![%=]), | |
62 | /// The `^=` operator | |
63 | BitXorEq(Token![^=]), | |
64 | /// The `&=` operator | |
65 | BitAndEq(Token![&=]), | |
66 | /// The `|=` operator | |
67 | BitOrEq(Token![|=]), | |
68 | /// The `<<=` operator | |
69 | ShlEq(Token![<<=]), | |
70 | /// The `>>=` operator | |
71 | ShrEq(Token![>>=]), | |
72 | } | |
73 | } | |
74 | ||
75 | ast_enum! { | |
76 | /// A unary operator: `*`, `!`, `-`. | |
77 | /// | |
78 | /// *This type is available if Syn is built with the `"derive"` or `"full"` | |
79 | /// feature.* | |
80 | #[cfg_attr(feature = "clone-impls", derive(Copy))] | |
81 | pub enum UnOp { | |
82 | /// The `*` operator for dereferencing | |
83 | Deref(Token![*]), | |
84 | /// The `!` operator for logical inversion | |
85 | Not(Token![!]), | |
86 | /// The `-` operator for negation | |
87 | Neg(Token![-]), | |
88 | } | |
89 | } | |
90 | ||
91 | #[cfg(feature = "parsing")] | |
92 | pub mod parsing { | |
93 | use super::*; | |
94 | use synom::Synom; | |
95 | ||
96 | impl BinOp { | |
97 | named!(pub parse_binop -> Self, alt!( | |
98 | punct!(&&) => { BinOp::And } | |
99 | | | |
100 | punct!(||) => { BinOp::Or } | |
101 | | | |
102 | punct!(<<) => { BinOp::Shl } | |
103 | | | |
104 | punct!(>>) => { BinOp::Shr } | |
105 | | | |
106 | punct!(==) => { BinOp::Eq } | |
107 | | | |
108 | punct!(<=) => { BinOp::Le } | |
109 | | | |
110 | punct!(!=) => { BinOp::Ne } | |
111 | | | |
112 | punct!(>=) => { BinOp::Ge } | |
113 | | | |
114 | punct!(+) => { BinOp::Add } | |
115 | | | |
116 | punct!(-) => { BinOp::Sub } | |
117 | | | |
118 | punct!(*) => { BinOp::Mul } | |
119 | | | |
120 | punct!(/) => { BinOp::Div } | |
121 | | | |
122 | punct!(%) => { BinOp::Rem } | |
123 | | | |
124 | punct!(^) => { BinOp::BitXor } | |
125 | | | |
126 | punct!(&) => { BinOp::BitAnd } | |
127 | | | |
128 | punct!(|) => { BinOp::BitOr } | |
129 | | | |
130 | punct!(<) => { BinOp::Lt } | |
131 | | | |
132 | punct!(>) => { BinOp::Gt } | |
133 | )); | |
134 | ||
135 | #[cfg(feature = "full")] | |
136 | named!(pub parse_assign_op -> Self, alt!( | |
137 | punct!(+=) => { BinOp::AddEq } | |
138 | | | |
139 | punct!(-=) => { BinOp::SubEq } | |
140 | | | |
141 | punct!(*=) => { BinOp::MulEq } | |
142 | | | |
143 | punct!(/=) => { BinOp::DivEq } | |
144 | | | |
145 | punct!(%=) => { BinOp::RemEq } | |
146 | | | |
147 | punct!(^=) => { BinOp::BitXorEq } | |
148 | | | |
149 | punct!(&=) => { BinOp::BitAndEq } | |
150 | | | |
151 | punct!(|=) => { BinOp::BitOrEq } | |
152 | | | |
153 | punct!(<<=) => { BinOp::ShlEq } | |
154 | | | |
155 | punct!(>>=) => { BinOp::ShrEq } | |
156 | )); | |
157 | } | |
158 | ||
159 | impl Synom for UnOp { | |
160 | named!(parse -> Self, alt!( | |
161 | punct!(*) => { UnOp::Deref } | |
162 | | | |
163 | punct!(!) => { UnOp::Not } | |
164 | | | |
165 | punct!(-) => { UnOp::Neg } | |
166 | )); | |
167 | ||
168 | fn description() -> Option<&'static str> { | |
169 | Some("unary operator: `*`, `!`, or `-`") | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
174 | #[cfg(feature = "printing")] | |
175 | mod printing { | |
176 | use super::*; | |
177 | use quote::{ToTokens, Tokens}; | |
178 | ||
179 | impl ToTokens for BinOp { | |
180 | fn to_tokens(&self, tokens: &mut Tokens) { | |
181 | match *self { | |
182 | 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), | |
210 | } | |
211 | } | |
212 | } | |
213 | ||
214 | impl ToTokens for UnOp { | |
215 | fn to_tokens(&self, tokens: &mut Tokens) { | |
216 | 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 | } | |
221 | } | |
222 | } | |
223 | } |