2 /// A binary operator: `+`, `+=`, `&`.
4 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
7 /// The `+` operator (addition)
9 /// The `-` operator (subtraction)
11 /// The `*` operator (multiplication)
13 /// The `/` operator (division)
15 /// The `%` operator (modulus)
17 /// The `&&` operator (logical and)
19 /// The `||` operator (logical or)
21 /// The `^` operator (bitwise xor)
23 /// The `&` operator (bitwise and)
25 /// The `|` operator (bitwise or)
27 /// The `<<` operator (shift left)
29 /// The `>>` operator (shift right)
31 /// The `==` operator (equality)
33 /// The `<` operator (less than)
35 /// The `<=` operator (less than or equal to)
37 /// The `!=` operator (not equal to)
39 /// The `>=` operator (greater than or equal to)
41 /// The `>` operator (greater than)
59 /// The `<<=` operator
61 /// The `>>=` operator
67 /// A unary operator: `*`, `!`, `-`.
69 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
72 /// The `*` operator for dereferencing
74 /// The `!` operator for logical inversion
76 /// The `-` operator for negation
81 #[cfg(feature = "parsing")]
85 use crate::parse
::{Parse, ParseStream, Result}
;
87 fn parse_binop(input
: ParseStream
) -> Result
<BinOp
> {
88 if input
.peek(Token
![&&]) {
89 input
.parse().map(BinOp
::And
)
90 } else if input
.peek(Token
![||]) {
91 input
.parse().map(BinOp
::Or
)
92 } else if input
.peek(Token
![<<]) {
93 input
.parse().map(BinOp
::Shl
)
94 } else if input
.peek(Token
![>>]) {
95 input
.parse().map(BinOp
::Shr
)
96 } else if input
.peek(Token
![==]) {
97 input
.parse().map(BinOp
::Eq
)
98 } else if input
.peek(Token
![<=]) {
99 input
.parse().map(BinOp
::Le
)
100 } else if input
.peek(Token
![!=]) {
101 input
.parse().map(BinOp
::Ne
)
102 } else if input
.peek(Token
![>=]) {
103 input
.parse().map(BinOp
::Ge
)
104 } else if input
.peek(Token
![+]) {
105 input
.parse().map(BinOp
::Add
)
106 } else if input
.peek(Token
![-]) {
107 input
.parse().map(BinOp
::Sub
)
108 } else if input
.peek(Token
![*]) {
109 input
.parse().map(BinOp
::Mul
)
110 } else if input
.peek(Token
![/]) {
111 input
.parse().map(BinOp
::Div
)
112 } else if input
.peek(Token
![%]) {
113 input
.parse().map(BinOp
::Rem
)
114 } else if input
.peek(Token
![^
]) {
115 input
.parse().map(BinOp
::BitXor
)
116 } else if input
.peek(Token
![&]) {
117 input
.parse().map(BinOp
::BitAnd
)
118 } else if input
.peek(Token
![|]) {
119 input
.parse().map(BinOp
::BitOr
)
120 } else if input
.peek(Token
![<]) {
121 input
.parse().map(BinOp
::Lt
)
122 } else if input
.peek(Token
![>]) {
123 input
.parse().map(BinOp
::Gt
)
125 Err(input
.error("expected binary operator"))
129 impl Parse
for BinOp
{
130 #[cfg(not(feature = "full"))]
131 fn parse(input
: ParseStream
) -> Result
<Self> {
135 #[cfg(feature = "full")]
136 fn parse(input
: ParseStream
) -> Result
<Self> {
137 if input
.peek(Token
![+=]) {
138 input
.parse().map(BinOp
::AddEq
)
139 } else if input
.peek(Token
![-=]) {
140 input
.parse().map(BinOp
::SubEq
)
141 } else if input
.peek(Token
![*=]) {
142 input
.parse().map(BinOp
::MulEq
)
143 } else if input
.peek(Token
![/=]) {
144 input
.parse().map(BinOp
::DivEq
)
145 } else if input
.peek(Token
![%=]) {
146 input
.parse().map(BinOp
::RemEq
)
147 } else if input
.peek(Token
![^
=]) {
148 input
.parse().map(BinOp
::BitXorEq
)
149 } else if input
.peek(Token
![&=]) {
150 input
.parse().map(BinOp
::BitAndEq
)
151 } else if input
.peek(Token
![|=]) {
152 input
.parse().map(BinOp
::BitOrEq
)
153 } else if input
.peek(Token
![<<=]) {
154 input
.parse().map(BinOp
::ShlEq
)
155 } else if input
.peek(Token
![>>=]) {
156 input
.parse().map(BinOp
::ShrEq
)
163 impl Parse
for UnOp
{
164 fn parse(input
: ParseStream
) -> Result
<Self> {
165 let lookahead
= input
.lookahead1();
166 if lookahead
.peek(Token
![*]) {
167 input
.parse().map(UnOp
::Deref
)
168 } else if lookahead
.peek(Token
![!]) {
169 input
.parse().map(UnOp
::Not
)
170 } else if lookahead
.peek(Token
![-]) {
171 input
.parse().map(UnOp
::Neg
)
173 Err(lookahead
.error())
179 #[cfg(feature = "printing")]
182 use proc_macro2
::TokenStream
;
185 impl ToTokens
for BinOp
{
186 fn to_tokens(&self, tokens
: &mut TokenStream
) {
188 BinOp
::Add(t
) => t
.to_tokens(tokens
),
189 BinOp
::Sub(t
) => t
.to_tokens(tokens
),
190 BinOp
::Mul(t
) => t
.to_tokens(tokens
),
191 BinOp
::Div(t
) => t
.to_tokens(tokens
),
192 BinOp
::Rem(t
) => t
.to_tokens(tokens
),
193 BinOp
::And(t
) => t
.to_tokens(tokens
),
194 BinOp
::Or(t
) => t
.to_tokens(tokens
),
195 BinOp
::BitXor(t
) => t
.to_tokens(tokens
),
196 BinOp
::BitAnd(t
) => t
.to_tokens(tokens
),
197 BinOp
::BitOr(t
) => t
.to_tokens(tokens
),
198 BinOp
::Shl(t
) => t
.to_tokens(tokens
),
199 BinOp
::Shr(t
) => t
.to_tokens(tokens
),
200 BinOp
::Eq(t
) => t
.to_tokens(tokens
),
201 BinOp
::Lt(t
) => t
.to_tokens(tokens
),
202 BinOp
::Le(t
) => t
.to_tokens(tokens
),
203 BinOp
::Ne(t
) => t
.to_tokens(tokens
),
204 BinOp
::Ge(t
) => t
.to_tokens(tokens
),
205 BinOp
::Gt(t
) => t
.to_tokens(tokens
),
206 BinOp
::AddEq(t
) => t
.to_tokens(tokens
),
207 BinOp
::SubEq(t
) => t
.to_tokens(tokens
),
208 BinOp
::MulEq(t
) => t
.to_tokens(tokens
),
209 BinOp
::DivEq(t
) => t
.to_tokens(tokens
),
210 BinOp
::RemEq(t
) => t
.to_tokens(tokens
),
211 BinOp
::BitXorEq(t
) => t
.to_tokens(tokens
),
212 BinOp
::BitAndEq(t
) => t
.to_tokens(tokens
),
213 BinOp
::BitOrEq(t
) => t
.to_tokens(tokens
),
214 BinOp
::ShlEq(t
) => t
.to_tokens(tokens
),
215 BinOp
::ShrEq(t
) => t
.to_tokens(tokens
),
220 impl ToTokens
for UnOp
{
221 fn to_tokens(&self, tokens
: &mut TokenStream
) {
223 UnOp
::Deref(t
) => t
.to_tokens(tokens
),
224 UnOp
::Not(t
) => t
.to_tokens(tokens
),
225 UnOp
::Neg(t
) => t
.to_tokens(tokens
),