Skip to content

Add C and C++ syntax highlighting - #231

Merged
brendt merged 2 commits into
tempestphp:mainfrom
eas4ai:add-c-cpp-highlighting
Oct 26, 2026
Merged

brendt merged 2 commits into
tempestphp:mainfrom
eas4ai:add-c-cpp-highlighting

Conversation

@eas4ai

@eas4ai eas4ai commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Register c for C and cpp / c++ for C++
  • Highlight comments, preprocessor directives and includes, strings and raw strings, character literals, numbers, keywords, built-in and declared types, literals, namespaces, classes, templates, functions, operators, and punctuation
  • Shared C-like comment, include, function, and operator patterns live under src/Languages/CLike/
  • Add focused tests and benchmark fixtures for both languages

This is a clean resubmission of the closed #223, based directly on main and independent of the other language PRs: no core changes and no visual test harness.

Verification

  • vendor/bin/php-cs-fixer fix --allow-risky=yes — no remaining changes
  • vendor/bin/phpstan analyse src tests — no errors
  • vendor/bin/phpunit — 884 tests, 1,159 assertions
  • vendor/bin/phpbench run tests/Bench/HighlighterBench.php --variant='^(c|cpp)$' --report=aggregate — C 123.62 μs, C++ 189.38 μs

Screenshot

C++ syntax highlighting in dark mode

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Benchmark Results

Comparison of add-c-cpp-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.47% 52.910548μs -0.31% ±0.82% -19.11%
HighlighterBench benchParse bash 100 5 2.136mb +0.46% 151.735734μs -1.53% ±2.15% +111.34%
HighlighterBench benchParse bbcode 100 5 2.092mb +0.47% 54.335969μs -0.65% ±0.45% -17.02%
HighlighterBench benchParse blade 100 5 2.775mb +0.36% 759.545949μs +1.01% ±1.98% +335.94%
HighlighterBench benchParse c 100 5 ERR 120.331233μs +0.00% ±0.63% +0.00%
HighlighterBench benchParse cpp 100 5 ERR 184.344286μs +0.00% ±0.35% +0.00%
HighlighterBench benchParse css 100 5 2.123mb +0.47% 104.505284μs -0.73% ±0.50% +22.03%
HighlighterBench benchParse diff 100 5 2.127mb +0.47% 66.055871μs +0.05% ±0.70% -43.19%
HighlighterBench benchParse dockerfile 100 5 2.097mb +0.47% 62.935871μs -1.27% ±0.98% -16.33%
HighlighterBench benchParse dotenv 100 5 2.108mb +0.47% 57.030372μs -0.52% ±0.31% -86.29%
HighlighterBench benchParse gdscript 100 5 2.203mb +0.45% 246.704462μs -0.27% ±0.62% -70.31%
HighlighterBench benchParse graphql 100 5 2.123mb +0.47% 111.756301μs +0.42% ±0.33% -30.39%
HighlighterBench benchParse html 100 5 2.127mb +0.47% 84.665342μs +0.68% ±0.93% +33.86%
HighlighterBench benchParse http 100 5 2.096mb +0.47% 17.549961μs -1.89% ±0.54% -0.78%
HighlighterBench benchParse ini 100 5 2.112mb +0.47% 64.662603μs +1.47% ±1.86% +273.99%
HighlighterBench benchParse javascript 100 5 2.191mb +0.45% 203.697652μs +0.02% ±0.55% +136.48%
HighlighterBench benchParse json 100 5 2.166mb +0.46% 175.466791μs -2.42% ±0.38% -85.52%
HighlighterBench benchParse markdown 100 5 2.114mb +0.47% 55.224697μs +0.06% ±0.78% -24.52%
HighlighterBench benchParse nginx 100 5 2.178mb +0.45% 198.542720μs -2.09% ±0.58% -42.97%
HighlighterBench benchParse php 100 5 2.469mb +0.40% 606.363679μs -0.36% ±1.48% +259.50%
HighlighterBench benchParse python 100 5 2.367mb +0.42% 327.760176μs +0.73% ±0.92% +1.65%
HighlighterBench benchParse ruby 100 5 2.413mb +0.41% 175.808630μs -0.12% ±1.70% +416.73%
HighlighterBench benchParse scss 100 5 2.139mb +0.46% 135.990352μs -0.32% ±0.97% +495.18%
HighlighterBench benchParse sema 100 5 2.307mb +0.43% 164.859589μs -0.42% ±2.48% +498.82%
HighlighterBench benchParse sql 100 5 2.138mb +0.46% 115.635284μs -0.43% ±0.16% -62.01%
HighlighterBench benchParse svelte 100 5 2.412mb +0.41% 834.852622μs +0.17% ±1.19% +171.23%
HighlighterBench benchParse swift 100 5 2.185mb +0.45% 187.740881μs +0.53% ±0.58% +8.95%
HighlighterBench benchParse terminal 100 5 2.133mb +0.46% 137.700548μs -0.09% ±0.57% +92.07%
HighlighterBench benchParse terraform 100 5 2.138mb +0.46% 128.166810μs -0.09% ±0.38% -77.09%
HighlighterBench benchParse typescript 100 5 2.563mb +0.39% 186.966086μs +0.17% ±2.06% +118.49%
HighlighterBench benchParse txt 100 5 2.067mb +0.48% 6.214932μs -1.71% ±2.69% +73.62%
HighlighterBench benchParse twig 100 5 19.245mb +0.05% 1,114.123268μs -0.39% ±2.01% +152.53%
HighlighterBench benchParse vue 100 5 2.420mb +0.41% 312.237808μs -0.02% ±0.58% -44.73%
HighlighterBench benchParse xml 100 5 2.141mb +0.46% 131.564384μs -0.26% ±0.31% -64.71%
HighlighterBench benchParse yaml 100 5 2.199mb +0.45% 238.953699μs +0.03% ±0.39% +51.51%
HighlighterBench benchParse jsx 100 5 2.135mb +0.46% 37.440607μs +0.07% ±1.67% +109.43%
HighlighterBench benchParse tsx 100 5 2.219mb +0.45% 54.707945μs -0.02% ±1.86% +113.68%

Generated by phpbench against commit fc017f2

@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 15 C, C++, and shared C-like pattern classes. The new C++ type case also exposed class being matched as a type in class Point {}; the generic type-use alternative now excludes declaration keywords, with Point covered by the pattern test.

Verified locally before pushing: vendor/bin/phpunit — 918 tests, 1,193 assertions; vendor/bin/phpstan analyse src tests — no errors; PHP-CS-Fixer clean.

@eas4ai
eas4ai force-pushed the add-c-cpp-highlighting branch from 7329251 to b55abb6 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 C and C++ changes and pattern tests are unchanged apart from fitting the updated registration and benchmark lists.

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

@eas4ai
eas4ai force-pushed the add-c-cpp-highlighting branch from b55abb6 to fc017f2 Compare October 20, 2026 10:39
@brendt

brendt commented Oct 26, 2026

Copy link
Copy Markdown
Member

Thanks!

@brendt
brendt merged commit c336c76 into tempestphp:main Oct 26, 2026
5 checks passed
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