op.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. ast_enum! {
  3. /// A binary operator: `+`, `+=`, `&`.
  4. #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))]
  5. #[non_exhaustive]
  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. AddAssign(Token![+=]),
  45. /// The `-=` operator
  46. SubAssign(Token![-=]),
  47. /// The `*=` operator
  48. MulAssign(Token![*=]),
  49. /// The `/=` operator
  50. DivAssign(Token![/=]),
  51. /// The `%=` operator
  52. RemAssign(Token![%=]),
  53. /// The `^=` operator
  54. BitXorAssign(Token![^=]),
  55. /// The `&=` operator
  56. BitAndAssign(Token![&=]),
  57. /// The `|=` operator
  58. BitOrAssign(Token![|=]),
  59. /// The `<<=` operator
  60. ShlAssign(Token![<<=]),
  61. /// The `>>=` operator
  62. ShrAssign(Token![>>=]),
  63. }
  64. }
  65. ast_enum! {
  66. /// A unary operator: `*`, `!`, `-`.
  67. #[cfg_attr(docsrs, 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. #[cfg(feature = "parsing")]
  79. pub(crate) mod parsing {
  80. use crate::error::Result;
  81. use crate::op::{BinOp, UnOp};
  82. use crate::parse::{Parse, ParseStream};
  83. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  84. impl Parse for BinOp {
  85. fn parse(input: ParseStream) -> Result<Self> {
  86. if input.peek(Token![+=]) {
  87. input.parse().map(BinOp::AddAssign)
  88. } else if input.peek(Token![-=]) {
  89. input.parse().map(BinOp::SubAssign)
  90. } else if input.peek(Token![*=]) {
  91. input.parse().map(BinOp::MulAssign)
  92. } else if input.peek(Token![/=]) {
  93. input.parse().map(BinOp::DivAssign)
  94. } else if input.peek(Token![%=]) {
  95. input.parse().map(BinOp::RemAssign)
  96. } else if input.peek(Token![^=]) {
  97. input.parse().map(BinOp::BitXorAssign)
  98. } else if input.peek(Token![&=]) {
  99. input.parse().map(BinOp::BitAndAssign)
  100. } else if input.peek(Token![|=]) {
  101. input.parse().map(BinOp::BitOrAssign)
  102. } else if input.peek(Token![<<=]) {
  103. input.parse().map(BinOp::ShlAssign)
  104. } else if input.peek(Token![>>=]) {
  105. input.parse().map(BinOp::ShrAssign)
  106. } else if input.peek(Token![&&]) {
  107. input.parse().map(BinOp::And)
  108. } else if input.peek(Token![||]) {
  109. input.parse().map(BinOp::Or)
  110. } else if input.peek(Token![<<]) {
  111. input.parse().map(BinOp::Shl)
  112. } else if input.peek(Token![>>]) {
  113. input.parse().map(BinOp::Shr)
  114. } else if input.peek(Token![==]) {
  115. input.parse().map(BinOp::Eq)
  116. } else if input.peek(Token![<=]) {
  117. input.parse().map(BinOp::Le)
  118. } else if input.peek(Token![!=]) {
  119. input.parse().map(BinOp::Ne)
  120. } else if input.peek(Token![>=]) {
  121. input.parse().map(BinOp::Ge)
  122. } else if input.peek(Token![+]) {
  123. input.parse().map(BinOp::Add)
  124. } else if input.peek(Token![-]) {
  125. input.parse().map(BinOp::Sub)
  126. } else if input.peek(Token![*]) {
  127. input.parse().map(BinOp::Mul)
  128. } else if input.peek(Token![/]) {
  129. input.parse().map(BinOp::Div)
  130. } else if input.peek(Token![%]) {
  131. input.parse().map(BinOp::Rem)
  132. } else if input.peek(Token![^]) {
  133. input.parse().map(BinOp::BitXor)
  134. } else if input.peek(Token![&]) {
  135. input.parse().map(BinOp::BitAnd)
  136. } else if input.peek(Token![|]) {
  137. input.parse().map(BinOp::BitOr)
  138. } else if input.peek(Token![<]) {
  139. input.parse().map(BinOp::Lt)
  140. } else if input.peek(Token![>]) {
  141. input.parse().map(BinOp::Gt)
  142. } else {
  143. Err(input.error("expected binary operator"))
  144. }
  145. }
  146. }
  147. #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
  148. impl Parse for UnOp {
  149. fn parse(input: ParseStream) -> Result<Self> {
  150. let lookahead = input.lookahead1();
  151. if lookahead.peek(Token![*]) {
  152. input.parse().map(UnOp::Deref)
  153. } else if lookahead.peek(Token![!]) {
  154. input.parse().map(UnOp::Not)
  155. } else if lookahead.peek(Token![-]) {
  156. input.parse().map(UnOp::Neg)
  157. } else {
  158. Err(lookahead.error())
  159. }
  160. }
  161. }
  162. }
  163. #[cfg(feature = "printing")]
  164. mod printing {
  165. use crate::op::{BinOp, UnOp};
  166. use proc_macro2::TokenStream;
  167. use quote::ToTokens;
  168. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  169. impl ToTokens for BinOp {
  170. fn to_tokens(&self, tokens: &mut TokenStream) {
  171. match self {
  172. BinOp::Add(t) => t.to_tokens(tokens),
  173. BinOp::Sub(t) => t.to_tokens(tokens),
  174. BinOp::Mul(t) => t.to_tokens(tokens),
  175. BinOp::Div(t) => t.to_tokens(tokens),
  176. BinOp::Rem(t) => t.to_tokens(tokens),
  177. BinOp::And(t) => t.to_tokens(tokens),
  178. BinOp::Or(t) => t.to_tokens(tokens),
  179. BinOp::BitXor(t) => t.to_tokens(tokens),
  180. BinOp::BitAnd(t) => t.to_tokens(tokens),
  181. BinOp::BitOr(t) => t.to_tokens(tokens),
  182. BinOp::Shl(t) => t.to_tokens(tokens),
  183. BinOp::Shr(t) => t.to_tokens(tokens),
  184. BinOp::Eq(t) => t.to_tokens(tokens),
  185. BinOp::Lt(t) => t.to_tokens(tokens),
  186. BinOp::Le(t) => t.to_tokens(tokens),
  187. BinOp::Ne(t) => t.to_tokens(tokens),
  188. BinOp::Ge(t) => t.to_tokens(tokens),
  189. BinOp::Gt(t) => t.to_tokens(tokens),
  190. BinOp::AddAssign(t) => t.to_tokens(tokens),
  191. BinOp::SubAssign(t) => t.to_tokens(tokens),
  192. BinOp::MulAssign(t) => t.to_tokens(tokens),
  193. BinOp::DivAssign(t) => t.to_tokens(tokens),
  194. BinOp::RemAssign(t) => t.to_tokens(tokens),
  195. BinOp::BitXorAssign(t) => t.to_tokens(tokens),
  196. BinOp::BitAndAssign(t) => t.to_tokens(tokens),
  197. BinOp::BitOrAssign(t) => t.to_tokens(tokens),
  198. BinOp::ShlAssign(t) => t.to_tokens(tokens),
  199. BinOp::ShrAssign(t) => t.to_tokens(tokens),
  200. }
  201. }
  202. }
  203. #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
  204. impl ToTokens for UnOp {
  205. fn to_tokens(&self, tokens: &mut TokenStream) {
  206. match self {
  207. UnOp::Deref(t) => t.to_tokens(tokens),
  208. UnOp::Not(t) => t.to_tokens(tokens),
  209. UnOp::Neg(t) => t.to_tokens(tokens),
  210. }
  211. }
  212. }
  213. }