]> git.proxmox.com Git - rustc.git/blob - vendor/syn/src/op.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / vendor / syn / src / op.rs
1 ast_enum! {
2 /// A binary operator: `+`, `+=`, `&`.
3 ///
4 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
5 /// feature.*
6 pub enum BinOp {
7 /// The `+` operator (addition)
8 Add(Token![+]),
9 /// The `-` operator (subtraction)
10 Sub(Token![-]),
11 /// The `*` operator (multiplication)
12 Mul(Token![*]),
13 /// The `/` operator (division)
14 Div(Token![/]),
15 /// The `%` operator (modulus)
16 Rem(Token![%]),
17 /// The `&&` operator (logical and)
18 And(Token![&&]),
19 /// The `||` operator (logical or)
20 Or(Token![||]),
21 /// The `^` operator (bitwise xor)
22 BitXor(Token![^]),
23 /// The `&` operator (bitwise and)
24 BitAnd(Token![&]),
25 /// The `|` operator (bitwise or)
26 BitOr(Token![|]),
27 /// The `<<` operator (shift left)
28 Shl(Token![<<]),
29 /// The `>>` operator (shift right)
30 Shr(Token![>>]),
31 /// The `==` operator (equality)
32 Eq(Token![==]),
33 /// The `<` operator (less than)
34 Lt(Token![<]),
35 /// The `<=` operator (less than or equal to)
36 Le(Token![<=]),
37 /// The `!=` operator (not equal to)
38 Ne(Token![!=]),
39 /// The `>=` operator (greater than or equal to)
40 Ge(Token![>=]),
41 /// The `>` operator (greater than)
42 Gt(Token![>]),
43 /// The `+=` operator
44 AddEq(Token![+=]),
45 /// The `-=` operator
46 SubEq(Token![-=]),
47 /// The `*=` operator
48 MulEq(Token![*=]),
49 /// The `/=` operator
50 DivEq(Token![/=]),
51 /// The `%=` operator
52 RemEq(Token![%=]),
53 /// The `^=` operator
54 BitXorEq(Token![^=]),
55 /// The `&=` operator
56 BitAndEq(Token![&=]),
57 /// The `|=` operator
58 BitOrEq(Token![|=]),
59 /// The `<<=` operator
60 ShlEq(Token![<<=]),
61 /// The `>>=` operator
62 ShrEq(Token![>>=]),
63 }
64 }
65
66 ast_enum! {
67 /// A unary operator: `*`, `!`, `-`.
68 ///
69 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
70 /// feature.*
71 pub enum UnOp {
72 /// The `*` operator for dereferencing
73 Deref(Token![*]),
74 /// The `!` operator for logical inversion
75 Not(Token![!]),
76 /// The `-` operator for negation
77 Neg(Token![-]),
78 }
79 }
80
81 #[cfg(feature = "parsing")]
82 pub mod parsing {
83 use super::*;
84
85 use crate::parse::{Parse, ParseStream, Result};
86
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)
124 } else {
125 Err(input.error("expected binary operator"))
126 }
127 }
128
129 impl Parse for BinOp {
130 #[cfg(not(feature = "full"))]
131 fn parse(input: ParseStream) -> Result<Self> {
132 parse_binop(input)
133 }
134
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)
157 } else {
158 parse_binop(input)
159 }
160 }
161 }
162
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)
172 } else {
173 Err(lookahead.error())
174 }
175 }
176 }
177 }
178
179 #[cfg(feature = "printing")]
180 mod printing {
181 use super::*;
182 use proc_macro2::TokenStream;
183 use quote::ToTokens;
184
185 impl ToTokens for BinOp {
186 fn to_tokens(&self, tokens: &mut TokenStream) {
187 match self {
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),
216 }
217 }
218 }
219
220 impl ToTokens for UnOp {
221 fn to_tokens(&self, tokens: &mut TokenStream) {
222 match self {
223 UnOp::Deref(t) => t.to_tokens(tokens),
224 UnOp::Not(t) => t.to_tokens(tokens),
225 UnOp::Neg(t) => t.to_tokens(tokens),
226 }
227 }
228 }
229 }