Skip to content

Support separating single quotes in C++ floating point and integer literals #1886

@nabijaczleweli

Description

@nabijaczleweli

Information

  • Language: C++
  • Plugins: none

Does the problem still occur in the latest version of Prism? Yes.

Description
C++ supports separating single quotes in its floating point and integer literals – e.g. 1'000'000 is equivalent to 1000000.

In this screenshot, top is current highlight and bottom is the correct highlight:
good/wrong SS.

The problem gets more exacerbated for numbers with multiple quotes (values are examples from The International Standard), current on the left and correct on the right:
good/even worse SS

Code snippet

The code being highlighted incorrectly.
auto i = 1'048'576;
auto j = 0x10'0000;
auto k = 0'004'000'000;
auto l = 1.602'176'565e-19;

What I tried

Adding the following to components/prism-cpp.js' extend('c') call:

'number': /(?:\b0x(?:[\da-f']+\.?[\da-f']*|\.[\da-f']+)(?:p[+-]?[\d']+)?|(?:\b[\d']+\.?[\d']*|\B\.[\d']+)(?:e[+-]?[\d']+)?)[ful]*/i

The regex is a the one from components/prism-c.js, modified to include ' in every place where a digit could also be matched.

And adding tests/languages/cpp/separating_single_quotes_feature.test, containing:

1'048'576
0x10'0000
0'004'000'000
1.602'176'565e-19

----------------------------------------------------

[
	["number", "1'048'576"],
	["number", "0x10'0000"],
	["number", "0'004'000'000"],
	["number", "1.602'176'565e-19"]
]

----------------------------------------------------

Checks for the C++ feature of separating single quotes in literals (http://eel.is/c++draft/lex.icon and http://eel.is/c++draft/lex.fcon).

Which, sadly, fails, due to the parts of the numbers being forcibly interpreted as strings, and I don't know how to make it not do that, hence why I'm opening an issue instead of a PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions