• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ast_enum! {
2     /// A binary operator: `+`, `+=`, `&`.
3     #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
4     #[non_exhaustive]
5     pub enum BinOp {
6         /// The `+` operator (addition)
7         Add(Token![+]),
8         /// The `-` operator (subtraction)
9         Sub(Token![-]),
10         /// The `*` operator (multiplication)
11         Mul(Token![*]),
12         /// The `/` operator (division)
13         Div(Token![/]),
14         /// The `%` operator (modulus)
15         Rem(Token![%]),
16         /// The `&&` operator (logical and)
17         And(Token![&&]),
18         /// The `||` operator (logical or)
19         Or(Token![||]),
20         /// The `^` operator (bitwise xor)
21         BitXor(Token![^]),
22         /// The `&` operator (bitwise and)
23         BitAnd(Token![&]),
24         /// The `|` operator (bitwise or)
25         BitOr(Token![|]),
26         /// The `<<` operator (shift left)
27         Shl(Token![<<]),
28         /// The `>>` operator (shift right)
29         Shr(Token![>>]),
30         /// The `==` operator (equality)
31         Eq(Token![==]),
32         /// The `<` operator (less than)
33         Lt(Token![<]),
34         /// The `<=` operator (less than or equal to)
35         Le(Token![<=]),
36         /// The `!=` operator (not equal to)
37         Ne(Token![!=]),
38         /// The `>=` operator (greater than or equal to)
39         Ge(Token![>=]),
40         /// The `>` operator (greater than)
41         Gt(Token![>]),
42         /// The `+=` operator
43         AddAssign(Token![+=]),
44         /// The `-=` operator
45         SubAssign(Token![-=]),
46         /// The `*=` operator
47         MulAssign(Token![*=]),
48         /// The `/=` operator
49         DivAssign(Token![/=]),
50         /// The `%=` operator
51         RemAssign(Token![%=]),
52         /// The `^=` operator
53         BitXorAssign(Token![^=]),
54         /// The `&=` operator
55         BitAndAssign(Token![&=]),
56         /// The `|=` operator
57         BitOrAssign(Token![|=]),
58         /// The `<<=` operator
59         ShlAssign(Token![<<=]),
60         /// The `>>=` operator
61         ShrAssign(Token![>>=]),
62     }
63 }
64 
65 ast_enum! {
66     /// A unary operator: `*`, `!`, `-`.
67     #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
68     #[non_exhaustive]
69     pub enum UnOp {
70         /// The `*` operator for dereferencing
71         Deref(Token![*]),
72         /// The `!` operator for logical inversion
73         Not(Token![!]),
74         /// The `-` operator for negation
75         Neg(Token![-]),
76     }
77 }
78 
79 #[cfg(feature = "parsing")]
80 pub(crate) mod parsing {
81     use super::*;
82     use crate::parse::{Parse, ParseStream, Result};
83 
84     #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
85     impl Parse for BinOp {
parse(input: ParseStream) -> Result<Self>86         fn parse(input: ParseStream) -> Result<Self> {
87             if input.peek(Token![+=]) {
88                 input.parse().map(BinOp::AddAssign)
89             } else if input.peek(Token![-=]) {
90                 input.parse().map(BinOp::SubAssign)
91             } else if input.peek(Token![*=]) {
92                 input.parse().map(BinOp::MulAssign)
93             } else if input.peek(Token![/=]) {
94                 input.parse().map(BinOp::DivAssign)
95             } else if input.peek(Token![%=]) {
96                 input.parse().map(BinOp::RemAssign)
97             } else if input.peek(Token![^=]) {
98                 input.parse().map(BinOp::BitXorAssign)
99             } else if input.peek(Token![&=]) {
100                 input.parse().map(BinOp::BitAndAssign)
101             } else if input.peek(Token![|=]) {
102                 input.parse().map(BinOp::BitOrAssign)
103             } else if input.peek(Token![<<=]) {
104                 input.parse().map(BinOp::ShlAssign)
105             } else if input.peek(Token![>>=]) {
106                 input.parse().map(BinOp::ShrAssign)
107             } else if input.peek(Token![&&]) {
108                 input.parse().map(BinOp::And)
109             } else if input.peek(Token![||]) {
110                 input.parse().map(BinOp::Or)
111             } else if input.peek(Token![<<]) {
112                 input.parse().map(BinOp::Shl)
113             } else if input.peek(Token![>>]) {
114                 input.parse().map(BinOp::Shr)
115             } else if input.peek(Token![==]) {
116                 input.parse().map(BinOp::Eq)
117             } else if input.peek(Token![<=]) {
118                 input.parse().map(BinOp::Le)
119             } else if input.peek(Token![!=]) {
120                 input.parse().map(BinOp::Ne)
121             } else if input.peek(Token![>=]) {
122                 input.parse().map(BinOp::Ge)
123             } else if input.peek(Token![+]) {
124                 input.parse().map(BinOp::Add)
125             } else if input.peek(Token![-]) {
126                 input.parse().map(BinOp::Sub)
127             } else if input.peek(Token![*]) {
128                 input.parse().map(BinOp::Mul)
129             } else if input.peek(Token![/]) {
130                 input.parse().map(BinOp::Div)
131             } else if input.peek(Token![%]) {
132                 input.parse().map(BinOp::Rem)
133             } else if input.peek(Token![^]) {
134                 input.parse().map(BinOp::BitXor)
135             } else if input.peek(Token![&]) {
136                 input.parse().map(BinOp::BitAnd)
137             } else if input.peek(Token![|]) {
138                 input.parse().map(BinOp::BitOr)
139             } else if input.peek(Token![<]) {
140                 input.parse().map(BinOp::Lt)
141             } else if input.peek(Token![>]) {
142                 input.parse().map(BinOp::Gt)
143             } else {
144                 Err(input.error("expected binary operator"))
145             }
146         }
147     }
148 
149     #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
150     impl Parse for UnOp {
parse(input: ParseStream) -> Result<Self>151         fn parse(input: ParseStream) -> Result<Self> {
152             let lookahead = input.lookahead1();
153             if lookahead.peek(Token![*]) {
154                 input.parse().map(UnOp::Deref)
155             } else if lookahead.peek(Token![!]) {
156                 input.parse().map(UnOp::Not)
157             } else if lookahead.peek(Token![-]) {
158                 input.parse().map(UnOp::Neg)
159             } else {
160                 Err(lookahead.error())
161             }
162         }
163     }
164 }
165 
166 #[cfg(feature = "printing")]
167 mod printing {
168     use super::*;
169     use proc_macro2::TokenStream;
170     use quote::ToTokens;
171 
172     #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
173     impl ToTokens for BinOp {
to_tokens(&self, tokens: &mut TokenStream)174         fn to_tokens(&self, tokens: &mut TokenStream) {
175             match self {
176                 BinOp::Add(t) => t.to_tokens(tokens),
177                 BinOp::Sub(t) => t.to_tokens(tokens),
178                 BinOp::Mul(t) => t.to_tokens(tokens),
179                 BinOp::Div(t) => t.to_tokens(tokens),
180                 BinOp::Rem(t) => t.to_tokens(tokens),
181                 BinOp::And(t) => t.to_tokens(tokens),
182                 BinOp::Or(t) => t.to_tokens(tokens),
183                 BinOp::BitXor(t) => t.to_tokens(tokens),
184                 BinOp::BitAnd(t) => t.to_tokens(tokens),
185                 BinOp::BitOr(t) => t.to_tokens(tokens),
186                 BinOp::Shl(t) => t.to_tokens(tokens),
187                 BinOp::Shr(t) => t.to_tokens(tokens),
188                 BinOp::Eq(t) => t.to_tokens(tokens),
189                 BinOp::Lt(t) => t.to_tokens(tokens),
190                 BinOp::Le(t) => t.to_tokens(tokens),
191                 BinOp::Ne(t) => t.to_tokens(tokens),
192                 BinOp::Ge(t) => t.to_tokens(tokens),
193                 BinOp::Gt(t) => t.to_tokens(tokens),
194                 BinOp::AddAssign(t) => t.to_tokens(tokens),
195                 BinOp::SubAssign(t) => t.to_tokens(tokens),
196                 BinOp::MulAssign(t) => t.to_tokens(tokens),
197                 BinOp::DivAssign(t) => t.to_tokens(tokens),
198                 BinOp::RemAssign(t) => t.to_tokens(tokens),
199                 BinOp::BitXorAssign(t) => t.to_tokens(tokens),
200                 BinOp::BitAndAssign(t) => t.to_tokens(tokens),
201                 BinOp::BitOrAssign(t) => t.to_tokens(tokens),
202                 BinOp::ShlAssign(t) => t.to_tokens(tokens),
203                 BinOp::ShrAssign(t) => t.to_tokens(tokens),
204             }
205         }
206     }
207 
208     #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
209     impl ToTokens for UnOp {
to_tokens(&self, tokens: &mut TokenStream)210         fn to_tokens(&self, tokens: &mut TokenStream) {
211             match self {
212                 UnOp::Deref(t) => t.to_tokens(tokens),
213                 UnOp::Not(t) => t.to_tokens(tokens),
214                 UnOp::Neg(t) => t.to_tokens(tokens),
215             }
216         }
217     }
218 }
219