Skip to content

Commit 3f861b6

Browse files
committed
Add underscore digit separator support for hex literals
Enable hex literals to use underscores as digit separators, matching ClickHouse's support for numbers like 0xbad_cafe. Fixed the readNumberOrIdent function which is the code path used when parsing numbers starting with digits.
1 parent ea15fe3 commit 3f861b6

3 files changed

Lines changed: 5 additions & 51 deletions

File tree

lexer/lexer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,10 @@ func (l *Lexer) readNumber() Item {
630630
l.readChar()
631631
if l.ch == 'x' || l.ch == 'X' {
632632
// Hex literal (may include P notation for floats: 0x1p4, 0x1.2p-3)
633+
// Also allows underscores as digit separators: 0xbad_cafe
633634
sb.WriteRune(l.ch)
634635
l.readChar()
635-
for isHexDigit(l.ch) {
636+
for isHexDigit(l.ch) || l.ch == '_' {
636637
sb.WriteRune(l.ch)
637638
l.readChar()
638639
}
@@ -834,7 +835,8 @@ func (l *Lexer) readNumberOrIdent() Item {
834835
if val == "0" && (l.ch == 'x' || l.ch == 'X') {
835836
sb.WriteRune(l.ch)
836837
l.readChar()
837-
for isHexDigit(l.ch) {
838+
// Also allow underscores as digit separators: 0xbad_cafe
839+
for isHexDigit(l.ch) || l.ch == '_' {
838840
sb.WriteRune(l.ch)
839841
l.readChar()
840842
}

parser/testdata/02354_numeric_literals_with_underscores/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt4": true,
43
"stmt5": true,
54
"stmt6": true
65
}
Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,11 @@
11
{
22
"explain_todo": {
3-
"stmt10": true,
4-
"stmt11": true,
5-
"stmt12": true,
6-
"stmt13": true,
73
"stmt16": true,
84
"stmt17": true,
95
"stmt18": true,
10-
"stmt19": true,
11-
"stmt2": true,
12-
"stmt20": true,
13-
"stmt21": true,
14-
"stmt22": true,
15-
"stmt25": true,
16-
"stmt28": true,
17-
"stmt29": true,
18-
"stmt30": true,
19-
"stmt31": true,
20-
"stmt32": true,
21-
"stmt33": true,
22-
"stmt34": true,
23-
"stmt35": true,
24-
"stmt36": true,
256
"stmt39": true,
26-
"stmt4": true,
277
"stmt40": true,
28-
"stmt41": true,
29-
"stmt42": true,
30-
"stmt43": true,
31-
"stmt44": true,
32-
"stmt46": true,
33-
"stmt47": true,
34-
"stmt48": true,
35-
"stmt49": true,
36-
"stmt5": true,
37-
"stmt50": true,
38-
"stmt51": true,
39-
"stmt52": true,
40-
"stmt53": true,
41-
"stmt54": true,
42-
"stmt55": true,
43-
"stmt56": true,
44-
"stmt57": true,
45-
"stmt58": true,
46-
"stmt59": true,
47-
"stmt6": true,
488
"stmt60": true,
49-
"stmt61": true,
50-
"stmt62": true,
51-
"stmt63": true,
52-
"stmt64": true,
53-
"stmt65": true,
54-
"stmt7": true,
55-
"stmt8": true,
56-
"stmt9": true
9+
"stmt61": true
5710
}
5811
}

0 commit comments

Comments
 (0)