From f744113edf2648c775ab72402cac7d683a429c62 Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Wed, 6 Aug 2014 12:09:27 -0700 Subject: [PATCH] Scan octal literals --- src/compiler/scanner.ts | 26 +++++++++++++++++-- .../scannerES3NumericLiteral3.errors.txt | 4 +++ .../reference/scannerES3NumericLiteral3.js | 5 ---- .../scannerNumericLiteral3.errors.txt | 4 +++ .../reference/scannerNumericLiteral3.js | 5 ---- 5 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 tests/baselines/reference/scannerES3NumericLiteral3.errors.txt delete mode 100644 tests/baselines/reference/scannerES3NumericLiteral3.js create mode 100644 tests/baselines/reference/scannerNumericLiteral3.errors.txt delete mode 100644 tests/baselines/reference/scannerNumericLiteral3.js diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 70c1dbee6ac..c04cf3d8c23 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -300,6 +300,10 @@ module ts { return ch >= CharacterCodes._0 && ch <= CharacterCodes._9; } + function isOctalDigit(ch: number): boolean { + return ch >= CharacterCodes._0 && ch <= CharacterCodes._7; + } + export function skipTrivia(text: string, pos: number, stopAfterLineBreak?: boolean): number { while (true) { var ch = text.charCodeAt(pos); @@ -360,7 +364,9 @@ module ts { var precedingLineBreak: boolean; function error(message: DiagnosticMessage): void { - if (onError) onError(message); + if (onError) { + onError(message); + } } function isIdentifierStart(ch: number): boolean { @@ -398,6 +404,14 @@ module ts { return +(text.substring(start, end)); } + function scanOctalDigits(): number { + var start = pos; + while (isOctalDigit(text.charCodeAt(pos))) { + pos++; + } + return +(text.substring(start, pos)); + } + function scanHexDigits(count: number, exact?: boolean): number { var digits = 0; var value = 0; @@ -681,7 +695,7 @@ module ts { if (!commentClosed) { pos++; - onError(Diagnostics.Asterisk_Slash_expected); + error(Diagnostics.Asterisk_Slash_expected); } if (onComment) { @@ -708,6 +722,14 @@ module ts { tokenValue = "" + value; return SyntaxKind.NumericLiteral; } + // Try to parse as an octal + if (pos + 1 < len && isOctalDigit(text.charCodeAt(pos + 1))) { + tokenValue = "" + scanOctalDigits(); + return SyntaxKind.NumericLiteral; + } + // This fall-through is a deviation from the EcmaScript grammar. The grammar says that a leading zero + // can only be followed by an octal digit, a dot, or the end of the number literal. However, we are being + // permissive and allowing decimal digits of the form 08* and 09* (which many browsers also do). case CharacterCodes._1: case CharacterCodes._2: case CharacterCodes._3: diff --git a/tests/baselines/reference/scannerES3NumericLiteral3.errors.txt b/tests/baselines/reference/scannerES3NumericLiteral3.errors.txt new file mode 100644 index 00000000000..42f7a908a76 --- /dev/null +++ b/tests/baselines/reference/scannerES3NumericLiteral3.errors.txt @@ -0,0 +1,4 @@ +==== tests/cases/conformance/scanner/ecmascript3/scannerES3NumericLiteral3.ts (1 errors) ==== + 01.0 + ~~ +!!! ';' expected. \ No newline at end of file diff --git a/tests/baselines/reference/scannerES3NumericLiteral3.js b/tests/baselines/reference/scannerES3NumericLiteral3.js deleted file mode 100644 index 7ecb6c8d377..00000000000 --- a/tests/baselines/reference/scannerES3NumericLiteral3.js +++ /dev/null @@ -1,5 +0,0 @@ -//// [scannerES3NumericLiteral3.ts] -01.0 - -//// [scannerES3NumericLiteral3.js] -01.0; diff --git a/tests/baselines/reference/scannerNumericLiteral3.errors.txt b/tests/baselines/reference/scannerNumericLiteral3.errors.txt new file mode 100644 index 00000000000..6f82581759b --- /dev/null +++ b/tests/baselines/reference/scannerNumericLiteral3.errors.txt @@ -0,0 +1,4 @@ +==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral3.ts (1 errors) ==== + 01.0 + ~~ +!!! ';' expected. \ No newline at end of file diff --git a/tests/baselines/reference/scannerNumericLiteral3.js b/tests/baselines/reference/scannerNumericLiteral3.js deleted file mode 100644 index 382c0622ac2..00000000000 --- a/tests/baselines/reference/scannerNumericLiteral3.js +++ /dev/null @@ -1,5 +0,0 @@ -//// [scannerNumericLiteral3.ts] -01.0 - -//// [scannerNumericLiteral3.js] -01.0;