Skip to content

Commit

Permalink
fix(cpp) improve number literals in C++ (#3847)
Browse files Browse the repository at this point in the history
  • Loading branch information
leduyquang753 committed Nov 8, 2023
1 parent 5bb7114 commit fdd24e9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Core Grammars:

- fix(cpp) not all kinds of number literals are highlighted correctly [Lê Duy Quang][]
- fix(css) fix overly greedy pseudo class matching [Bradley Mackey][]
- enh(arcade) updated to ArcGIS Arcade version 1.24 [Kristian Ekenes][]
- fix(rust) fix escaped double quotes in string [Mohamed Ali][]
Expand All @@ -17,6 +18,7 @@ Developer Tool:

- enh(tools): order CSS options picklist [David Schach][]

[Lê Duy Quang]: https://github.com/leduyquang753
[Mohamed Ali]: https://github.com/MohamedAli00949
[Bradley Mackey]: https://github.com/bradleymackey
[Kristian Ekenes]: https://github.com/ekenes
Expand Down
41 changes: 38 additions & 3 deletions src/languages/cpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,44 @@ export default function(hljs) {
const NUMBERS = {
className: 'number',
variants: [
{ begin: '\\b(0b[01\']+)' },
{ begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)' },
{ begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
// Floating-point literal.
{ begin:
"[+-]?(?:" // Leading sign.
// Decimal.
+ "(?:"
+"[0-9](?:'?[0-9])*\\.(?:[0-9](?:'?[0-9])*)?"
+ "|\\.[0-9](?:'?[0-9])*"
+ ")(?:[Ee][+-]?[0-9](?:'?[0-9])*)?"
+ "|[0-9](?:'?[0-9])*[Ee][+-]?[0-9](?:'?[0-9])*"
// Hexadecimal.
+ "|0[Xx](?:"
+"[0-9A-Fa-f](?:'?[0-9A-Fa-f])*(?:\\.(?:[0-9A-Fa-f](?:'?[0-9A-Fa-f])*)?)?"
+ "|\\.[0-9A-Fa-f](?:'?[0-9A-Fa-f])*"
+ ")[Pp][+-]?[0-9](?:'?[0-9])*"
+ ")(?:" // Literal suffixes.
+ "[Ff](?:16|32|64|128)?"
+ "|(BF|bf)16"
+ "|[Ll]"
+ "|" // Literal suffix is optional.
+ ")"
},
// Integer literal.
{ begin:
"[+-]?\\b(?:" // Leading sign.
+ "0[Bb][01](?:'?[01])*" // Binary.
+ "|0[Xx][0-9A-Fa-f](?:'?[0-9A-Fa-f])*" // Hexadecimal.
+ "|0(?:'?[0-7])*" // Octal or just a lone zero.
+ "|[1-9](?:'?[0-9])*" // Decimal.
+ ")(?:" // Literal suffixes.
+ "[Uu](?:LL?|ll?)"
+ "|[Uu][Zz]?"
+ "|(?:LL?|ll?)[Uu]?"
+ "|[Zz][Uu]"
+ "|" // Literal suffix is optional.
+ ")"
// Note: there are user-defined literal suffixes too, but perhaps having the custom suffix not part of the
// literal highlight actually makes it stand out more.
}
],
relevance: 0
};
Expand Down
64 changes: 54 additions & 10 deletions test/markup/cpp/number-literals.expect.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
<span class="hljs-comment">/* digit separators */</span>
<span class="hljs-type">int</span> number = <span class="hljs-number">2&#x27;555&#x27;555&#x27;555</span>; <span class="hljs-comment">// digit separators</span>
<span class="hljs-type">float</span> exponentFloat = <span class="hljs-number">.123&#x27;456e3&#x27;000</span>; <span class="hljs-comment">// digit separators in floats</span>
<span class="hljs-type">float</span> suffixed = <span class="hljs-number">3.000&#x27;001&#x27;234f</span> <span class="hljs-comment">// digit separators in suffixed numbers</span>
<span class="hljs-type">char</span> word[] = { <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;\0&#x27;</span> }; <span class="hljs-comment">// make sure digit separators don&#x27;t mess up chars</span>
<span class="hljs-type">float</span> negative = <span class="hljs-number">-123.0f</span>; <span class="hljs-comment">// negative floating point numbers</span>
<span class="hljs-comment">/* Floating-point literals. */</span>
<span class="hljs-comment">// Decimal.</span>
<span class="hljs-number">1.</span>
<span class="hljs-number">+12.</span>
<span class="hljs-number">-1&#x27;234.</span>
<span class="hljs-number">1.2</span>
<span class="hljs-number">1.234&#x27;5</span>
<span class="hljs-number">1&#x27;234.567&#x27;8</span>
<span class="hljs-number">1&#x27;234.567&#x27;8e-9</span>
<span class="hljs-number">1&#x27;234.567&#x27;8E9&#x27;0</span>
<span class="hljs-number">1234e56</span>
<span class="hljs-number">1&#x27;234e5&#x27;6</span>
<span class="hljs-comment">// Hexadecimal.</span>
<span class="hljs-number">0x1p2</span>
<span class="hljs-number">+0x1.p2</span>
<span class="hljs-number">-0X1A.P2</span>
<span class="hljs-number">0x1A&#x27;2B3C.p4</span>
<span class="hljs-number">0x1.Ap2</span>
<span class="hljs-number">0x1A.2B3C&#x27;4Dp+5</span>
<span class="hljs-number">0x1A&#x27;2b3C.4D5e&#x27;6Fp7&#x27;8</span>
<span class="hljs-comment">// Literal suffixes.</span>
<span class="hljs-number">1.F</span> <span class="hljs-number">1.F16</span> <span class="hljs-number">1.F32</span> <span class="hljs-number">1.f64</span> <span class="hljs-number">1.f128</span>
<span class="hljs-number">2.BF16</span> <span class="hljs-number">2.bf16</span>
<span class="hljs-number">0X3&#x27;4.P5L</span> <span class="hljs-number">0x3&#x27;4.p5l</span>

<span class="hljs-comment">/* literal suffixes: these are to be highlighted as numbers */</span>
<span class="hljs-number">90ll</span> <span class="hljs-number">90LL</span> <span class="hljs-number">90u</span> <span class="hljs-number">90U</span> <span class="hljs-number">90l</span> <span class="hljs-number">90L</span>
<span class="hljs-number">90ul</span> <span class="hljs-number">90ul</span> <span class="hljs-number">90Ul</span> <span class="hljs-number">90UL</span> <span class="hljs-number">90lu</span> <span class="hljs-number">90lU</span> <span class="hljs-number">90Lu</span> <span class="hljs-number">90LU</span>
<span class="hljs-number">90ull</span> <span class="hljs-number">90uLL</span> <span class="hljs-number">90Ull</span> <span class="hljs-number">90ULL</span> <span class="hljs-number">90llu</span> <span class="hljs-number">90llU</span> <span class="hljs-number">90LLu</span> <span class="hljs-number">90LLU</span>
<span class="hljs-comment">/* Integer literals. */</span>
<span class="hljs-comment">// Binary.</span>
<span class="hljs-number">+0b1</span>
<span class="hljs-number">0B01</span>
<span class="hljs-number">-0b10&#x27;1011</span>
<span class="hljs-comment">// Hexadecimal.</span>
<span class="hljs-number">+0x1</span>
<span class="hljs-number">0X1A</span>
<span class="hljs-number">-0x1A&#x27;2b3C</span>
<span class="hljs-comment">// Octal.</span>
<span class="hljs-number">+01</span>
<span class="hljs-number">012</span>
<span class="hljs-number">-0123&#x27;4567</span>
<span class="hljs-comment">// Decimal.</span>
<span class="hljs-number">0</span>
<span class="hljs-number">+1</span>
<span class="hljs-number">12</span>
<span class="hljs-number">-123&#x27;456&#x27;789</span>
<span class="hljs-comment">// Literal suffixes.</span>
<span class="hljs-number">0B1U</span> <span class="hljs-number">0b1u</span>
<span class="hljs-number">0X2L</span> <span class="hljs-number">0x2l</span>
<span class="hljs-number">03LL</span> <span class="hljs-number">03ll</span>
<span class="hljs-number">4UL</span> <span class="hljs-number">4Ul</span> <span class="hljs-number">4uL</span> <span class="hljs-number">4ul</span>
<span class="hljs-number">5LU</span> <span class="hljs-number">5Lu</span> <span class="hljs-number">5lU</span> <span class="hljs-number">5lu</span>
<span class="hljs-number">6ULL</span> <span class="hljs-number">6Ull</span> <span class="hljs-number">6uLL</span> <span class="hljs-number">6ull</span>
<span class="hljs-number">7LLU</span> <span class="hljs-number">7LLu</span> <span class="hljs-number">7llU</span> <span class="hljs-number">7llu</span>
<span class="hljs-number">8ZU</span> <span class="hljs-number">8Zu</span> <span class="hljs-number">8zU</span> <span class="hljs-number">8zu</span>
<span class="hljs-number">9&#x27;0UZ</span> <span class="hljs-number">9&#x27;0Uz</span> <span class="hljs-number">9&#x27;0uZ</span> <span class="hljs-number">9&#x27;0uz</span>

<span class="hljs-type">char</span> word[] = { <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;\0&#x27;</span> }; <span class="hljs-comment">// Make sure digit separators don&#x27;t mess up chars.</span>
64 changes: 54 additions & 10 deletions test/markup/cpp/number-literals.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
/* digit separators */
int number = 2'555'555'555; // digit separators
float exponentFloat = .123'456e3'000; // digit separators in floats
float suffixed = 3.000'001'234f // digit separators in suffixed numbers
char word[] = { '3', '\0' }; // make sure digit separators don't mess up chars
float negative = -123.0f; // negative floating point numbers
/* Floating-point literals. */
// Decimal.
1.
+12.
-1'234.
1.2
1.234'5
1'234.567'8
1'234.567'8e-9
1'234.567'8E9'0
1234e56
1'234e5'6
// Hexadecimal.
0x1p2
+0x1.p2
-0X1A.P2
0x1A'2B3C.p4
0x1.Ap2
0x1A.2B3C'4Dp+5
0x1A'2b3C.4D5e'6Fp7'8
// Literal suffixes.
1.F 1.F16 1.F32 1.f64 1.f128
2.BF16 2.bf16
0X3'4.P5L 0x3'4.p5l

/* literal suffixes: these are to be highlighted as numbers */
90ll 90LL 90u 90U 90l 90L
90ul 90ul 90Ul 90UL 90lu 90lU 90Lu 90LU
90ull 90uLL 90Ull 90ULL 90llu 90llU 90LLu 90LLU
/* Integer literals. */
// Binary.
+0b1
0B01
-0b10'1011
// Hexadecimal.
+0x1
0X1A
-0x1A'2b3C
// Octal.
+01
012
-0123'4567
// Decimal.
0
+1
12
-123'456'789
// Literal suffixes.
0B1U 0b1u
0X2L 0x2l
03LL 03ll
4UL 4Ul 4uL 4ul
5LU 5Lu 5lU 5lu
6ULL 6Ull 6uLL 6ull
7LLU 7LLu 7llU 7llu
8ZU 8Zu 8zU 8zu
9'0UZ 9'0Uz 9'0uZ 9'0uz

char word[] = { '3', '\0' }; // Make sure digit separators don't mess up chars.

0 comments on commit fdd24e9

Please sign in to comment.