Skip to content

Commit

Permalink
refactor: create a custom function to handle special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpuppa committed Nov 9, 2023
1 parent 0e47c22 commit 6747edb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/org/camunda/feel/api/FeelEngineApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class FeelEngineApi(engine: FeelEngine) {
* the result of the evaluation as [[EvaluationResult]]
*/
def evaluateExpression(expression: String, context: Context): EvaluationResult =
engine.parseExpression(expression = StringContext.processEscapes(expression)) match {
engine.parseExpression(expression = expression) match {
case Right(parsedExpression) =>
evaluate(
expression = parsedExpression,
Expand Down
23 changes: 22 additions & 1 deletion src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import scala.util.Try
object FeelParser {

def parseExpression(expression: String): Parsed[Exp] =
parse(expression, fullExpression(_))
parse(translateEscapes(expression), fullExpression(_))

def parseUnaryTests(expression: String): Parsed[Exp] =
parse(expression, fullUnaryExpression(_))
Expand Down Expand Up @@ -688,4 +688,25 @@ object FeelParser {
}
}.getOrElse(ConstNull)
}

private def translateEscapes(input: String): String = {
val escapeMap = Map(
"\\b" -> "\b", // backspace
"\\t" -> "\t", // tab
"\\n" -> "\n", // newline
"\\f" -> "\f", // form feed
"\\r" -> "\r", // carriage return
"\\\"" -> "\"", // double quote
"\\'" -> "'", // single quote
"\\s" -> " " // Space character
// Add more escape sequences as needed
)

var result = input
escapeMap.foreach { case (escape, replacement) =>
result = result.replace(escape, replacement)
}

result
}
}

0 comments on commit 6747edb

Please sign in to comment.