Scan octal literals

This commit is contained in:
Jason Freeman 2014-08-06 12:09:27 -07:00
parent 893940cedf
commit f744113edf
5 changed files with 32 additions and 12 deletions

View File

@ -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:

View File

@ -0,0 +1,4 @@
==== tests/cases/conformance/scanner/ecmascript3/scannerES3NumericLiteral3.ts (1 errors) ====
01.0
~~
!!! ';' expected.

View File

@ -1,5 +0,0 @@
//// [scannerES3NumericLiteral3.ts]
01.0
//// [scannerES3NumericLiteral3.js]
01.0;

View File

@ -0,0 +1,4 @@
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral3.ts (1 errors) ====
01.0
~~
!!! ';' expected.

View File

@ -1,5 +0,0 @@
//// [scannerNumericLiteral3.ts]
01.0
//// [scannerNumericLiteral3.js]
01.0;