Commit e77eebfe authored by Sathya Gunasekaran's avatar Sathya Gunasekaran Committed by Commit Bot

[numeric separator] Simplify error handling

Pointing to the exact spot of the incorrect numeric separator seems
clearer both in terms of the error itself, and the resulting code in
scanner because we don't have to keep track of the start position.

Previously, the error was:
  ➜ ./out.gn/x64.release/d8 --harmony-numeric-separator -e '0x1__1'
  unnamed:1: SyntaxError: Only one underscore is allowed as numeric separator
  0x1__1
  ^^^^
  SyntaxError: Only one underscore is allowed as numeric separator

Now, the error is:
  ➜ ./out.gn/x64.release/d8 --harmony-numeric-separator -e '0x1__1'
  unnamed:1: SyntaxError: Only one underscore is allowed as numeric separator
  0x1__1
      ^
  SyntaxError: Only one underscore is allowed as numeric separator


Bug: v8:7317
Change-Id: I7df1b39816e51a97234da6ed0fca1bf8c0223c3e
Reviewed-on: https://chromium-review.googlesource.com/962241
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Reviewed-by: 's avatarMathias Bynens <mathias@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51950}
parent a1fe961c
......@@ -1226,7 +1226,6 @@ Handle<String> Scanner::SourceMappingUrl(Isolate* isolate) const {
}
bool Scanner::ScanDigitsWithNumericSeparators(bool (*predicate)(uc32 ch),
int start_pos,
bool is_check_first_digit) {
// we must have at least one digit after 'x'/'b'/'o'
if (is_check_first_digit && !predicate(c0_)) return false;
......@@ -1236,7 +1235,7 @@ bool Scanner::ScanDigitsWithNumericSeparators(bool (*predicate)(uc32 ch),
if (c0_ == '_') {
Advance<false, false>();
if (c0_ == '_') {
ReportScannerError(Location(start_pos, source_pos()),
ReportScannerError(Location(source_pos(), source_pos() + 1),
MessageTemplate::kContinuousNumericSeparator);
return false;
}
......@@ -1248,7 +1247,7 @@ bool Scanner::ScanDigitsWithNumericSeparators(bool (*predicate)(uc32 ch),
}
if (separator_seen) {
ReportScannerError(Location(start_pos, source_pos()),
ReportScannerError(Location(source_pos(), source_pos() + 1),
MessageTemplate::kTrailingNumericSeparator);
return false;
}
......@@ -1256,9 +1255,9 @@ bool Scanner::ScanDigitsWithNumericSeparators(bool (*predicate)(uc32 ch),
return true;
}
bool Scanner::ScanDecimalDigits(int start_pos) {
bool Scanner::ScanDecimalDigits() {
if (allow_harmony_numeric_separator()) {
return ScanDigitsWithNumericSeparators(&IsDecimalDigit, start_pos, false);
return ScanDigitsWithNumericSeparators(&IsDecimalDigit, false);
}
while (IsDecimalDigit(c0_)) {
AddLiteralCharAdvance();
......@@ -1266,14 +1265,13 @@ bool Scanner::ScanDecimalDigits(int start_pos) {
return true;
}
bool Scanner::ScanDecimalAsSmiWithNumericSeparators(int start_pos,
uint64_t* value) {
bool Scanner::ScanDecimalAsSmiWithNumericSeparators(uint64_t* value) {
bool separator_seen = false;
while (IsDecimalDigit(c0_) || c0_ == '_') {
if (c0_ == '_') {
Advance<false, false>();
if (c0_ == '_') {
ReportScannerError(Location(start_pos, source_pos()),
ReportScannerError(Location(source_pos(), source_pos() + 1),
MessageTemplate::kContinuousNumericSeparator);
return false;
}
......@@ -1288,7 +1286,7 @@ bool Scanner::ScanDecimalAsSmiWithNumericSeparators(int start_pos,
}
if (separator_seen) {
ReportScannerError(Location(start_pos, source_pos()),
ReportScannerError(Location(source_pos(), source_pos() + 1),
MessageTemplate::kTrailingNumericSeparator);
return false;
}
......@@ -1296,9 +1294,9 @@ bool Scanner::ScanDecimalAsSmiWithNumericSeparators(int start_pos,
return true;
}
bool Scanner::ScanDecimalAsSmi(int start_pos, uint64_t* value) {
bool Scanner::ScanDecimalAsSmi(uint64_t* value) {
if (allow_harmony_numeric_separator()) {
return ScanDecimalAsSmiWithNumericSeparators(start_pos, value);
return ScanDecimalAsSmiWithNumericSeparators(value);
}
while (IsDecimalDigit(c0_)) {
......@@ -1310,9 +1308,9 @@ bool Scanner::ScanDecimalAsSmi(int start_pos, uint64_t* value) {
return true;
}
bool Scanner::ScanBinaryDigits(int start_pos) {
bool Scanner::ScanBinaryDigits() {
if (allow_harmony_numeric_separator()) {
return ScanDigitsWithNumericSeparators(&IsBinaryDigit, start_pos, true);
return ScanDigitsWithNumericSeparators(&IsBinaryDigit, true);
}
// we must have at least one binary digit after 'b'/'B'
......@@ -1326,9 +1324,9 @@ bool Scanner::ScanBinaryDigits(int start_pos) {
return true;
}
bool Scanner::ScanOctalDigits(int start_pos) {
bool Scanner::ScanOctalDigits() {
if (allow_harmony_numeric_separator()) {
return ScanDigitsWithNumericSeparators(&IsOctalDigit, start_pos, true);
return ScanDigitsWithNumericSeparators(&IsOctalDigit, true);
}
// we must have at least one octal digit after 'o'/'O'
......@@ -1362,9 +1360,9 @@ bool Scanner::ScanImplicitOctalDigits(int start_pos,
}
}
bool Scanner::ScanHexDigits(int start_pos) {
bool Scanner::ScanHexDigits() {
if (allow_harmony_numeric_separator()) {
return ScanDigitsWithNumericSeparators(&IsHexDigit, start_pos, true);
return ScanDigitsWithNumericSeparators(&IsHexDigit, true);
}
// we must have at least one hex digit after 'x'/'X'
......@@ -1378,11 +1376,11 @@ bool Scanner::ScanHexDigits(int start_pos) {
return true;
}
bool Scanner::ScanSignedInteger(int start_pos) {
bool Scanner::ScanSignedInteger() {
if (c0_ == '+' || c0_ == '-') AddLiteralCharAdvance();
// we must have at least one decimal digit after 'e'/'E'
if (!IsDecimalDigit(c0_)) return false;
return ScanDecimalDigits(start_pos);
return ScanDecimalDigits();
}
Token::Value Scanner::ScanNumber(bool seen_period) {
......@@ -1400,7 +1398,7 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
return Token::ILLEGAL;
}
// we know we have at least one digit
if (!ScanDecimalDigits(start_pos)) return Token::ILLEGAL;
if (!ScanDecimalDigits()) return Token::ILLEGAL;
} else {
// if the first character is '0' we must check for octals and hex
if (c0_ == '0') {
......@@ -1411,15 +1409,15 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
if (c0_ == 'x' || c0_ == 'X') {
AddLiteralCharAdvance();
kind = HEX;
if (!ScanHexDigits(start_pos)) return Token::ILLEGAL;
if (!ScanHexDigits()) return Token::ILLEGAL;
} else if (c0_ == 'o' || c0_ == 'O') {
AddLiteralCharAdvance();
kind = OCTAL;
if (!ScanOctalDigits(start_pos)) return Token::ILLEGAL;
if (!ScanOctalDigits()) return Token::ILLEGAL;
} else if (c0_ == 'b' || c0_ == 'B') {
AddLiteralCharAdvance();
kind = BINARY;
if (!ScanBinaryDigits(start_pos)) return Token::ILLEGAL;
if (!ScanBinaryDigits()) return Token::ILLEGAL;
} else if ('0' <= c0_ && c0_ <= '7') {
kind = IMPLICIT_OCTAL;
if (!ScanImplicitOctalDigits(start_pos, &kind)) {
......@@ -1443,7 +1441,7 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
if (at_start) {
uint64_t value = 0;
// scan subsequent decimal digits
if (!ScanDecimalAsSmi(start_pos, &value)) {
if (!ScanDecimalAsSmi(&value)) {
return Token::ILLEGAL;
}
......@@ -1463,14 +1461,14 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
HandleLeadSurrogate();
}
if (!ScanDecimalDigits(start_pos)) return Token::ILLEGAL;
if (!ScanDecimalDigits()) return Token::ILLEGAL;
if (c0_ == '.') {
seen_period = true;
AddLiteralCharAdvance();
if (allow_harmony_numeric_separator() && c0_ == '_') {
return Token::ILLEGAL;
}
if (!ScanDecimalDigits(start_pos)) return Token::ILLEGAL;
if (!ScanDecimalDigits()) return Token::ILLEGAL;
}
}
}
......@@ -1501,7 +1499,7 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
// scan exponent
AddLiteralCharAdvance();
if (!ScanSignedInteger(start_pos)) return Token::ILLEGAL;
if (!ScanSignedInteger()) return Token::ILLEGAL;
}
// The source character immediately following a numeric literal must
......
......@@ -736,16 +736,15 @@ class Scanner {
Token::Value ScanHtmlComment();
bool ScanDigitsWithNumericSeparators(bool (*predicate)(uc32 ch),
int start_pos,
bool is_check_first_digit);
bool ScanDecimalDigits(int start_pos);
bool ScanDecimalDigits();
// Optimized function to scan decimal number as Smi.
bool ScanDecimalAsSmi(int start_pos, uint64_t* value);
bool ScanDecimalAsSmiWithNumericSeparators(int start_pos, uint64_t* value);
bool ScanHexDigits(int start_pos);
bool ScanBinaryDigits(int start_pos);
bool ScanSignedInteger(int start_pos);
bool ScanOctalDigits(int start_pos);
bool ScanDecimalAsSmi(uint64_t* value);
bool ScanDecimalAsSmiWithNumericSeparators(uint64_t* value);
bool ScanHexDigits();
bool ScanBinaryDigits();
bool ScanSignedInteger();
bool ScanOctalDigits();
bool ScanImplicitOctalDigits(int start_pos, NumberKind* kind);
Token::Value ScanNumber(bool seen_period);
......
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