Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chumsky parser #18

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions interpreter/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,11 @@ impl<'a> Value {
let expr = Value::resolve(expr, ctx)?;
match op {
UnaryOp::Not => Value::Bool(!expr.to_bool()),
UnaryOp::DoubleNot => Value::Bool(expr.to_bool()),
UnaryOp::Minus => match expr {
UnaryOp::Neg => match expr {
Value::Int(i) => Value::Int(-i),
Value::Float(i) => Value::Float(-i),
_ => unimplemented!(),
},
UnaryOp::DoubleMinus => match expr {
Value::Int(_) => expr,
Value::UInt(_) => expr,
Value::Float(_) => expr,
_ => unimplemented!(),
},
}
.into()
}
Expand Down
4 changes: 3 additions & 1 deletion parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ name = "cel-parser"
description = "A parser for the Common Expression Language (CEL)"
repository = "https://github.com/clarkmcc/cel-rust"
version = "0.5.0"
authors = ["Tom Forbes <[email protected]>", "Clark McCauley <[email protected]>"]
authors = ["Tom Forbes <[email protected]>", "Clark McCauley <[email protected]>", "Brian Thorne <[email protected]>"]
edition = "2018"
license = "MIT"
categories = ["parsing", "cel"]

[dependencies]
lalrpop-util = { version = "0.19.1", features = ["lexer"] }
regex = "1.4.2"
ariadne = { version = "0.3.0", features = ["auto-color"] }
chumsky = "0.9.2"

[build-dependencies]
lalrpop = { version = "0.19.1", features = ["lexer"] }
4 changes: 1 addition & 3 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ pub enum ArithmeticOp {
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum UnaryOp {
Not,
DoubleNot,
Minus,
DoubleMinus,
Neg,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down
12 changes: 5 additions & 7 deletions parser/src/cel.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ ArithmeticOp: ArithmeticOp = { // (3)

UnaryOp: UnaryOp = {
"!" => UnaryOp::Not,
"!!" => UnaryOp::DoubleNot,
"-" => UnaryOp::Minus,
"--" => UnaryOp::DoubleMinus,
"-" => UnaryOp::Neg,
}

RelationOp: RelationOp = {
Expand All @@ -91,10 +89,10 @@ RelationOp: RelationOp = {

Atom: Atom = {
// Integer literals. Annoying to parse :/
r"-?[0-9]+" => Atom::Int(<>.parse().unwrap()),
r"-?0[xX]([0-9a-fA-F]+)" => Atom::Int(i64::from_str_radix(<>, 16).unwrap()),
r"-?[0-9]+ [uU]" => Atom::UInt(<>.parse().unwrap()),
r"-?0[xX]([0-9a-fA-F]+) [uU]" => Atom::UInt(u64::from_str_radix(<>, 16).unwrap()),
r"[0-9]+" => Atom::Int(<>.parse().unwrap()),
r"0[xX]([0-9a-fA-F]+)" => Atom::Int(i64::from_str_radix(<>, 16).unwrap()),
r"[0-9]+ [uU]" => Atom::UInt(<>.parse().unwrap()),
r"0[xX]([0-9a-fA-F]+) [uU]" => Atom::UInt(u64::from_str_radix(<>, 16).unwrap()),

// Float with decimals and optional exponent
r"([-+]?[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?)" => Atom::Float(<>.parse().unwrap()),
Expand Down
Loading