Skip to content

Commit

Permalink
Expressions: fix unrecognized backslash in string literal
Browse files Browse the repository at this point in the history
Thankfully, this bug was revealed by our test StrLiterals
(https://github.com/kaitai-io/kaitai_struct_tests/blob/3b242b185c15ea4e347711bc2eb7fe9d86b97557/formats/str_literals.ksy#L8-L9):

```ksy
  backslashes:
    value: '"\\\u005c\134"'
```
  • Loading branch information
generalmimon committed Oct 22, 2023
1 parent a3f2783 commit afa7d02
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ object Lexical {
"\\" -> "\\" // backslash
)

def quotedchar[_: P] = P( CharIn("\"'\\abefnrtv").! ).map(QUOTED_CC)
// Note: `CharIn("\\\\")` is necessary to include a single literal backslash,
// because the contents of `CharIn` are translated to a regex character class
// `[...]` (and as in regexes, ranges like `a-z` are also supported, etc.).
// Therefore, to match either `+` or `-` literally, you would need
// `CharIn("+\\-")`; consequently, a literal backslash is `CharIn("\\\\")`.
def quotedchar[_: P] = P( CharIn("\"'\\\\abefnrtv").! ).map(QUOTED_CC)

def quotedoctal[_: P]: P[String] = P( octdigit.rep(1).! ).map { (digits) =>
val code = Integer.parseInt(digits, 8).toChar
Character.toString(code)
Expand Down

0 comments on commit afa7d02

Please sign in to comment.