noa_parser/bytes/primitives/
binary_operator.rs

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