Skip to content

Commit

Permalink
final parser combined
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 9, 2024
1 parent e16f590 commit cbcfa70
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
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.1.10"
version = "0.1.11"
edition = "2021"

[lints.rust]
Expand Down
5 changes: 3 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ impl LogicOp {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum ArrayOp {
In,
NotIn,
}

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub enum AstNode {
Void,
Variable(Atom),
Constant(Atom),
List(Vec<Atom>),
Expand Down
25 changes: 18 additions & 7 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,21 @@ fn parse_logic_expr(i: &str) -> IResult<&str, AstNode> {
}

fn parse(input: &str) -> IResult<&str, Vec<AstNode>> {
alt((
many0(ws(parse_logic_expr)),
map(parse_compare_or_array_expr, |v| vec![v]),
))(input)
let mut res: Vec<AstNode> = vec![];

let (i, mut head) =
alt((parse_logic_expr, parse_compare_or_array_expr))(input).expect("parse failed");

let (i, tail) =
many0(pair(ws(parse_logic_op), parse_compare_or_array_expr))(i).expect("Parse failed");

for (op, expr) in tail {
head = AstNode::Logic(Box::new(head.clone()), op.clone(), Box::new(expr.clone()));
}

res.push(head.clone());

Ok((i, res))
}

#[cfg(test)]
Expand Down Expand Up @@ -248,11 +259,11 @@ mod tests {
#[test]
fn test_more_complext_not_in() {
assert_eq!(
parse_logic_expr("a=3 && c = 3 || d not in in (2,4,5)").is_ok(),
parse_logic_expr("a=3 && c = 3 || d not in (2,4,5)").is_ok(),
true
);
let (i, v) = parse("a=3 && c = 3 || d not in in (2,4,5)").unwrap();
// dbg!(i, v);
let (i, v) = parse("a=3 && c = 3 || d not in (2,4,5) and this<>34.43").unwrap();
assert_eq!(i, "");
}

#[test]
Expand Down

0 comments on commit cbcfa70

Please sign in to comment.