Skip to content

Commit

Permalink
fixing more edge case bugs for parsing whitespaced scopes and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 17, 2024
1 parent 492f13f commit 06e100d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vimspector.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"configuration": {
"request": "launch",
"program": "${workspaceRoot}/target/debug/deps/parser_test-ce9cb22a64720f6c",
"args": ["test", "scoped_test_case"]
"args": ["test", "scopes_bug_test"]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bool_expr_parser_nom"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["Nikolajus Krauklis <[email protected]>"]

Expand Down
5 changes: 3 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::fmt;
use std::str::FromStr;

use chrono::NaiveDate;

Expand Down Expand Up @@ -73,7 +74,7 @@ pub enum ComparisonOp {
}

impl ComparisonOp {
pub fn from_str(expr: &str) -> Self {
pub fn build_from_str(expr: &str) -> Self {
match expr {
"==" | "=" => ComparisonOp::Eq,
">" => ComparisonOp::More,
Expand Down Expand Up @@ -105,7 +106,7 @@ pub enum LogicOp {
}

impl LogicOp {
pub fn from_str(i: &str) -> Self {
pub fn build_from_str(i: &str) -> Self {
match i.to_lowercase().as_str() {
"and" | "&&" => LogicOp::And,
"or" | "||" => LogicOp::Or,
Expand Down
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn parse_expr(input: &str) -> IResult<&str, AstNode> {
}

pub fn parse(i: &str) -> IResult<&str, AstNode> {
alt((parse_parenthesized_expr, parse_expr))(i)
alt((ws(parse_expr), ws(parse_parenthesized_expr)))(i)
}

mod tests {
Expand Down
39 changes: 37 additions & 2 deletions tests/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ fn test_parsing() {}

#[test]
fn test_evaluation() {
let rule = r###"accountRole in (Admin,admin,"Admin/Order Manager")
let rule = r###"
accountRole in (Admin,admin,"Admin/Order Manager")
and upper(account_country_code) in (LT , NL, DE, GB, US)
and account_uuid in ("543987b0-e69f-41ec-9a68-cfc5cfb15afe", "6133b8d6-4078-4270-9a68-fa0ac78bf512")
and accountType in ("Some Corporate & Managament Type", Corporate , Managament)
Expand Down Expand Up @@ -39,7 +40,7 @@ fn test_evaluation() {

#[test]
fn scoped_test_case() {
let rule = r###"accountRole in (Admin, "Admin/Order Manager") and
let rule = r###"(accountRole in (Admin, "Admin/Order Manager")) and
((lower(account_country_code) == lt or account_uuid = 32434) and accountType="Some Corporate & Managament Type") and user_id == 2032312"###;

let context = HashMap::from([
Expand All @@ -61,3 +62,37 @@ fn scoped_test_case() {
assert_eq!(val, true);
assert_eq!(i, ""); // empty remainder of parsed string
}

#[test]
fn scopes_bug_test() {
let rule = "(a=1 or b=2) and ((c=3 or d=4) and e=5)";
let context = HashMap::from([
("a", Atom::Number(1)),
("b", Atom::Number(2)),
("c", Atom::Number(3)),
("d", Atom::Number(4)),
("e", Atom::Number(5)),
]);
let (i, expr) = parse(rule).unwrap();
let val = eval(&expr, &context).unwrap();
assert_eq!(val, true);
assert_eq!(i, "");
}

#[test]
fn scopes_bug_with_new_lines_around_test() {
let rule = r###"
(a=1 or b=2) and ((c=3 or d=4) and e=5)
"###;
let context = HashMap::from([
("a", Atom::Number(1)),
("b", Atom::Number(2)),
("c", Atom::Number(3)),
("d", Atom::Number(4)),
("e", Atom::Number(5)),
]);
let (i, expr) = parse(rule).unwrap();
let val = eval(&expr, &context).unwrap();
assert_eq!(val, true);
assert_eq!(i, "");
}

0 comments on commit 06e100d

Please sign in to comment.