From a5800408636e824929abf2523c7b50907e8a2434 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 24 Nov 2014 14:36:05 -0800 Subject: [PATCH] Address code review --- .../diagnosticInformationMap.generated.ts | 4 +- src/compiler/diagnosticMessages.json | 4 +- src/compiler/emitter.ts | 16 ++++-- src/compiler/scanner.ts | 49 +++++++++---------- .../binaryIntegerLiteralError.errors.txt | 12 ++--- .../octalIntegerLiteralError.errors.txt | 12 ++--- 6 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 13587df29e..e4094b2581 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -125,8 +125,8 @@ module ts { Unterminated_template_literal: { code: 1160, category: DiagnosticCategory.Error, key: "Unterminated template literal." }, Unterminated_regular_expression_literal: { code: 1161, category: DiagnosticCategory.Error, key: "Unterminated regular expression literal." }, An_object_member_cannot_be_declared_optional: { code: 1162, category: DiagnosticCategory.Error, key: "An object member cannot be declared optional." }, - Binary_digits_expected: { code: 1163, category: DiagnosticCategory.Error, key: "Binary digits expected." }, - Octal_digits_expected: { code: 1164, category: DiagnosticCategory.Error, key: "Octal digits expected." }, + Binary_digit_expected: { code: 1163, category: DiagnosticCategory.Error, key: "Binary digit expected." }, + Octal_digit_expected: { code: 1164, category: DiagnosticCategory.Error, key: "Octal digit expected." }, Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 427eba4531..1cca0aa130 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -491,11 +491,11 @@ "category": "Error", "code": 1162 }, - "Binary digits expected.": { + "Binary digit expected.": { "category": "Error", "code": 1163 }, - "Octal digits expected.": { + "Octal digit expected.": { "category": "Error", "code": 1164 }, diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 0bf6c1f635..4b4535616d 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -788,6 +788,18 @@ module ts { } } + function isBinaryOrOctalIntegerLiteral(text: string): boolean { + if (text.length <= 0) { + return false; + } + + if (text.charCodeAt(1) === CharacterCodes.B || text.charCodeAt(1) === CharacterCodes.b || + text.charCodeAt(1) === CharacterCodes.O || text.charCodeAt(1) === CharacterCodes.o) { + return true; + } + return false; + } + function emitLiteral(node: LiteralExpression): void { var text = getLiteralText(); @@ -795,9 +807,7 @@ module ts { writer.writeLiteral(text); } // For version below ES6, emit binary integer literal and octal integer literal as decimal value - else if (compilerOptions.target < ScriptTarget.ES6 && node.kind === SyntaxKind.NumericLiteral && - ((text.charCodeAt(1) === CharacterCodes.B || text.charCodeAt(1) === CharacterCodes.b || - text.charCodeAt(1) === CharacterCodes.O || text.charCodeAt(1) === CharacterCodes.o))) { + else if (compilerOptions.target < ScriptTarget.ES6 && node.kind === SyntaxKind.NumericLiteral && isBinaryOrOctalIntegerLiteral(text)) { write(node.text); } else { diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 596155c813..3c75580e7e 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -520,27 +520,6 @@ module ts { return +(text.substring(start, pos)); } - function scanBinaryOrOctalDigits(base: number): number { - if (base !== 2 && base !== 8) { - return -1; - } - var value = 0; - while (true) { - var ch = text.charCodeAt(pos); - var valueOfCh = ch - CharacterCodes._0; - if (!isDigit(ch)) { - break; - } - // We know at this point that ch must be digit - if (valueOfCh >= base) { - return -1; - } - value = value * base + valueOfCh; - pos++; - } - return value; - } - function scanHexDigits(count: number, mustMatchCount?: boolean): number { var digits = 0; var value = 0; @@ -774,6 +753,26 @@ module ts { return token = SyntaxKind.Identifier; } + function scanBinaryOrOctalDigits(base: number): number { + Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + + var value = 0; + while (true) { + var ch = text.charCodeAt(pos); + var valueOfCh = ch - CharacterCodes._0; + if (!isDigit(ch)) { + break; + } + // We know at this point that ch must be digit + if (valueOfCh >= base) { + break; + } + value = value * base + valueOfCh; + pos++; + } + return value; + } + function scan(): SyntaxKind { startPos = pos; precedingLineBreak = false; @@ -956,9 +955,9 @@ module ts { } else if (pos + 2 < len && (text.charCodeAt(pos + 1) === CharacterCodes.B || text.charCodeAt(pos + 1) === CharacterCodes.b)) { pos += 2; - var value = scanBinaryOrOctalDigits(/* binary */2); + var value = scanBinaryOrOctalDigits(/* base */ 2); if (value < 0) { - error(Diagnostics.Binary_digits_expected); + error(Diagnostics.Binary_digit_expected); value = 0; } tokenValue = "" + value; @@ -966,9 +965,9 @@ module ts { } else if (pos + 2 < len && (text.charCodeAt(pos + 1) === CharacterCodes.O || text.charCodeAt(pos + 1) === CharacterCodes.o)) { pos += 2; - var value = scanBinaryOrOctalDigits(/* octal */8); + var value = scanBinaryOrOctalDigits(/* base */ 8); if (value < 0) { - error(Diagnostics.Octal_digits_expected); + error(Diagnostics.Octal_digit_expected); value = 0; } tokenValue = "" + value; diff --git a/tests/baselines/reference/binaryIntegerLiteralError.errors.txt b/tests/baselines/reference/binaryIntegerLiteralError.errors.txt index 3346ee3377..20358a8406 100644 --- a/tests/baselines/reference/binaryIntegerLiteralError.errors.txt +++ b/tests/baselines/reference/binaryIntegerLiteralError.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts(2,17): error TS1163: Binary digits expected. -tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts(3,17): error TS1163: Binary digits expected. +tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts(2,17): error TS1005: ',' expected. +tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts(3,17): error TS1005: ',' expected. tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts(6,5): error TS2300: Duplicate identifier '0b11010'. tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts(7,5): error TS2300: Duplicate identifier '26'. tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts(8,5): error TS2300: Duplicate identifier '"26"'. @@ -8,11 +8,11 @@ tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralErr ==== tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/binaryIntegerLiteralError.ts (5 errors) ==== // error var bin1 = 0B1102110; - -!!! error TS1163: Binary digits expected. + ~~~~ +!!! error TS1005: ',' expected. var bin1 = 0b11023410; - -!!! error TS1163: Binary digits expected. + ~~~~~ +!!! error TS1005: ',' expected. var obj1 = { 0b11010: "hi", diff --git a/tests/baselines/reference/octalIntegerLiteralError.errors.txt b/tests/baselines/reference/octalIntegerLiteralError.errors.txt index d0e2531796..f1386f75b8 100644 --- a/tests/baselines/reference/octalIntegerLiteralError.errors.txt +++ b/tests/baselines/reference/octalIntegerLiteralError.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts(2,19): error TS1164: Octal digits expected. -tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts(3,18): error TS1164: Octal digits expected. +tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts(2,19): error TS1005: ',' expected. +tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts(3,18): error TS1005: ',' expected. tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts(6,5): error TS2300: Duplicate identifier '0O45436'. tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts(7,5): error TS2300: Duplicate identifier '19230'. tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts(8,5): error TS2300: Duplicate identifier '"19230"'. @@ -8,11 +8,11 @@ tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralErro ==== tests/cases/conformance/es6/binaryAndOctalIntegerLiteral/octalIntegerLiteralError.ts (5 errors) ==== // error var oct1 = 0O13334823; - -!!! error TS1164: Octal digits expected. + ~~~ +!!! error TS1005: ',' expected. var oct2 = 0o34318592; - -!!! error TS1164: Octal digits expected. + ~~~~ +!!! error TS1005: ',' expected. var obj1 = { 0O45436: "hi",