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 da3569a commit 30ed9c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 62 deletions.
74 changes: 12 additions & 62 deletions src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,66 +34,7 @@ import fastparse.{
StringIn,
parse
}
import org.camunda.feel.syntaxtree.{
Addition,
ArithmeticNegation,
AtLeastOne,
ClosedConstRangeBoundary,
Conjunction,
ConstBool,
ConstContext,
ConstDate,
ConstDateTime,
ConstDayTimeDuration,
ConstInputValue,
ConstList,
ConstLocalDateTime,
ConstLocalTime,
ConstNull,
ConstNumber,
ConstRange,
ConstString,
ConstTime,
ConstYearMonthDuration,
Disjunction,
Division,
Equal,
EveryItem,
Exp,
Exponentiation,
Filter,
For,
FunctionDefinition,
FunctionInvocation,
FunctionParameters,
GreaterOrEqual,
GreaterThan,
If,
In,
InputEqualTo,
InputGreaterOrEqual,
InputGreaterThan,
InputInRange,
InputLessOrEqual,
InputLessThan,
InstanceOf,
IterationContext,
JavaFunctionInvocation,
LessOrEqual,
LessThan,
Multiplication,
NamedFunctionParameters,
Not,
OpenConstRangeBoundary,
PathExpression,
PositionalFunctionParameters,
QualifiedFunctionInvocation,
ConstRangeBoundary,
Ref,
SomeItem,
Subtraction,
UnaryTestExpression
}
import org.camunda.feel.syntaxtree._
import org.camunda.feel.{
Date,
isOffsetDateTime,
Expand Down Expand Up @@ -147,7 +88,9 @@ object FeelParser {
"return",
"then",
"else",
"satisfies"
"satisfies",
"and",
"or"
)
).!

Expand Down Expand Up @@ -490,7 +433,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 +450,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 30ed9c6

Please sign in to comment.