noa_parser/bytes/primitives/
binary_operator.rs

1//! Binary operators
2use crate::acceptor::Acceptor;
3use crate::bytes::token::Token;
4use crate::errors::{ParseError, ParseResult};
5use crate::recognizer::recognize;
6use crate::scanner::Scanner;
7use crate::visitor::Visitor;
8
9enum BinaryOperatorInternal {
10    Equal(BinaryOperatorEqual),
11    NotEqual(BinaryOperatorNotEqual),
12    LessThan(BinaryOperatorLessThan),
13    LessThanOrEqual(BinaryOperatorLessThanOrEqual),
14    GreaterThan(BinaryOperatorGreaterThan),
15    GreaterThanOrEqual(BinaryOperatorGreaterThanOrEqual),
16}
17
18/// Binary operators
19///
20/// This enum represents all the binary operators.
21///
22/// # Variants
23///
24/// * `Equal` - The `==` operator
25/// * `NotEqual` - The `!=` operator
26/// * `LessThan` - The `<` operator
27/// * `LessThanOrEqual` - The `<=` operator
28/// * `GreaterThan` - The `>` operator
29/// * `GreaterThanOrEqual` - The `>=` operator
30pub enum BinaryOperator {
31    /// The `==` operator
32    Equal,
33    /// The `!=` operator
34    NotEqual,
35    /// The `<` operator
36    LessThan,
37    /// The `<=` operator
38    LessThanOrEqual,
39    /// The `>` operator
40    GreaterThan,
41    /// The `>=` operator
42    GreaterThanOrEqual,
43}
44
45impl From<BinaryOperatorInternal> for BinaryOperator {
46    fn from(value: BinaryOperatorInternal) -> Self {
47        match value {
48            BinaryOperatorInternal::Equal(_) => BinaryOperator::Equal,
49            BinaryOperatorInternal::NotEqual(_) => BinaryOperator::NotEqual,
50            BinaryOperatorInternal::LessThan(_) => BinaryOperator::LessThan,
51            BinaryOperatorInternal::LessThanOrEqual(_) => BinaryOperator::LessThanOrEqual,
52            BinaryOperatorInternal::GreaterThan(_) => BinaryOperator::GreaterThan,
53            BinaryOperatorInternal::GreaterThanOrEqual(_) => BinaryOperator::GreaterThanOrEqual,
54        }
55    }
56}
57
58struct BinaryOperatorEqual;
59
60impl<'a> Visitor<'a, u8> for BinaryOperatorEqual {
61    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
62        recognize(Token::Equal, scanner)?;
63        recognize(Token::Equal, scanner)?;
64        Ok(BinaryOperatorEqual)
65    }
66}
67
68struct BinaryOperatorNotEqual;
69
70impl<'a> Visitor<'a, u8> for BinaryOperatorNotEqual {
71    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
72        recognize(Token::Exclamation, scanner)?;
73        recognize(Token::Equal, scanner)?;
74        Ok(BinaryOperatorNotEqual)
75    }
76}
77
78struct BinaryOperatorLessThan;
79
80impl<'a> Visitor<'a, u8> for BinaryOperatorLessThan {
81    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
82        recognize(Token::LessThan, scanner)?;
83        Ok(BinaryOperatorLessThan)
84    }
85}
86
87struct BinaryOperatorLessThanOrEqual;
88
89impl<'a> Visitor<'a, u8> for BinaryOperatorLessThanOrEqual {
90    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
91        recognize(Token::LessThan, scanner)?;
92        recognize(Token::Equal, scanner)?;
93        Ok(BinaryOperatorLessThanOrEqual)
94    }
95}
96
97struct BinaryOperatorGreaterThan;
98
99impl<'a> Visitor<'a, u8> for BinaryOperatorGreaterThan {
100    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
101        recognize(Token::GreaterThan, scanner)?;
102        Ok(BinaryOperatorGreaterThan)
103    }
104}
105
106struct BinaryOperatorGreaterThanOrEqual;
107
108impl<'a> Visitor<'a, u8> for BinaryOperatorGreaterThanOrEqual {
109    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
110        recognize(Token::GreaterThan, scanner)?;
111        recognize(Token::Equal, scanner)?;
112        Ok(BinaryOperatorGreaterThanOrEqual)
113    }
114}
115
116impl<'a> Visitor<'a, u8> for BinaryOperator {
117    /// Try to accept the binary operator and return the result of the visit.
118    ///
119    /// # Arguments
120    ///
121    /// * `scanner` - The scanner to accept the binary operator for.
122    ///
123    /// # Returns
124    ///
125    /// The result of the visit.
126    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
127        let acceptor = Acceptor::new(scanner)
128            .try_or(BinaryOperatorInternal::Equal)?
129            .try_or(BinaryOperatorInternal::NotEqual)?
130            .try_or(BinaryOperatorInternal::LessThan)?
131            .try_or(BinaryOperatorInternal::LessThanOrEqual)?
132            .try_or(BinaryOperatorInternal::GreaterThan)?
133            .try_or(BinaryOperatorInternal::GreaterThanOrEqual)?
134            .finish()
135            .ok_or(ParseError::UnexpectedToken)?;
136        Ok(acceptor.into())
137    }
138}