Skip to content

Commit

Permalink
Added null as a first class token (#29)
Browse files Browse the repository at this point in the history
An expression may evaluate to Null but an equality test evaluates to UndefinedIdentifier
  • Loading branch information
jhugman authored May 25, 2023
2 parents ee044f7 + b28fe78 commit 2dff4e7
Show file tree
Hide file tree
Showing 5 changed files with 1,042 additions and 842 deletions.
2 changes: 2 additions & 0 deletions jexl-eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl<'a> Evaluator<'a> {
Expression::Boolean(b) => Ok(value!(b)),
Expression::String(s) => Ok(value!(s)),
Expression::Array(xs) => xs.into_iter().map(|x| self.eval_ast(*x, context)).collect(),
Expression::Null => Ok(Value::Null),

Expression::Object(items) => {
let mut map = serde_json::Map::with_capacity(items.len());
Expand Down Expand Up @@ -834,6 +835,7 @@ mod tests {
test("BOOLEAN", "true", true);
test("OBJECT", "OBJECT", true);
test("ARRAY", "[ 'string' ]", true);
test("NULL", "null", true);

test("OBJECT", "{ 'x': 1, 'y': 2 }", false);

Expand Down
1 change: 1 addition & 0 deletions jexl-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Expression {
Array(Vec<Box<Expression>>),
Object(Vec<(String, Box<Expression>)>),
Identifier(String),
Null,

BinaryOperation {
operation: OpCode,
Expand Down
22 changes: 22 additions & 0 deletions jexl-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ mod tests {
);
}

#[test]
fn test_dot_op_equality_with_null() {
let exp = "foo.bar == null";
let parsed = Parser::parse(exp).unwrap();
assert_eq!(
parsed,
Expression::BinaryOperation {
operation: OpCode::Equal,
left: Box::new(Expression::DotOperation {
subject: Box::new(Expression::Identifier("foo".to_string())),
ident: "bar".to_string()
}),
right: Box::new(Expression::Null),
}
);
}

#[test]
fn test_dot_op_object_literal() {
let exp = "{'foo': 1}.foo";
Expand All @@ -151,4 +168,9 @@ mod tests {
}
);
}

#[test]
fn test_parsing_null() {
assert_eq!(Parser::parse("null"), Ok(Expression::Null));
}
}
4 changes: 4 additions & 0 deletions jexl-parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Expr80: Box<Expression> = {
String => Box::new(Expression::String(<>)),
Array => Box::new(Expression::Array(<>)),
Object => Box::new(Expression::Object(<>)),
Null => Box::new(Expression::Null),
Identifier => Box::new(Expression::Identifier(<>)),
"(" <Expression> ")",
};
Expand Down Expand Up @@ -126,6 +127,9 @@ String: String = {
<s: r#"'([^'\\]*(\\')?)*'"#> => s[1..s.len() - 1].to_string().replace("\\'", "'"),
};

Null: Option<Box<Expression>> = {
"null" => None,
}

Identifier: String = {
r#"[a-zA-Z_][a-zA-Z0-9_]*"# => <>.to_string()
Expand Down
Loading

0 comments on commit 2dff4e7

Please sign in to comment.