Skip to content

Add Rust syntax highlighting - #229

Open
eas4ai wants to merge 3 commits into
tempestphp:mainfrom
eas4ai:add-rust-highlighting
Open

eas4ai wants to merge 3 commits into
tempestphp:mainfrom
eas4ai:add-rust-highlighting

Conversation

@eas4ai

@eas4ai eas4ai commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Register rust and rs
  • Highlight Rust comments, attributes, strings, characters, lifetimes, numbers, keywords, types, macros, functions, operators, and punctuation
  • Add focused tests and a benchmark fixture

This is a clean resubmission of the closed #221, based directly on main. It follows the review there: no core changes (Escape, Highlighter::parse, and the diff injections are untouched apart from registering the language), no visual test harness, attributes match only the #[path prefix so a ] inside a string no longer duplicates rendered text, and possessive quantifiers avoid PCRE backtracking errors on unterminated block comments and long whitespace runs.

Verification

  • composer validate --strict
  • vendor/bin/php-cs-fixer fix --allow-risky=yes — no remaining changes
  • vendor/bin/phpstan analyse src tests — no errors
  • vendor/bin/phpunit — 858 tests, 1,121 assertions
  • vendor/bin/phpbench run tests/Bench/HighlighterBench.php --variant='^rust$' --report=aggregate — 143.79 μs (±0.57%)

Screenshot

Rust syntax highlighting in dark mode

@eas4ai

eas4ai commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up commit after an independent review pass over this branch:

  • Escaped char literals ('\u{41}', '\x41') and non-ASCII chars ('é') now highlight as values.
  • Turbofish and nested generics (collect::<Vec<_>>(), fn foo<T: Iterator<Item = Vec<i32>>>(), Fn(i32) -> i32 bounds) now highlight the function name, without flagging comparison operands such as a in a < b && c > (d).
  • C strings (c"hello") keep their prefix inside the value span.
  • Capitalized identifier tails (_Foo, my_String) are no longer colored as types.
  • Regression tests added for each case, plus nested unterminated-comment and long-whitespace backtracking guards.

Full suite after the change: 868 tests, 1,133 assertions; PHPStan and PHP-CS-Fixer clean.

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Benchmark Results

Comparison of add-rust-highlighting against main (3d96482ee77208c8e445d4f68667918c7f440f62).

