Skip to content

Match vscode-textmate's tokenization rules more closely - #155

Merged
ryangjchandler merged 1 commit into
3.xfrom
pcre-compat
Nov 4, 2026
Merged

ryangjchandler merged 1 commit into
3.xfrom
pcre-compat

Conversation

@ryangjchandler

Copy link
Copy Markdown
Collaborator

Most of the differences between Phiki and vscode-textmate in COMPATIBILITY.md were caused by the tokenizer and grammar parser, not by the regex engines. This PR fixes them, so both engines get closer to vscode-textmate.

Engine Grammars identical Matching tokens
PCRE 163 → 219 of 227 96.2% → 99.9%
Oniguruma 113 → 152 of 227 93.1% → 97.1%

Fixes

  • Repositories inside rules: #name includes now look in the enclosing rule's repository first, as vscode-textmate does. Objective-C, Objective-C++, Wikitext, Clarity and Cadence use these heavily.
  • applyEndPatternLast is now supported. 21 bundled grammars use it, including Groovy, GraphQL, Markdown frontmatter, Fortran and Marko.
  • Back-references: \1 was removed from every match and begin pattern. The text substituted into end patterns wasn't escaped, and while patterns never got the begin captures.
  • Injection selectors: the parser stopped after the first group, so (a | b) (c | d) - e matched like (a | b). It also treated - inside a scope name such as vue-html as an exclusion, and evaluated | and - left to right. Selectors are now tokenized and grouped the way vscode-textmate does it. This is why Svelte and Astro embedded the wrong language.
  • Rules with missing patterns: like vscode-textmate, a begin/end or begin/while rule whose patterns all include rules or grammars that don't exist is now left out. This fixed Lean, Ara, Jison, Nextflow, Hurl and Haml. The result is computed once per rule, and tokenizing all samples with PCRE is slightly faster than before (17.2s, previously 19.3s).
  • Smaller tokenizer and parser fixes:
    • ${n:/downcase} scope names always used capture 0.
    • begin/while captures after a gap were dropped.
    • A capture inside a lookahead lost its scope.
    • Captures after multibyte text were retokenized past their end.
    • Empty names, null captures, and empty or missing end patterns are now handled the way vscode-textmate handles them.
  • Oniguruma engine: it mixed the byte offsets from mbregex with character offsets, which corrupted tokens after multibyte text (the bsl sample). It also treated a capture of "0" as unmatched.

Behaviour change to review

A begin without an end used to be treated as a match. It is now a region that never closes, as in vscode-textmate. Only the xml and blade grammars have such rules.

Captures written as a bare string ("1": "name") still get their name. vscode-textmate ignores them, so Stata keeps a small difference.

Remaining PCRE differences

  • racket: one 75 KB regex is too large for PCRE2.
  • markdown: the strikethrough regex needs a variable-length lookbehind with a back-reference.
  • cpp: it includes cpp-macro. That file is bundled but isn't a Grammar case, so Phiki can't resolve it by scope.
  • git-rebase, coffee, shellsession: this comes from the reference script, not from Phiki. meta/vscode-textmate-samples.js doesn't load grammars that are only referenced from captures. Phiki does tokenize them.
  • stata: the bare-string captures above.
  • swift: two characters inside a regex conditional.

Tests

  • A dataset in TokenizerTest covers each tokenizer fix with both engines. I checked every expected value against vscode-textmate, and all but one case fail without these changes.
  • InjectionParserTest covers the selector fixes.
  • COMPATIBILITY.md was regenerated with composer compatibility.

Most differences from vscode-textmate came from the tokenizer and grammar
parser rather than the regex engines:

- Resolve `#name` includes against the repository of an enclosing rule.
- Support `applyEndPatternLast`.
- Keep back-references in match and begin patterns, escape the text
  substituted into end patterns, and resolve them in while patterns.
- Parse injection selectors like vscode-textmate: neighbouring operands are
  joined with "and", `-` inside a scope name is not an exclusion, and
  exclusions bind more tightly than `|`.
- Leave out begin/end and begin/while rules whose patterns all include
  rules or grammars that don't exist.
- Fix `${n:/downcase}` scope names, begin/while captures after a gap,
  captures inside a lookahead, and retokenizing captures after multibyte
  text.
- Treat empty names, null captures, and empty or missing ends the way
  vscode-textmate does.
- Use byte offsets throughout the Oniguruma engine.

PCRE now matches vscode-textmate for 219 of 227 grammars (from 163), and
Oniguruma for 152 (from 113). Regenerate COMPATIBILITY.md.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1a80f-8c39-72fb-be3a-ae1b9ab154f7
Co-authored-by: Ryan Chandler <ryan@ryangjchandler.co.uk>
@ryangjchandler
ryangjchandler merged commit 0dafd64 into 3.x Nov 4, 2026
3 checks passed
@ryangjchandler
ryangjchandler deleted the pcre-compat branch November 4, 2026 20:59
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