Skip to content

Commit 080188a

Browse files
committed
{fix} off-by-one
1 parent add4548 commit 080188a

File tree

1 file changed

+63
-30
lines changed

1 file changed

+63
-30
lines changed

src/parser/tokenize.js

+63-30
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,11 @@ export function* tokenize(t, opts) {
458458

459459
switch (current) {
460460
case 0x62: // b
461-
pop();
461+
current = pop();
462462

463-
if (!consume(isHexadecimalDigit)) {
464-
if (consume(isHexadecimalDigitOrUnderscore)) {
463+
if (!consume(isBinaryDigit)) {
464+
if (current === 0x5f) {
465+
// _
465466
(errorsInToken ??= []).push(
466467
mkError(
467468
"Invalid hexadecimal number, the first character after 0x cannot be an underscore",
@@ -482,10 +483,11 @@ export function* tokenize(t, opts) {
482483
yield mkToken(T_NUMBER_BINARY);
483484
continue;
484485
case 0x6f: // o
485-
pop();
486+
current = pop();
486487

487-
if (!consume(isHexadecimalDigit)) {
488-
if (consume(isHexadecimalDigitOrUnderscore)) {
488+
if (!consume(isOctalDigit)) {
489+
if (current === 0x5f) {
490+
// _
489491
(errorsInToken ??= []).push(
490492
mkError(
491493
"Invalid hexadecimal number, the first character after 0x cannot be an underscore",
@@ -506,10 +508,11 @@ export function* tokenize(t, opts) {
506508
yield mkToken(T_NUMBER_OCTAL);
507509
continue;
508510
case 0x78: // x
509-
pop();
511+
current = pop();
510512

511513
if (!consume(isHexadecimalDigit)) {
512-
if (consume(isHexadecimalDigitOrUnderscore)) {
514+
if (current === 0x5f) {
515+
// _
513516
(errorsInToken ??= []).push(
514517
mkError(
515518
"Invalid hexadecimal number, the first character after 0x cannot be an underscore",
@@ -538,7 +541,8 @@ export function* tokenize(t, opts) {
538541
// .
539542

540543
if (!consume(isDecimalDigit)) {
541-
if (consume(isDecimalDigitOrUnderscore)) {
544+
if (current === 0x5f) {
545+
// _
542546
(errorsInToken ??= []).push(
543547
mkError(
544548
"Invalid decimal number, the part after the decimal point mustn't start on an underscore",
@@ -562,20 +566,22 @@ export function* tokenize(t, opts) {
562566
consume(isNumberSign);
563567

564568
if (!consume(isDecimalDigit)) {
565-
if (consume(isDecimalDigitOrUnderscore)) {
566-
zerOrMore(isDecimalDigitOrUnderscore);
569+
if (current === 0x5f) {
570+
// _
571+
(errorsInToken ??= []).push(
572+
mkError(
573+
"Invalid decimal number, the number after the exponent mustn't start on an underscore",
574+
),
575+
);
576+
} else {
577+
zerOrMore(isIdentifierChar);
578+
567579
yield mkToken(
568580
T_NUMBER_DECIMAL,
569-
"Invalid decimal number, the number after the exponent mustn't start on an underscore",
581+
"Invalid decimal number, missing a number after the exponent",
570582
);
571583
continue;
572584
}
573-
574-
yield mkToken(
575-
T_NUMBER_DECIMAL,
576-
"Invalid decimal number, missing a number after the exponent",
577-
);
578-
continue;
579585
}
580586

581587
zerOrMore(isDecimalDigitOrUnderscore);
@@ -631,10 +637,11 @@ export function* tokenize(t, opts) {
631637

632638
switch (current) {
633639
case 0x62: // b
634-
pop();
640+
current = pop();
635641

636642
if (!consume(isBinaryDigit)) {
637-
if (consume(isBinaryDigitOrUnderscore)) {
643+
if (current === 0x5f) {
644+
// _
638645
(errorsInToken ??= []).push(
639646
mkError(
640647
"Invalid binary number, the first character after 0b cannot be an underscore",
@@ -652,10 +659,11 @@ export function* tokenize(t, opts) {
652659
yield mkToken(T_NUMBER_BINARY);
653660
continue;
654661
case 0x6f: // o
655-
pop();
662+
current = pop();
656663

657664
if (!consume(isOctalDigit)) {
658-
if (consume(isOctalDigitOrUnderscore)) {
665+
if (current === 0x5f) {
666+
// _
659667
(errorsInToken ??= []).push(
660668
mkError(
661669
"Invalid octal number, the first character after 0o cannot be an underscore",
@@ -673,10 +681,11 @@ export function* tokenize(t, opts) {
673681
yield mkToken(T_NUMBER_OCTAL);
674682
continue;
675683
case 0x78: // x
676-
pop();
684+
current = pop();
677685

678686
if (!consume(isHexadecimalDigit)) {
679-
if (consume(isHexadecimalDigitOrUnderscore)) {
687+
if (current === 0x5f) {
688+
// _
680689
(errorsInToken ??= []).push(
681690
mkError(
682691
"Invalid hexadecimal number, the first character after 0x cannot be an underscore",
@@ -705,9 +714,20 @@ export function* tokenize(t, opts) {
705714
// .
706715

707716
if (!consume(isDecimalDigit)) {
708-
zerOrMore(isIdentifierChar);
709-
yield mkToken(T_IDENTIFIER_STRING, "Invalid decimal number");
710-
continue;
717+
if (current === 0x5f) {
718+
// _
719+
(errorsInToken ??= []).push(
720+
mkError(
721+
"Invalid decimal number, the part after the decimal point mustn't start on an underscore",
722+
),
723+
);
724+
} else {
725+
(errorsInToken ??= []).push(
726+
mkError(
727+
"Invalid decimal number, a decimal point must be followed by a digit",
728+
),
729+
);
730+
}
711731
}
712732

713733
zerOrMore(isDecimalDigitOrUnderscore);
@@ -719,9 +739,22 @@ export function* tokenize(t, opts) {
719739
consume(isNumberSign);
720740

721741
if (!consume(isDecimalDigit)) {
722-
zerOrMore(isIdentifierChar);
723-
yield mkToken(T_IDENTIFIER_STRING, "Invalid decimal number");
724-
continue;
742+
if (current === 0x5f) {
743+
// _
744+
(errorsInToken ??= []).push(
745+
mkError(
746+
"Invalid decimal number, the number after the exponent mustn't start on an underscore",
747+
),
748+
);
749+
} else {
750+
zerOrMore(isIdentifierChar);
751+
752+
yield mkToken(
753+
T_NUMBER_DECIMAL,
754+
"Invalid decimal number, missing a number after the exponent",
755+
);
756+
continue;
757+
}
725758
}
726759

727760
zerOrMore(isDecimalDigitOrUnderscore);

0 commit comments

Comments
 (0)