Skip to content

Commit

Permalink
fix: Handle escape sequences
Browse files Browse the repository at this point in the history
Correct the handling of escape sequences in string literals.

Don't replace escape sequences in regex expressions, for example, \r or \n. In the parser, these sequences start with \\. Same for \s, don't replace it with a whitespace, since this is also a part of a regex.

Handle \\ to avoid that the sequence is escaped and returned as \\\\.
  • Loading branch information
saig0 committed Sep 24, 2024
1 parent 7245daf commit d4702cd
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -694,22 +694,16 @@ object FeelParser {
}.getOrElse(ConstNull)
}

// replace escaped character with the provided replacement
private def translateEscapes(input: String): String = {
val escapeMap = Map(
"\\b" -> "\b",
"\\t" -> "\t",
"\\n" -> "\n",
"\\f" -> "\f",
"\\r" -> "\r",
"\\\"" -> "\"",
"\\'" -> "'",
"\\s" -> " "
// Add more escape sequences as needed
)

escapeMap.foldLeft(input) { case (result, (escape, replacement)) =>
result.replace(escape, replacement)
}
// replace all escape sequences
input
.replaceAll("(?<!\\\\)\\\\n", "\n") // new line
.replaceAll("(?<!\\\\)\\\\r", "\r") // carriage return
.replaceAll("(?<!\\\\)\\\\t", "\t") // tab
.replaceAll("(?<!\\\\)\\\\b", "\b") // backspace
.replaceAll("(?<!\\\\)\\\\f", "\f") // form feed
.replaceAll("(?<!\\\\)\\\\'", "'") // single quote
.replaceAll("(?<!\\\\)\\\\\"", "\"") // double quote
.replaceAll("\\\\\\\\", "\\\\") // backslash (for regex characters)
}
}

0 comments on commit d4702cd

Please sign in to comment.