Repository navigation
Decode JSON string escapes in one pass in the debug JSON parser - #317
Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
Conversation
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>
Federico Brancasi (federicobrancasi)
requested a balanced review from Copilot
November 29, 2026 11:17
There was a problem hiding this comment.
🟢 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
VSCODE_TEXTMATE_DEBUGset (or with theinspecttool from the README, which turns debug mode on),parseRawGrammarreads.jsongrammars with its ownparseJSONinstead ofJSON.parse. VS Code's built-in C# grammar then fails to load:"begin": "\\A\\u200B"throwsNear offset 775: invalid escape sequence. The same happens for any JSON grammar that uses Oniguruma's\uHHHHescape, or that matches a literal\ufollowed by four hex digits. For example,"\\uFEFF"should parse to the regex\uFEFF, but it throws instead. The decoder innextJSONToken(src/json.ts) runs tworeplacepasses: the first turns\uXXXXinto 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 andn) is decoded twice and becomes a newline.This PR decodes
\uXXXXand the other escapes in a singlereplace, so each backslash is read once, asJSON.parsedoes.How to test
npx vitest run["\\A\\u200B"]and["\u005cn"]cases fails without the changenpx tsc