feat: use oniguruma-parser/optimizer for all grammars #133
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.
Closes #125.
Introduces a fancy Oniguruma regex optimizer that not only minifies more robustly (e.g., C++ drops more than 35,000 characters compared to the existing minification), but also improves performance of some regexes (for both the Oniguruma and JS engines in Shiki). C++ is probably the biggest beneficiary of these performance improvements because of some of the monster regexes it includes that are able to be significantly improved by the optimizer.
Changes in the PR:
scripts/grammars/cleanup.ts
.compressRegExp
.oniguruma-parser/optimizer
, behind a try/catch.injections
to traversal of grammars, to match a similar update @antfu made in Shiki that seems like it's also appropriate here (see shikijs/shiki@3722d2f).pnpm run fetch:force
.packages/tm-grammars/grammar-syntaxes.md
, which was outdated.Also, it seems like the the previous minification was inappropriately modifying some regexes. E.g., with the ActionScript 3 grammar...
Raw version:
Old minified version:
With the new optimizer:
The old version changed
[A-Za-z0-9_$]
to\w
, which is inappropriate since Oniguruma's\w
is Unicode-based by default and doesn't include$
. Or maybe this was legacy processing that has already been removed but the grammar hasn't since been force-updated?Regardless, the old version prevents correctly highlighting class names that include "$". So this PR will also fix highlighting bugs like this that were the result of prior, incorrect minification.
Aside: Oniguruma's
\w
is equivalent to[\p{L}\p{M}\p{N}\p{Pc}]
(different than POSIX\p{Word}
/[:word:]
, which are equivalent to[\p{Alpha}\p{M}\p{Nd}\p{Pc}]
!). If the new optimizer sees that first set of Unicode categories in a character class, it indeed collapses them to\w
.