highlightjs / highlightjs/highlight.js

(C++) Integer literals with quote delimiters in #defines break highlighting

Open
#3,928 11 comments 0 reactions 0 assignees View on GitHub
bug good first issue help welcome language
Dominant language
JavaScript
Stars
25k
Forks
3.8k
Avg merge
11h 35m
Merged PRs (30d)
3

Description

**Describe the issue**
[Integer literals](https://en.cppreference.com/w/cpp/language/integer_literal) in C++ may be written as 1'000'000'000 instead of 1000000000 for greater readability. When this is used inside a `#define`, if breaks highlighting which treats the rest of the code as being inside a string.

**Which language seems to have the issue?**
cpp

**Are you using `highlight` or `highlightAuto`?**
highlight

**Sample Code to Reproduce**

```
#include
#define INF 1'000'000'000

int main() {
int a = INF;
return 0;
}
```
![image](https://github.com/highlightjs/highlight.js/assets/1394590/a747d2b6-53f1-4dd5-ad81-c7735885f9b0)

**Expected behavior**
The token 1'000'000'000 should be highlighted as a number, and the following code should also be highlighted normally, instead of as if it is inside a string:

![image](https://github.com/highlightjs/highlight.js/assets/1394590/4684d8e0-526f-42bf-ae5a-c1501f55b596)

**Additional context**

The snippet below seems to be highlighted fine, so the issue is likely the interplay between quote-separated literals and defines.

```
#include

int f(int x) { return f(2'000); }

int main() {
int a = 1'000'000'000;
int b = f(1'000);
return 0;
}
```

Contributor guide

Open the contributing guide

Research direction

Inspect the C++ grammar used by highlight, not highlightAuto, and reproduce the problem with the provided #define sample. Done means 1'000'000'000 is highlighted as a number, following code receives normal highlighting, and the non-define examples remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.