Skip to content

Commit 07ee351

Browse files
authored
fix(c) fixed hex decimal numbers (#4094)
1 parent 19819d5 commit 07ee351

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Core Grammars:
1515
- enh(erlang) OTP25/27 maybe statement [nixxquality][]
1616
- enh(dart) Support digit-separators in number literals [Sam Rawlins][]
1717
- enh(csharp) add Contextual keywords `file`, `args`, `dynamic`, `record`, `required` and `scoped` [Alvin Joy][]
18+
- fix(c) - Fixed hex numbers with decimals [Dxuian]
1819
- fix(ruby) - fix `|=` operator false positives (as block arguments) [Aboobacker MK]
1920

2021
New Grammars:
@@ -42,6 +43,7 @@ CONTRIBUTORS
4243
[nixxquality]: https://github.com/nixxquality
4344
[srawlins]: https://github.com/srawlins
4445
[Alvin Joy]: https://github.com/alvinsjoy
46+
[Dxuian]:https://github.com/Dxuian
4547
[Aboobacker MK]: https://github.com/tachyons
4648
[Imken]: https://github.com/immccn123
4749

src/languages/c.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ export default function(hljs) {
5757
const NUMBERS = {
5858
className: 'number',
5959
variants: [
60-
{ begin: '\\b(0b[01\']+)' },
61-
{ begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)' },
62-
{ begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
63-
],
60+
{ match: /\b(0b[01']+)/ },
61+
{ match: /(-?)\b([\d']+(\.[\d']*)?|\.[\d']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)/ },
62+
{ match: /(-?)\b(0[xX][a-fA-F0-9]+(?:'[a-fA-F0-9]+)*(?:\.[a-fA-F0-9]*(?:'[a-fA-F0-9]*)*)?(?:[pP][-+]?[0-9]+)?(l|L)?(u|U)?)/ },
63+
{ match: /(-?)\b\d+(?:'\d+)*(?:\.\d*(?:'\d*)*)?(?:[eE][-+]?\d+)?/ }
64+
],
6465
relevance: 0
65-
};
66-
66+
};
67+
6768
const PREPROCESSOR = {
6869
className: 'meta',
6970
begin: /#\s*[a-z]+\b/,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<span class="hljs-comment">/* Floating-point literals. */</span>
2+
<span class="hljs-comment">// Decimal.</span>
3+
<span class="hljs-number">1.</span>
4+
+<span class="hljs-number">12.</span>
5+
<span class="hljs-number">1.2</span>
6+
<span class="hljs-number">1234e56</span>
7+
8+
<span class="hljs-comment">// Hexadecimal.</span>
9+
<span class="hljs-number">0x1p2</span>
10+
+<span class="hljs-number">0x1.p2</span>
11+
<span class="hljs-number">-0X1A.P2</span>
12+
<span class="hljs-number">0x1.Ap2</span>
13+
14+
<span class="hljs-comment">// Literal suffixes.</span>
15+
<span class="hljs-number">1.F</span>
16+
17+
<span class="hljs-comment">/* Integer literals. */</span>
18+
<span class="hljs-comment">// Binary.</span>
19+
+<span class="hljs-number">0b1</span> <span class="hljs-comment">// Note: Not standard C, but valid in some compilers</span>
20+
<span class="hljs-number">0B</span>01 <span class="hljs-comment">// Note: Not standard C, but valid in some compilers</span>
21+
22+
<span class="hljs-comment">// Hexadecimal.</span>
23+
+<span class="hljs-number">0x1</span>
24+
<span class="hljs-number">0X1A</span>
25+
26+
<span class="hljs-comment">// Octal.</span>
27+
+<span class="hljs-number">01</span>
28+
<span class="hljs-number">012</span>
29+
30+
<span class="hljs-comment">// Decimal.</span>
31+
<span class="hljs-number">0</span>
32+
+<span class="hljs-number">1</span>
33+
<span class="hljs-number">12</span>
34+
35+
<span class="hljs-comment">// Literal suffixes.</span>
36+
<span class="hljs-number">0X2L</span>
37+
<span class="hljs-number">0x2l</span>
38+
<span class="hljs-number">03LL</span>
39+
<span class="hljs-number">03ll</span>
40+
<span class="hljs-number">4UL</span> <span class="hljs-number">4Ul</span> <span class="hljs-number">4uL</span> <span class="hljs-number">4ul</span>
41+
<span class="hljs-number">5LU</span> <span class="hljs-number">5Lu</span> <span class="hljs-number">5lU</span> <span class="hljs-number">5lu</span>
42+
<span class="hljs-number">6ULL</span> <span class="hljs-number">6Ull</span> <span class="hljs-number">6uLL</span> <span class="hljs-number">6ull</span>
43+
<span class="hljs-number">7LLU</span> <span class="hljs-number">7LLu</span> <span class="hljs-number">7llU</span> <span class="hljs-number">7llu</span>
44+
45+
<span class="hljs-comment">// Character array.</span>
46+
<span class="hljs-type">char</span> word[] = { <span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-string">&#x27;\0&#x27;</span> };

test/markup/c/number-literals.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Floating-point literals. */
2+
// Decimal.
3+
1.
4+
+12.
5+
1.2
6+
1234e56
7+
8+
// Hexadecimal.
9+
0x1p2
10+
+0x1.p2
11+
-0X1A.P2
12+
0x1.Ap2
13+
14+
// Literal suffixes.
15+
1.F
16+
17+
/* Integer literals. */
18+
// Binary.
19+
+0b1 // Note: Not standard C, but valid in some compilers
20+
0B01 // Note: Not standard C, but valid in some compilers
21+
22+
// Hexadecimal.
23+
+0x1
24+
0X1A
25+
26+
// Octal.
27+
+01
28+
012
29+
30+
// Decimal.
31+
0
32+
+1
33+
12
34+
35+
// Literal suffixes.
36+
0X2L
37+
0x2l
38+
03LL
39+
03ll
40+
4UL 4Ul 4uL 4ul
41+
5LU 5Lu 5lU 5lu
42+
6ULL 6Ull 6uLL 6ull
43+
7LLU 7LLu 7llU 7llu
44+
45+
// Character array.
46+
char word[] = { '3', '\0' };

0 commit comments

Comments
 (0)