(start: number)
| 463 | } |
| 464 | |
| 465 | private scanNumber(start: number): Token { |
| 466 | let simple = this.index === start; |
| 467 | let hasSeparators = false; |
| 468 | this.advance(); // Skip initial digit. |
| 469 | while (true) { |
| 470 | if (chars.isDigit(this.peek)) { |
| 471 | // Do nothing. |
| 472 | } else if (this.peek === chars.$_) { |
| 473 | // Separators are only valid when they're surrounded by digits. E.g. `1_0_1` is |
| 474 | // valid while `_101` and `101_` are not. The separator can't be next to the decimal |
| 475 | // point or another separator either. Note that it's unlikely that we'll hit a case where |
| 476 | // the underscore is at the start, because that's a valid identifier and it will be picked |
| 477 | // up earlier in the parsing. We validate for it anyway just in case. |
| 478 | if ( |
| 479 | !chars.isDigit(this.input.charCodeAt(this.index - 1)) || |
| 480 | !chars.isDigit(this.input.charCodeAt(this.index + 1)) |
| 481 | ) { |
| 482 | return this.error('Invalid numeric separator', 0); |
| 483 | } |
| 484 | hasSeparators = true; |
| 485 | } else if (this.peek === chars.$PERIOD) { |
| 486 | simple = false; |
| 487 | } else if (isExponentStart(this.peek)) { |
| 488 | this.advance(); |
| 489 | if (isExponentSign(this.peek)) this.advance(); |
| 490 | if (!chars.isDigit(this.peek)) return this.error('Invalid exponent', -1); |
| 491 | simple = false; |
| 492 | } else { |
| 493 | break; |
| 494 | } |
| 495 | this.advance(); |
| 496 | } |
| 497 | |
| 498 | let str = this.input.substring(start, this.index); |
| 499 | if (hasSeparators) { |
| 500 | str = str.replace(/_/g, ''); |
| 501 | } |
| 502 | const value = simple ? parseIntAutoRadix(str) : parseFloat(str); |
| 503 | return newNumberToken(start, this.index, value); |
| 504 | } |
| 505 | |
| 506 | private scanString(): Token { |
| 507 | const start = this.index; |
no test coverage detected