Commit c0eb72e0 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[scanner] Tweak ScanNumber

Change-Id: I1654da286ae15bc028803286a188b5d89111c3d3
Reviewed-on: https://chromium-review.googlesource.com/c/1495983
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59986}
parent ab24897c
...@@ -853,15 +853,15 @@ Token::Value Scanner::ScanNumber(bool seen_period) { ...@@ -853,15 +853,15 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
// either 0, 0exxx, 0Exxx, 0.xxx, a hex number, a binary number or // either 0, 0exxx, 0Exxx, 0.xxx, a hex number, a binary number or
// an octal number. // an octal number.
if (c0_ == 'x' || c0_ == 'X') { if (AsciiAlphaToLower(c0_) == 'x') {
AddLiteralCharAdvance(); AddLiteralCharAdvance();
kind = HEX; kind = HEX;
if (!ScanHexDigits()) return Token::ILLEGAL; if (!ScanHexDigits()) return Token::ILLEGAL;
} else if (c0_ == 'o' || c0_ == 'O') { } else if (AsciiAlphaToLower(c0_) == 'o') {
AddLiteralCharAdvance(); AddLiteralCharAdvance();
kind = OCTAL; kind = OCTAL;
if (!ScanOctalDigits()) return Token::ILLEGAL; if (!ScanOctalDigits()) return Token::ILLEGAL;
} else if (c0_ == 'b' || c0_ == 'B') { } else if (AsciiAlphaToLower(c0_) == 'b') {
AddLiteralCharAdvance(); AddLiteralCharAdvance();
kind = BINARY; kind = BINARY;
if (!ScanBinaryDigits()) return Token::ILLEGAL; if (!ScanBinaryDigits()) return Token::ILLEGAL;
...@@ -883,14 +883,12 @@ Token::Value Scanner::ScanNumber(bool seen_period) { ...@@ -883,14 +883,12 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
} }
// Parse decimal digits and allow trailing fractional part. // Parse decimal digits and allow trailing fractional part.
if (kind == DECIMAL || kind == DECIMAL_WITH_LEADING_ZERO) { if (IsDecimalNumberKind(kind)) {
// This is an optimization for parsing Decimal numbers as Smi's. // This is an optimization for parsing Decimal numbers as Smi's.
if (at_start) { if (at_start) {
uint64_t value = 0; uint64_t value = 0;
// scan subsequent decimal digits // scan subsequent decimal digits
if (!ScanDecimalAsSmi(&value)) { if (!ScanDecimalAsSmi(&value)) return Token::ILLEGAL;
return Token::ILLEGAL;
}
if (next().literal_chars.one_byte_literal().length() <= 10 && if (next().literal_chars.one_byte_literal().length() <= 10 &&
value <= Smi::kMaxValue && c0_ != '.' && !IsIdentifierStart(c0_)) { value <= Smi::kMaxValue && c0_ != '.' && !IsIdentifierStart(c0_)) {
...@@ -917,8 +915,7 @@ Token::Value Scanner::ScanNumber(bool seen_period) { ...@@ -917,8 +915,7 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
} }
bool is_bigint = false; bool is_bigint = false;
if (c0_ == 'n' && !seen_period && if (c0_ == 'n' && !seen_period && IsValidBigIntKind(kind)) {
(kind == DECIMAL || kind == HEX || kind == OCTAL || kind == BINARY)) {
// Check that the literal is within our limits for BigInt length. // Check that the literal is within our limits for BigInt length.
// For simplicity, use 4 bits per character to calculate the maximum // For simplicity, use 4 bits per character to calculate the maximum
// allowed literal length. // allowed literal length.
...@@ -932,12 +929,11 @@ Token::Value Scanner::ScanNumber(bool seen_period) { ...@@ -932,12 +929,11 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
is_bigint = true; is_bigint = true;
Advance(); Advance();
} else if (c0_ == 'e' || c0_ == 'E') { } else if (AsciiAlphaToLower(c0_) == 'e') {
// scan exponent, if any // scan exponent, if any
DCHECK(kind != HEX); // 'e'/'E' must be scanned as part of the hex number DCHECK(kind != HEX); // 'e'/'E' must be scanned as part of the hex number
if (!(kind == DECIMAL || kind == DECIMAL_WITH_LEADING_ZERO)) if (!IsDecimalNumberKind(kind)) return Token::ILLEGAL;
return Token::ILLEGAL;
// scan exponent // scan exponent
AddLiteralCharAdvance(); AddLiteralCharAdvance();
......
...@@ -543,14 +543,22 @@ class Scanner { ...@@ -543,14 +543,22 @@ class Scanner {
}; };
enum NumberKind { enum NumberKind {
IMPLICIT_OCTAL,
BINARY, BINARY,
OCTAL, OCTAL,
IMPLICIT_OCTAL,
HEX, HEX,
DECIMAL, DECIMAL,
DECIMAL_WITH_LEADING_ZERO DECIMAL_WITH_LEADING_ZERO
}; };
inline bool IsValidBigIntKind(NumberKind kind) {
return IsInRange(kind, BINARY, DECIMAL);
}
inline bool IsDecimalNumberKind(NumberKind kind) {
return IsInRange(kind, DECIMAL, DECIMAL_WITH_LEADING_ZERO);
}
static const int kCharacterLookaheadBufferSize = 1; static const int kCharacterLookaheadBufferSize = 1;
static const int kMaxAscii = 127; static const int kMaxAscii = 127;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment