Skip to content

fix(cpp): highlight integer literals with digit separators in #define - #4547

Open
fguzman-stack wants to merge 1 commit into
highlightjs:mainfrom
fguzman-stack:fix/cpp-preprocessor-digit-separators
Open

fguzman-stack wants to merge 1 commit into
highlightjs:mainfrom
fguzman-stack:fix/cpp-preprocessor-digit-separators

Conversation

@fguzman-stack

Copy link
Copy Markdown

Problem

C++ integer literals that use digit separators (e.g. 1'000'000'000) break highlighting when written inside a preprocessor directive such as #define.

#define INF 1'000'000'000

int main() { return 0; }

The PREPROCESSOR mode only lists STRINGS in its contains, so the ' in 1'000'000'000 is matched by the character-literal string rule. It pairs up greedily ('0') and leaves an unterminated quote, so everything after the directive gets swallowed as a hljs-string. See #3928.

Fix

Add a separator-only number rule (PREPROCESSOR_NUMBERS) to the PREPROCESSOR mode so that a numeric literal containing at least one ' is consumed as a number before STRINGS can claim it.

This is deliberately narrow: plain numbers inside #define are still not highlighted, which preserves the existing behavior asserted by the preprocessor tests (e.g. while (0), #define foo 1<<16). It only fixes the case that was actually broken.

Tests

Added test/markup/cpp/preprocessor-numbers covering binary / hex / octal / decimal separator literals inside #define, plus following code that must stay un-swallowed.

  • ONLY_LANGUAGES=cpp npm run test-markup -> 20 passing.
  • With the fix removed, preprocessor-numbers fails and the other 19 still pass (confirms it is a real regression guard).
  • eslint --no-eslintrc -c .eslintrc.lang.js src/languages/cpp.js -> clean.

Closes #3928

C++ digit separators (e.g. 1'000'000'000) inside a preprocessor directive
were swallowed by the character-literal string mode, leaving an unterminated
apostrophe that treated the rest of the line as a string.

Add a separator-only number rule to the PREPROCESSOR mode so these literals
are highlighted and no longer break the following lines, while keeping the
existing behaviour of not highlighting plain numbers in #define directives.

Closes highlightjs#3928

Assisted-by: Claude Sonnet
Signed-off-by: fguzman-stack <222879315+fguzman-stack@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant