Skip to content

Commit f744113

Browse files
committed
Scan octal literals
1 parent 893940c commit f744113

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

src/compiler/scanner.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ module ts {
300300
return ch >= CharacterCodes._0 && ch <= CharacterCodes._9;
301301
}
302302

303+
function isOctalDigit(ch: number): boolean {
304+
return ch >= CharacterCodes._0 && ch <= CharacterCodes._7;
305+
}
306+
303307
export function skipTrivia(text: string, pos: number, stopAfterLineBreak?: boolean): number {
304308
while (true) {
305309
var ch = text.charCodeAt(pos);
@@ -360,7 +364,9 @@ module ts {
360364
var precedingLineBreak: boolean;
361365

362366
function error(message: DiagnosticMessage): void {
363-
if (onError) onError(message);
367+
if (onError) {
368+
onError(message);
369+
}
364370
}
365371

366372
function isIdentifierStart(ch: number): boolean {
@@ -398,6 +404,14 @@ module ts {
398404
return +(text.substring(start, end));
399405
}
400406

407+
function scanOctalDigits(): number {
408+
var start = pos;
409+
while (isOctalDigit(text.charCodeAt(pos))) {
410+
pos++;
411+
}
412+
return +(text.substring(start, pos));
413+
}
414+
401415
function scanHexDigits(count: number, exact?: boolean): number {
402416
var digits = 0;
403417
var value = 0;
@@ -681,7 +695,7 @@ module ts {
681695

682696
if (!commentClosed) {
683697
pos++;
684-
onError(Diagnostics.Asterisk_Slash_expected);
698+
error(Diagnostics.Asterisk_Slash_expected);
685699
}
686700

687701
if (onComment) {
@@ -708,6 +722,14 @@ module ts {
708722
tokenValue = "" + value;
709723
return SyntaxKind.NumericLiteral;
710724
}
725+
// Try to parse as an octal
726+
if (pos + 1 < len && isOctalDigit(text.charCodeAt(pos + 1))) {
727+
tokenValue = "" + scanOctalDigits();
728+
return SyntaxKind.NumericLiteral;
729+
}
730+
// This fall-through is a deviation from the EcmaScript grammar. The grammar says that a leading zero
731+
// can only be followed by an octal digit, a dot, or the end of the number literal. However, we are being
732+
// permissive and allowing decimal digits of the form 08* and 09* (which many browsers also do).
711733
case CharacterCodes._1:
712734
case CharacterCodes._2:
713735
case CharacterCodes._3:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
==== tests/cases/conformance/scanner/ecmascript3/scannerES3NumericLiteral3.ts (1 errors) ====
2+
01.0
3+
~~
4+
!!! ';' expected.

tests/baselines/reference/scannerES3NumericLiteral3.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral3.ts (1 errors) ====
2+
01.0
3+
~~
4+
!!! ';' expected.

tests/baselines/reference/scannerNumericLiteral3.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)