blob: 96d8c995855fea943de0d7915277a010c59ff73b [file] [log] [blame]
Alex Crichton62a0a592017-05-22 13:58:53 -07001ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -08002 /// A binary operator: `+`, `+=`, `&`.
David Tolnay461d98e2018-01-07 11:07:19 -08003 ///
4 /// *This type is available if Syn is built with the `"derive"` or `"full"`
5 /// feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -07006 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -07007 pub enum BinOp {
8 /// The `+` operator (addition)
David Tolnayf8db7ba2017-11-11 22:52:16 -08009 Add(Token![+]),
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// The `-` operator (subtraction)
David Tolnayf8db7ba2017-11-11 22:52:16 -080011 Sub(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -070012 /// The `*` operator (multiplication)
David Tolnayf8db7ba2017-11-11 22:52:16 -080013 Mul(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070014 /// The `/` operator (division)
David Tolnayf8db7ba2017-11-11 22:52:16 -080015 Div(Token![/]),
Alex Crichton62a0a592017-05-22 13:58:53 -070016 /// The `%` operator (modulus)
David Tolnayf8db7ba2017-11-11 22:52:16 -080017 Rem(Token![%]),
Alex Crichton62a0a592017-05-22 13:58:53 -070018 /// The `&&` operator (logical and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080019 And(Token![&&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070020 /// The `||` operator (logical or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080021 Or(Token![||]),
Alex Crichton62a0a592017-05-22 13:58:53 -070022 /// The `^` operator (bitwise xor)
David Tolnayf8db7ba2017-11-11 22:52:16 -080023 BitXor(Token![^]),
Alex Crichton62a0a592017-05-22 13:58:53 -070024 /// The `&` operator (bitwise and)
David Tolnayf8db7ba2017-11-11 22:52:16 -080025 BitAnd(Token![&]),
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// The `|` operator (bitwise or)
David Tolnayf8db7ba2017-11-11 22:52:16 -080027 BitOr(Token![|]),
Alex Crichton62a0a592017-05-22 13:58:53 -070028 /// The `<<` operator (shift left)
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 Shl(Token![<<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070030 /// The `>>` operator (shift right)
David Tolnayf8db7ba2017-11-11 22:52:16 -080031 Shr(Token![>>]),
Alex Crichton62a0a592017-05-22 13:58:53 -070032 /// The `==` operator (equality)
David Tolnayf8db7ba2017-11-11 22:52:16 -080033 Eq(Token![==]),
Alex Crichton62a0a592017-05-22 13:58:53 -070034 /// The `<` operator (less than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080035 Lt(Token![<]),
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// The `<=` operator (less than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080037 Le(Token![<=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070038 /// The `!=` operator (not equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080039 Ne(Token![!=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070040 /// The `>=` operator (greater than or equal to)
David Tolnayf8db7ba2017-11-11 22:52:16 -080041 Ge(Token![>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070042 /// The `>` operator (greater than)
David Tolnayf8db7ba2017-11-11 22:52:16 -080043 Gt(Token![>]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070044 /// The `+=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 AddEq(Token![+=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070046 /// The `-=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080047 SubEq(Token![-=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070048 /// The `*=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 MulEq(Token![*=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070050 /// The `/=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 DivEq(Token![/=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 /// The `%=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080053 RemEq(Token![%=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070054 /// The `^=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080055 BitXorEq(Token![^=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070056 /// The `&=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080057 BitAndEq(Token![&=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070058 /// The `|=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080059 BitOrEq(Token![|=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070060 /// The `<<=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080061 ShlEq(Token![<<=]),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070062 /// The `>>=` operator
David Tolnayf8db7ba2017-11-11 22:52:16 -080063 ShrEq(Token![>>=]),
Alex Crichton62a0a592017-05-22 13:58:53 -070064 }
David Tolnay3cb23a92016-10-07 23:02:21 -070065}
66
Alex Crichton62a0a592017-05-22 13:58:53 -070067ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -080068 /// A unary operator: `*`, `!`, `-`.
David Tolnay461d98e2018-01-07 11:07:19 -080069 ///
70 /// *This type is available if Syn is built with the `"derive"` or `"full"`
71 /// feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -070072 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -070073 pub enum UnOp {
74 /// The `*` operator for dereferencing
David Tolnayf8db7ba2017-11-11 22:52:16 -080075 Deref(Token![*]),
Alex Crichton62a0a592017-05-22 13:58:53 -070076 /// The `!` operator for logical inversion
David Tolnayf8db7ba2017-11-11 22:52:16 -080077 Not(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -070078 /// The `-` operator for negation
David Tolnayf8db7ba2017-11-11 22:52:16 -080079 Neg(Token![-]),
Alex Crichton62a0a592017-05-22 13:58:53 -070080 }
David Tolnay3cb23a92016-10-07 23:02:21 -070081}
82
83#[cfg(feature = "parsing")]
84pub mod parsing {
85 use super::*;
David Tolnay2a54cfb2018-08-26 18:54:19 -040086
87 use parse::{Parse, ParseStream, Result};
David Tolnay3cb23a92016-10-07 23:02:21 -070088
David Tolnay01218d12018-08-29 18:13:07 -070089 fn parse_binop(input: ParseStream) -> Result<BinOp> {
90 if input.peek(Token![&&]) {
91 input.parse().map(BinOp::And)
92 } else if input.peek(Token![||]) {
93 input.parse().map(BinOp::Or)
94 } else if input.peek(Token![<<]) {
95 input.parse().map(BinOp::Shl)
96 } else if input.peek(Token![>>]) {
97 input.parse().map(BinOp::Shr)
98 } else if input.peek(Token![==]) {
99 input.parse().map(BinOp::Eq)
100 } else if input.peek(Token![<=]) {
101 input.parse().map(BinOp::Le)
102 } else if input.peek(Token![!=]) {
103 input.parse().map(BinOp::Ne)
104 } else if input.peek(Token![>=]) {
105 input.parse().map(BinOp::Ge)
106 } else if input.peek(Token![+]) {
107 input.parse().map(BinOp::Add)
108 } else if input.peek(Token![-]) {
109 input.parse().map(BinOp::Sub)
110 } else if input.peek(Token![*]) {
111 input.parse().map(BinOp::Mul)
112 } else if input.peek(Token![/]) {
113 input.parse().map(BinOp::Div)
114 } else if input.peek(Token![%]) {
115 input.parse().map(BinOp::Rem)
116 } else if input.peek(Token![^]) {
117 input.parse().map(BinOp::BitXor)
118 } else if input.peek(Token![&]) {
119 input.parse().map(BinOp::BitAnd)
120 } else if input.peek(Token![|]) {
121 input.parse().map(BinOp::BitOr)
122 } else if input.peek(Token![<]) {
123 input.parse().map(BinOp::Lt)
124 } else if input.peek(Token![>]) {
125 input.parse().map(BinOp::Gt)
126 } else {
127 Err(input.error("expected binary operator"))
128 }
129 }
130
131 impl Parse for BinOp {
132 #[cfg(not(feature = "full"))]
133 fn parse(input: ParseStream) -> Result<Self> {
134 parse_binop(input)
David Tolnay2a54cfb2018-08-26 18:54:19 -0400135 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700136
Alex Crichton954046c2017-05-30 21:49:42 -0700137 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -0700138 fn parse(input: ParseStream) -> Result<Self> {
139 if input.peek(Token![+=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400140 input.parse().map(BinOp::AddEq)
David Tolnay01218d12018-08-29 18:13:07 -0700141 } else if input.peek(Token![-=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400142 input.parse().map(BinOp::SubEq)
David Tolnay01218d12018-08-29 18:13:07 -0700143 } else if input.peek(Token![*=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400144 input.parse().map(BinOp::MulEq)
David Tolnay01218d12018-08-29 18:13:07 -0700145 } else if input.peek(Token![/=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400146 input.parse().map(BinOp::DivEq)
David Tolnay01218d12018-08-29 18:13:07 -0700147 } else if input.peek(Token![%=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400148 input.parse().map(BinOp::RemEq)
David Tolnay01218d12018-08-29 18:13:07 -0700149 } else if input.peek(Token![^=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400150 input.parse().map(BinOp::BitXorEq)
David Tolnay01218d12018-08-29 18:13:07 -0700151 } else if input.peek(Token![&=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400152 input.parse().map(BinOp::BitAndEq)
David Tolnay01218d12018-08-29 18:13:07 -0700153 } else if input.peek(Token![|=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400154 input.parse().map(BinOp::BitOrEq)
David Tolnay01218d12018-08-29 18:13:07 -0700155 } else if input.peek(Token![<<=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400156 input.parse().map(BinOp::ShlEq)
David Tolnay01218d12018-08-29 18:13:07 -0700157 } else if input.peek(Token![>>=]) {
David Tolnay2a54cfb2018-08-26 18:54:19 -0400158 input.parse().map(BinOp::ShrEq)
159 } else {
David Tolnay01218d12018-08-29 18:13:07 -0700160 parse_binop(input)
David Tolnay2a54cfb2018-08-26 18:54:19 -0400161 }
162 }
Alex Crichton954046c2017-05-30 21:49:42 -0700163 }
David Tolnay438c9052016-10-07 23:24:48 -0700164
David Tolnay2a54cfb2018-08-26 18:54:19 -0400165 impl Parse for UnOp {
166 fn parse(input: ParseStream) -> Result<Self> {
167 let lookahead = input.lookahead1();
168 if lookahead.peek(Token![*]) {
169 input.parse().map(UnOp::Deref)
170 } else if lookahead.peek(Token![!]) {
171 input.parse().map(UnOp::Not)
172 } else if lookahead.peek(Token![-]) {
173 input.parse().map(UnOp::Neg)
174 } else {
175 Err(lookahead.error())
176 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800177 }
Alex Crichton954046c2017-05-30 21:49:42 -0700178 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700179}
180
181#[cfg(feature = "printing")]
182mod printing {
183 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700184 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700185 use quote::ToTokens;
David Tolnay3cb23a92016-10-07 23:02:21 -0700186
David Tolnay3cb23a92016-10-07 23:02:21 -0700187 impl ToTokens for BinOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700188 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay3cb23a92016-10-07 23:02:21 -0700189 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700190 BinOp::Add(ref t) => t.to_tokens(tokens),
191 BinOp::Sub(ref t) => t.to_tokens(tokens),
192 BinOp::Mul(ref t) => t.to_tokens(tokens),
193 BinOp::Div(ref t) => t.to_tokens(tokens),
194 BinOp::Rem(ref t) => t.to_tokens(tokens),
195 BinOp::And(ref t) => t.to_tokens(tokens),
196 BinOp::Or(ref t) => t.to_tokens(tokens),
197 BinOp::BitXor(ref t) => t.to_tokens(tokens),
198 BinOp::BitAnd(ref t) => t.to_tokens(tokens),
199 BinOp::BitOr(ref t) => t.to_tokens(tokens),
200 BinOp::Shl(ref t) => t.to_tokens(tokens),
201 BinOp::Shr(ref t) => t.to_tokens(tokens),
202 BinOp::Eq(ref t) => t.to_tokens(tokens),
203 BinOp::Lt(ref t) => t.to_tokens(tokens),
204 BinOp::Le(ref t) => t.to_tokens(tokens),
205 BinOp::Ne(ref t) => t.to_tokens(tokens),
206 BinOp::Ge(ref t) => t.to_tokens(tokens),
207 BinOp::Gt(ref t) => t.to_tokens(tokens),
208 BinOp::AddEq(ref t) => t.to_tokens(tokens),
209 BinOp::SubEq(ref t) => t.to_tokens(tokens),
210 BinOp::MulEq(ref t) => t.to_tokens(tokens),
211 BinOp::DivEq(ref t) => t.to_tokens(tokens),
212 BinOp::RemEq(ref t) => t.to_tokens(tokens),
213 BinOp::BitXorEq(ref t) => t.to_tokens(tokens),
214 BinOp::BitAndEq(ref t) => t.to_tokens(tokens),
215 BinOp::BitOrEq(ref t) => t.to_tokens(tokens),
216 BinOp::ShlEq(ref t) => t.to_tokens(tokens),
217 BinOp::ShrEq(ref t) => t.to_tokens(tokens),
David Tolnay3cb23a92016-10-07 23:02:21 -0700218 }
219 }
220 }
221
222 impl ToTokens for UnOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700223 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700224 match *self {
225 UnOp::Deref(ref t) => t.to_tokens(tokens),
226 UnOp::Not(ref t) => t.to_tokens(tokens),
227 UnOp::Neg(ref t) => t.to_tokens(tokens),
228 }
David Tolnay3cb23a92016-10-07 23:02:21 -0700229 }
230 }
231}