Permalink
Browse files

Reduce the cyclomatic complexity of numeric literal scanning.

Spin off the hex and octal handling to two separate functions. This
decrease the cyclomatic complexity of scanNumericLiteral from 28 to 19.

http://code.google.com/p/esprima/issues/detail?id=396
  • Loading branch information...
1 parent 1032a91 commit 517cc4bbb38adefceee3ad3e403d3a02d113819f @ariya committed Jan 4, 2013
Showing with 54 additions and 51 deletions.
  1. +53 −50 esprima.js
  2. +1 −1 package.json
View
@@ -675,6 +675,56 @@ parseStatement: true, parseSourceElement: true */
// 7.8.3 Numeric Literals
+ function scanHexLiteral(start) {
+ var number = '';
+
+ while (index < length) {
+ if (!isHexDigit(source[index])) {
+ break;
+ }
+ number += source[index++];
+ }
+
+ if (number.length === 0) {
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
+ }
+
+ if (isIdentifierStart(source.charCodeAt(index))) {
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
+ }
+
+ return {
+ type: Token.NumericLiteral,
+ value: parseInt('0x' + number, 16),
+ lineNumber: lineNumber,
+ lineStart: lineStart,
+ range: [start, index]
+ };
+ }
+
+ function scanOctalLiteral(start) {
+ var number = '0' + source[index++];
+ while (index < length) {
+ if (!isOctalDigit(source[index])) {
+ break;
+ }
+ number += source[index++];
+ }
+
+ if (isIdentifierStart(source.charCodeAt(index)) || isDecimalDigit(source.charCodeAt(index))) {
+ throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
+ }
+
+ return {
+ type: Token.NumericLiteral,
+ value: parseInt(number, 8),
+ octal: true,
+ lineNumber: lineNumber,
+ lineStart: lineStart,
+ range: [start, index]
+ };
+ }
+
function scanNumericLiteral() {
var number, start, ch;
@@ -692,58 +742,11 @@ parseStatement: true, parseSourceElement: true */
// Octal number starts with '0'.
if (number === '0') {
if (ch === 'x' || ch === 'X') {
- number += source[index++];
- while (index < length) {
- ch = source[index];
- if (!isHexDigit(ch)) {
- break;
- }
- number += source[index++];
- }
-
- if (number.length <= 2) {
- // only 0x
- throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
- }
-
- if (index < length) {
- ch = source[index];
- if (isIdentifierStart(ch.charCodeAt(0))) {
- throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
- }
- }
- return {
- type: Token.NumericLiteral,
- value: parseInt(number, 16),
- lineNumber: lineNumber,
- lineStart: lineStart,
- range: [start, index]
- };
+ ++index;
+ return scanHexLiteral(start);
}
if (isOctalDigit(ch)) {
- number += source[index++];
- while (index < length) {
- ch = source[index];
- if (!isOctalDigit(ch)) {
- break;
- }
- number += source[index++];
- }
-
- if (index < length) {
- ch = source.charCodeAt(index);
- if (isIdentifierStart(ch) || isDecimalDigit(ch)) {
- throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
- }
- }
- return {
- type: Token.NumericLiteral,
- value: parseInt(number, 8),
- octal: true,
- lineNumber: lineNumber,
- lineStart: lineStart,
- range: [start, index]
- };
+ return scanOctalLiteral(start);
}
// decimal number starts with '0' such as '09' is illegal.
View
@@ -38,7 +38,7 @@
"complexity": "npm run-script analyze-complexity && npm run-script check-complexity",
"analyze-complexity": "node tools/list-complexity.js",
- "check-complexity": "node node_modules/complexity-report/src/cli.js --maxcc 30 --silent -l -w esprima.js",
+ "check-complexity": "node node_modules/complexity-report/src/cli.js --maxcc 21 --silent -l -w esprima.js",
"benchmark": "node test/benchmarks.js",
"benchmark-quick": "node test/benchmarks.js quick"

0 comments on commit 517cc4b

Please sign in to comment.