Skip to content

Commit 36db271

Browse files
fix: Handle single zero and decimal numbers correctly in lexer
Co-Authored-By: [email protected] <[email protected]>
1 parent df6a01d commit 36db271

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

jparse/lexer.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (l *lexer) scanNumber() token {
430430
return token{Type: typeMinus, Value: "-", Position: pos}
431431
}
432432

433-
// Handle special number formats (hex, binary, octal)
433+
// Handle special number formats (hex, binary, octal)
434434
if l.acceptRune('0') {
435435
next := l.peek()
436436
switch next {
@@ -452,15 +452,11 @@ func (l *lexer) scanNumber() token {
452452
return token{Type: typeError, Value: "invalid hexadecimal number", Position: pos}
453453
}
454454
return token{Type: typeNumber, Value: l.input[pos:l.current], Position: pos}
455-
}
456-
// Handle leading zeros
457-
if isDigit(next) {
458-
l.acceptAll(isDigit)
459-
if l.acceptRune('.') {
460-
if !l.acceptAll(isDigit) {
461-
l.backup()
462-
return token{Type: typeNumber, Value: l.input[pos:l.current-1], Position: pos}
463-
}
455+
case '.':
456+
l.nextRune()
457+
if !l.acceptAll(isDigit) {
458+
l.backup()
459+
return token{Type: typeNumber, Value: "0", Position: pos}
464460
}
465461
if l.acceptRunes2('e', 'E') {
466462
l.acceptRunes2('+', '-')
@@ -469,7 +465,34 @@ func (l *lexer) scanNumber() token {
469465
}
470466
}
471467
return token{Type: typeNumber, Value: l.input[pos:l.current], Position: pos}
468+
case 'e', 'E':
469+
l.nextRune()
470+
l.acceptRunes2('+', '-')
471+
if !l.acceptAll(isDigit) {
472+
return token{Type: typeError, Value: "invalid number literal", Position: pos}
473+
}
474+
return token{Type: typeNumber, Value: l.input[pos:l.current], Position: pos}
472475
}
476+
477+
// Handle single zero or leading zeros
478+
if !isDigit(next) {
479+
return token{Type: typeNumber, Value: "0", Position: pos}
480+
}
481+
482+
l.acceptAll(isDigit)
483+
if l.acceptRune('.') {
484+
if !l.acceptAll(isDigit) {
485+
l.backup()
486+
return token{Type: typeNumber, Value: l.input[pos:l.current-1], Position: pos}
487+
}
488+
}
489+
if l.acceptRunes2('e', 'E') {
490+
l.acceptRunes2('+', '-')
491+
if !l.acceptAll(isDigit) {
492+
return token{Type: typeError, Value: "invalid number literal", Position: pos}
493+
}
494+
}
495+
return token{Type: typeNumber, Value: l.input[pos:l.current], Position: pos}
473496
}
474497

475498
// Handle decimal point at start

0 commit comments

Comments
 (0)