Implement missing modulo operator in new parser ()

This commit is contained in:
Mees Delzenne 2024-06-13 13:23:24 +02:00 committed by GitHub
parent 1530f9107e
commit 17ea0d881e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 18 additions and 1 deletions
core/src/syn
lib/tests

View file

@ -281,6 +281,7 @@ impl<'a> Lexer<'a> {
}
_ => t!("/"),
},
b'%' => t!("%"),
b'*' => match self.reader.peek() {
Some(b'*') => {
self.reader.next();

View file

@ -113,7 +113,7 @@ impl Parser<'_> {
| t!("<|") => Some((9, 10)),
t!("+") | t!("-") => Some((11, 12)),
t!("*") | t!("×") | t!("/") | t!("÷") => Some((13, 14)),
t!("*") | t!("×") | t!("/") | t!("÷") | t!("%") => Some((13, 14)),
t!("**") => Some((15, 16)),
t!("?:") | t!("??") => Some((17, 18)),
_ => None,
@ -271,6 +271,7 @@ impl Parser<'_> {
t!("-") => Operator::Sub,
t!("*") | t!("×") => Operator::Mul,
t!("/") | t!("÷") => Operator::Div,
t!("%") => Operator::Rem,
t!("") | t!("CONTAINS") => Operator::Contain,
t!("") | t!("CONTAINSNOT") => Operator::NotContain,
t!("") | t!("INSIDE") => Operator::Inside,

View file

@ -160,6 +160,9 @@ macro_rules! t {
("+") => {
$crate::syn::token::TokenKind::Operator($crate::syn::token::Operator::Add)
};
("%") => {
$crate::syn::token::TokenKind::Operator($crate::syn::token::Operator::Modulo)
};
("-") => {
$crate::syn::token::TokenKind::Operator($crate::syn::token::Operator::Subtract)
};

View file

@ -78,6 +78,8 @@ pub enum Operator {
Divide,
/// `×` or `∙`
Mult,
/// `%`
Modulo,
/// `||`
Or,
/// `&&`
@ -156,6 +158,7 @@ impl Operator {
Operator::Or => "||",
Operator::And => "&&",
Operator::Mult => "×",
Operator::Modulo => "%",
Operator::LessEqual => "<=",
Operator::GreaterEqual => ">=",
Operator::Star => "*",

9
lib/tests/expression.rs Normal file
View file

@ -0,0 +1,9 @@
mod helpers;
use helpers::Test;
use surrealdb::err::Error;
#[tokio::test]
async fn modulo() -> Result<(), Error> {
Test::new("8 % 3").await?.expect_val("2")?;
Ok(())
}