highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.31k stars 3.52k forks source link

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

Open jureslak opened 7 months ago

jureslak commented 7 months ago

Describe the issue Integer literals 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 <iostream>
#define INF 1'000'000'000

int main() {
    int a = INF;
    return 0;
}

image

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

Additional context

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

#include <iostream>

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

int main() {
    int a = 1'000'000'000;
    int b = f(1'000);
    return 0;
}
TokenPayId commented 7 months ago

Describe the issue

Integer literals 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 <iostream>

#define INF 1'000'000'000

int main() {

  int a = INF;

    return 0;

}

image

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

Additional context

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


#include <iostream>

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

int main() {

  int a = 1'000'000'000;

    int b = f(1'000);

    return null;

}
TokenPayId commented 7 months ago

Describe the issue

Integer literals 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 <iostream>

#define INF 1'000'000'000

int main() {

  int a = INF;

    return 0;

}

image

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

Additional context

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


#include <iostream>

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

int main() {

  int a = 1'000'000'000;

    int b = f(1'000);

    return null;

}
joshgoebel commented 7 months ago

Can you confirm a fix: https://github.com/highlightjs/highlight.js/pull/3930

jureslak commented 7 months ago

Looks great, thanks!

Before:

image

After:

image

akhtarmdsaad commented 4 months ago

Is this issue resolved?

joshgoebel commented 4 months ago

Not yet, I think I've started this needs to be contextual, so it's a bit more complicated than what we have so far.