Skip to content

Commit

Permalink
adding parsing lists
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 8, 2024
1 parent 53038d2 commit 6fc0df7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl LogicOp {
pub enum AstNode {
Variable(Atom),
Constant(Atom),
List(Vec<Atom>),
Compare(Box<AstNode>, ComparisonOp, Box<AstNode>),
Logic(Box<AstNode>, LogicOp, Box<AstNode>),
}
25 changes: 23 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nom::{
character::complete::{alpha1, alphanumeric1, digit1, multispace0},
combinator::{map, opt, recognize},
error::ParseError,
multi::{many0, many0_count},
multi::{many0, many0_count, separated_list0},
number::complete::double,
sequence::{delimited, pair, tuple},
IResult,
Expand Down Expand Up @@ -55,8 +55,8 @@ fn parse_atom(i: &str) -> IResult<&str, Atom> {
alt((
parse_string,
parse_boolean,
parse_number,
parse_float,
parse_number,
parse_variable,
))(i)
}
Expand All @@ -79,6 +79,15 @@ fn parse_logic_op(i: &str) -> IResult<&str, LogicOp> {
))(i)
}

fn parse_list(i: &str) -> IResult<&str, AstNode> {
let parser = delimited(
tag("("),
separated_list0(tag(","), ws(parse_atom)),
tag(")"),
);
map(parser, AstNode::List)(i)
}

fn parse_compare_expr(i: &str) -> IResult<&str, AstNode> {
let parser = tuple((parse_variable, ws(parse_comparison_op), parse_atom));
map(parser, |(var, op, val)| {
Expand Down Expand Up @@ -120,6 +129,12 @@ mod tests {
assert_eq!(v, Atom::Number(199));
}

#[test]
fn test_float() {
let (_,v) = parse_atom("3.14").unwrap();
assert_eq!(v, Atom::Float(3.14));
}

#[test]
fn test_parse_string() {
let (_, v) = parse_string("\"this is demo\"").unwrap();
Expand Down Expand Up @@ -159,4 +174,10 @@ mod tests {
let (i, v) = parse_logic_expr("_demo >= 10 && demo == \"something more than that\"").unwrap();
assert_eq!(i, "");
}

#[test]
fn test_parse_list() {
let (i, v) = parse_list("(1,2, 34, \"demo\", -10, -3.14)").unwrap();
assert_eq!(i, "");
}
}

0 comments on commit 6fc0df7

Please sign in to comment.