Skip to content

Commit

Permalink
fix(engine): add reserved keywords
Browse files Browse the repository at this point in the history
* add the reserved keywords: "and", "or"
* avoid that a conjunction/disjunction is parsed as function name
* change the parsing behavior to avoid that a function name contains a reserved keyword like "and"/"or"
  • Loading branch information
saig0 committed Feb 9, 2022
1 parent 7d7092e commit 640a37c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ object FeelParser {
"return",
"then",
"else",
"satisfies"
"satisfies",
"and",
"or"
)
).!

Expand Down Expand Up @@ -490,7 +492,7 @@ object FeelParser {

private def functionInvocation[_: P]: P[Exp] =
P(
(identifierWithWhitespaces
((identifierWithWhitespaces | functionNameWithReservedWord)
.map(List(_)) | qualifiedName) ~ "(" ~ functionParameters.? ~ ")"
).map {
case (name :: Nil, None) =>
Expand All @@ -507,6 +509,13 @@ object FeelParser {
parameters)
}

// List all built-in function names that contains a reserved word. These names are not allowed as
// regular function names.
private def functionNameWithReservedWord[_: P]: P[String] =
P(
"and" | "or" | "date and time" | "years and months duration"
).!

private def functionParameters[_: P]: P[FunctionParameters] =
namedParameters | positionalParameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ class InterpreterBooleanExpressionTest
ValBoolean(true))
}

it should "be in conjunction (with parentheses)" in {
eval("x and (y)", Map("x" -> true, "y" -> false)) should be(
ValBoolean(false))

eval("(x) and y", Map("x" -> true, "y" -> false)) should be(
ValBoolean(false))
}

it should "be in disjunction" in {

eval("false or true") should be(ValBoolean(true))
Expand All @@ -91,6 +99,12 @@ class InterpreterBooleanExpressionTest
eval("2 or 4") should be(ValNull)
}

it should "be in disjunction (with parentheses)" in {
eval("x or (y)", Map("x" -> false, "y" -> true)) should be(ValBoolean(true))

eval("(x) or y", Map("x" -> false, "y" -> true)) should be(ValBoolean(true))
}

it should "be in disjunction with comparison" in {
eval("1 = 1 or 1 = 2") should be(ValBoolean(true))
}
Expand Down

0 comments on commit 640a37c

Please sign in to comment.