Open to see the benchmark results
benchmark subject set revs its mem_peak mode rstdev
HighlighterBench benchParse apache 100 5 2.105mb +0.32% 54.940685μs -5.76% ±3.19% +22.17%
HighlighterBench benchParse bash 100 5 2.136mb +0.32% 168.272094μs -0.39% ±1.68% +50.03%
HighlighterBench benchParse bbcode 100 5 2.092mb +0.33% 65.047710μs +16.66% ±0.42% -68.87%
HighlighterBench benchParse blade 100 5 2.775mb +0.25% 799.501722μs -0.89% ±2.62% +50.93%
HighlighterBench benchParse css 100 5 2.123mb +0.32% 106.357221μs -4.06% ±2.43% -19.33%
HighlighterBench benchParse diff 100 5 2.127mb +0.32% 71.857495μs +2.68% ±2.07% -26.45%
HighlighterBench benchParse dockerfile 100 5 2.097mb +0.33% 67.507847μs +1.57% ±0.65% -63.39%
HighlighterBench benchParse dotenv 100 5 2.108mb +0.32% 61.115049μs +1.57% ±2.99% +54.74%
HighlighterBench benchParse gdscript 100 5 2.203mb +0.31% 256.669374μs -1.66% ±2.65% +26.77%
HighlighterBench benchParse graphql 100 5 2.123mb +0.32% 120.531292μs +2.30% ±2.61% +32.47%
HighlighterBench benchParse html 100 5 2.127mb +0.32% 89.024677μs -2.54% ±2.71% +10.88%
HighlighterBench benchParse http 100 5 2.096mb +0.33% 18.127945μs -2.71% ±2.12% -13.42%
HighlighterBench benchParse ini 100 5 2.112mb +0.32% 65.728826μs -0.48% ±0.73% -39.44%
HighlighterBench benchParse javascript 100 5 2.191mb +0.31% 225.767123μs +4.48% ±1.34% -46.85%
HighlighterBench benchParse json 100 5 2.166mb +0.32% 182.570861μs +2.31% ±1.97% +3.24%
HighlighterBench benchParse markdown 100 5 2.114mb +0.32% 59.782994μs -1.34% ±2.09% -23.61%
HighlighterBench benchParse nginx 100 5 2.178mb +0.31% 210.212427μs -2.07% ±3.22% +31.88%
HighlighterBench benchParse php 100 5 2.469mb +0.28% 652.013757μs +0.31% ±1.21% -47.51%
HighlighterBench benchParse python 100 5 2.367mb +0.29% 344.329589μs +2.09% ±2.89% +2.62%
HighlighterBench benchParse ruby 100 5 2.413mb +0.28% 189.451292μs +6.17% ±2.90% +106.07%
HighlighterBench benchParse rust 100 5 ERR 143.906947μs +0.00% ±2.83% +0.00%
HighlighterBench benchParse scss 100 5 2.139mb +0.32% 155.650313μs +7.98% ±2.66% -4.59%
HighlighterBench benchParse sema 100 5 2.307mb +0.30% 184.709628μs +4.62% ±1.86% -35.50%
HighlighterBench benchParse sql 100 5 2.138mb +0.32% 123.409589μs -4.28% ±1.66% -35.31%
HighlighterBench benchParse svelte 100 5 2.412mb +0.28% 891.751409μs +0.10% ±2.19% +3.07%
HighlighterBench benchParse swift 100 5 2.185mb +0.31% 199.967143μs -0.11% ±1.66% -39.77%
HighlighterBench benchParse terminal 100 5 2.133mb +0.32% 139.523151μs -7.68% ±1.79% -28.03%
HighlighterBench benchParse terraform 100 5 2.138mb +0.32% 134.841272μs -4.62% ±2.45% +30.81%
HighlighterBench benchParse typescript 100 5 2.563mb +0.27% 193.779374μs +2.81% ±1.53% -27.27%
HighlighterBench benchParse txt 100 5 2.067mb +0.33% 6.357691μs -0.30% ±1.67% -32.70%
HighlighterBench benchParse twig 100 5 19.245mb +0.04% 1,215.105245μs +4.67% ±1.82% +79.93%
HighlighterBench benchParse vue 100 5 2.420mb +0.28% 339.289961μs +2.27% ±1.50% +412.40%
HighlighterBench benchParse xml 100 5 2.141mb +0.32% 135.876047μs -2.88% ±0.83% -42.15%
HighlighterBench benchParse yaml 100 5 2.199mb +0.31% 264.005049μs +6.03% ±3.19% +49.36%
HighlighterBench benchParse jsx 100 5 2.135mb +0.32% 38.984579μs +4.07% ±1.13% -18.86%
HighlighterBench benchParse tsx 100 5 2.219mb +0.31% 59.665205μs +7.93% ±2.96% +17.98%

Generated by phpbench against commit 98ad83f

@brendt brendt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add pattern tests please? Look at any other language pattern to see how they work.

@eas4ai

eas4ai commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Added #[PatternTest] attributes to all 13 Rust pattern classes (the character pattern already had two; it now has additional basic, escape, and byte-char cases too).

Verified locally after the change: vendor/bin/phpunit passes (897 tests, 1,162 assertions) and vendor/bin/phpstan analyse src tests reports no errors.

@eas4ai
eas4ai force-pushed the add-rust-highlighting branch from 2894666 to 7e70828 Compare October 20, 2026 10:35
@eas4ai

eas4ai commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main to resolve the conflicts in src/Highlighter.php and tests/Bench/HighlighterBench.php after the recent language merges. The Rust changes and pattern tests are unchanged apart from fitting the updated registration and benchmark lists.

Verified locally after the rebase: vendor/bin/phpunit — 1193 tests, 1715 assertions; PHPStan reports no errors. GitHub now reports this PR as mergeable with all checks passing.

@eas4ai
eas4ai force-pushed the add-rust-highlighting branch from 7e70828 to 98ad83f Compare October 20, 2026 10:39
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