{"record":{"id":"7bd396be6ccc7812","repo":"apple/pkl","slug":"invalidcharacterescapesequence","errorCode":"invalidCharacterEscapeSequence","errorMessage":"Invalid character escape sequence `{0}`.\n\nValid character escape sequences are:  {1}n  {1}r  {1}t  {1}\"  {1}\\\\ {1}<newline>","messagePattern":"Invalid character escape sequence `(.+?)`\\.\n\nValid character escape sequences are:  (.+?)n  (.+?)r  (.+?)t  (.+?)\"  (.+?)\\\\\\\\ (.+?)<newline>","errorType":"exception","errorClass":"ParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/Lexer.java","lineNumber":480,"sourceCode":"      case 'u' -> lexUnicodeEscape();\n      case '\\n' -> Token.STRING_ESCAPE_CONTINUATION;\n      case '\\r' -> {\n        if (lookahead == '\\n') {\n          nextChar();\n        }\n        yield Token.STRING_ESCAPE_CONTINUATION;\n      }\n      case ' ', '\\t' -> {\n        var c = cursor;\n        var next = nextChar();\n        while (next == ' ' || next == '\\t') next = nextChar();\n        if (next == '\\n' || next == '\\r')\n          throw lexError(\n              ErrorMessages.create(\"invalidLineContinuationEscapeSequenceWhitespace\"),\n              c - 2,\n              cursor - c + 2);\n\n        throw lexError(\n            ErrorMessages.create(\"invalidCharacterEscapeSequence\", \"\\\\\" + (char) ch, \"\\\\\"),\n            c - 2,\n            2);\n      }\n      default ->\n          throw lexError(\n              ErrorMessages.create(\"invalidCharacterEscapeSequence\", \"\\\\\" + (char) ch, \"\\\\\"),\n              cursor - 2,\n              2);\n    };\n  }\n\n  private Token lexUnicodeEscape() {\n    if (lookahead != '{') {\n      throw unexpectedChar(lookahead, \"{\");\n    }\n    do {\n      nextChar();","sourceCodeStart":462,"sourceCodeEnd":498,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/Lexer.java#L462-L498","documentation":"A backslash was followed by a character that does not begin a valid escape, and that character was a space or tab not followed by a newline (ruling out a line continuation). Pkl only allows \\n, \\r, \\t, \\\", \\\\, and \\newline as character escapes, so `\\ ` or `\\<tab>` with other following content is an invalid character escape sequence. The message echoes the offending two-character sequence and lists valid escapes.","triggerScenarios":"nextString → lexEscape, case ' '/'\\t': a `\\` is followed by space(s)/tab(s) and the next non-whitespace character is NOT `\\n`/`\\r` — e.g. `\"a\\ b\"` or `\"a\\ x\"`. The error is reported at the `\\` plus following char (2 chars).","commonSituations":"Trying to escape a space in a regex or path inside a Pkl string (unnecessary — spaces need no escaping); copy-paste from shell snippets where `\\ ` escaped spaces for the shell; typos like `\\/` or `\\'` in a URL string pasted from another language.","solutions":["Remove the backslash — space does not need escaping in Pkl strings: `\"a b\"` not `\"a\\ b\"`.","If a literal backslash is wanted, double it: `\"a\\\\ b\"`.","Use one of the valid escapes (\\n, \\r, \\t, \\\", \\\\, \\newline) as listed in the message.","For a line continuation, ensure nothing but the newline follows the `\\`."],"exampleFix":"// before\npath = \"C:\\ Program Files\"\n// after\npath = \"C:\\\\ Program Files\"","handlingStrategy":"validation","validationCode":"function invalidSpaceEscape(src) { return /\\\\[ \\t]+(?![\\r\\n])/.test(src); }\nif (invalidSpaceEscape(src)) throw new Error('Backslash followed by space/tab is not a valid escape; use \\\\\\\\ or remove the backslash');","typeGuard":"null","tryCatchPattern":"try { tokens = lexer.next(); } catch (e) { if (e.code === 'invalidCharacterEscapeSequence') { console.error('Only \\\\n \\\\r \\\\t \\\\\" \\\\\\\\ \\\\newline are valid'); } throw e; }","preventionTips":["Remember spaces never need escaping in Pkl strings.","Escape regexes/shell snippets inside raw #-strings instead.","Double any literal backslash as \\\\.","Memorize the valid escape set: \\n \\r \\t \\\" \\\\ and \\newline."],"tags":["lexer","pkl","escape-sequence","invalid"],"backgroundTag":"invalid-escape-sequence","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}