Skip to content

Decode JSON string escapes in one pass in the debug JSON parser - #317

Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/debug-json-escapes-one-pass
Draft

Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/debug-json-escapes-one-pass

Conversation

@federicobrancasi

Copy link
Copy Markdown
Member

With VSCODE_TEXTMATE_DEBUG set (or with the inspect tool from the README, which turns debug mode on), parseRawGrammar reads .json grammars with its own parseJSON instead of JSON.parse. VS Code's built-in C# grammar then fails to load: "begin": "\\A\\u200B" throws Near offset 775: invalid escape sequence. The same happens for any JSON grammar that uses Oniguruma's \uHHHH escape, or that matches a literal \u followed by four hex digits. For example, "\\uFEFF" should parse to the regex \uFEFF, but it throws instead. The decoder in nextJSONToken (src/json.ts) runs two replace passes: the first turns \uXXXX into a character even when its backslash is the second half of an escaped \\, and the second pass then finds a backslash before that character and fails. For the same reason "\u005cn" (a backslash and n) is decoded twice and becomes a newline.

This PR decodes \uXXXX and the other escapes in a single replace, so each backslash is read once, as JSON.parse does.

How to test

Command Result
npx vitest run 267 passed, 1 skipped; the "JSON Strings" test with the new ["\\A\\u200B"] and ["\u005cn"] cases fails without the change
npx tsc passes

With `VSCODE_TEXTMATE_DEBUG` set (or with the `inspect` tool from the README, which turns debug mode on), `parseRawGrammar` reads `.json` grammars with its own `parseJSON` instead of `JSON.parse`. VS Code's built-in C# grammar then fails to load: `"begin": "\\A\\u200B"` throws `Near offset 775: invalid escape sequence`. The same happens for any JSON grammar that uses Oniguruma's `\uHHHH` escape, or that matches a literal `\u` followed by four hex digits. For example, `"\\uFEFF"` should parse to the regex `\uFEFF`, but it throws instead. The decoder in `nextJSONToken` (`src/json.ts`) runs two `replace` passes: the first turns `\uXXXX` into a character even when its backslash is the second half of an escaped `\\`, and the second pass then finds a backslash before that character and fails. For the same reason `"\u005cn"` (a backslash and `n`) is decoded twice and becomes a newline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the single-pass string-escape decoding change.

No open issues 🎉

What changed in this PR

Fixes JSON string decoding in the debug-mode grammar parser so escaped backslashes and Unicode escapes are processed once, preventing valid grammars from failing to load or producing incorrect strings.

Changes:

  • Combines Unicode and other string escape decoding into a single replacement pass.
  • Adds regression cases for regex escapes and a Unicode-encoded backslash followed by n.
File Description
src/​tests/​json.test.ts Adds regression cases for strings previously decoded incorrectly.
src/​json.ts Decodes string escapes in one pass to prevent double decoding.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants