Skip to content

Commit

Permalink
Simplify various loops when scanning for a numeric literal.
Browse files Browse the repository at this point in the history
This decrease the cyclomatic complexity of scanNumericLiteral down to 14.

http://code.google.com/p/esprima/issues/detail?id=396
  • Loading branch information
ariya committed Jan 5, 2013
1 parent 517cc4b commit 2499612
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,24 +755,18 @@ parseStatement: true, parseSourceElement: true */
}
}

while (index < length) {
if (!isDecimalDigit(source.charCodeAt(index))) {
ch = source[index];
break;
}
while (isDecimalDigit(source.charCodeAt(index))) {
number += source[index++];
}
ch = source[index];
}

if (ch === '.') {
number += source[index++];
while (index < length) {
if (!isDecimalDigit(source.charCodeAt(index))) {
ch = source[index];
break;
}
while (isDecimalDigit(source.charCodeAt(index))) {
number += source[index++];
}
ch = source[index];
}

if (ch === 'e' || ch === 'E') {
Expand All @@ -782,30 +776,17 @@ parseStatement: true, parseSourceElement: true */
if (ch === '+' || ch === '-') {
number += source[index++];
}

ch = source[index];
if (ch && isDecimalDigit(ch.charCodeAt(0))) {
number += source[index++];
while (index < length) {
if (!isDecimalDigit(source.charCodeAt(index))) {
ch = source[index];
break;
}
if (isDecimalDigit(source.charCodeAt(index))) {
while (isDecimalDigit(source.charCodeAt(index))) {
number += source[index++];
}
} else {
ch = 'character ' + ch;
if (index >= length) {
ch = '<end>';
}
throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
}
}

if (index < length) {
if (isIdentifierStart(source.charCodeAt(index))) {
throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
}
if (isIdentifierStart(source.charCodeAt(index))) {
throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
}

return {
Expand Down

0 comments on commit 2499612

Please sign in to comment.