From cc7ca33eefe12bc42a66ad842132bb19475ce647 Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Thu, 7 Aug 2014 12:49:52 -0700 Subject: [PATCH] Simplify checking for octal literals in parser --- src/compiler/parser.ts | 30 +++++++++++++++--------------- src/compiler/scanner.ts | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index a7b2db64fb4..91a30143047 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1055,25 +1055,25 @@ module ts { function parseLiteralNode(): LiteralExpression { var node = createNode(token); node.text = scanner.getTokenValue(); + var tokenPos = scanner.getTokenPos(); nextToken(); finishNode(node); // Octal literals are not allowed in strict mode or ES5 - if (node.kind === SyntaxKind.NumericLiteral && (isInStrictMode || languageVersion >= ScriptTarget.ES5)) { - var numberLiteralSource = getSourceTextOfNodeFromSourceText(sourceText, node); - // This regex checks if the number is written in octal - // Note that theoretically would match literals like 009, which is not octal. But because - // of how the scanner separates the tokens, we would never get a token like this. Instead, - // we would get 00 and 9 as two separate tokens. - // We also do not need to check for negatives because any prefix operator would be part of a - // parent unary expression. - if (/0[0-7]+/.test(numberLiteralSource)) { - if (isInStrictMode) { - grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode); - } - else { - grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); - } + // Note that theoretically the following condition would hold true literals like 009, + // which is not octal.But because of how the scanner separates the tokens, we would + // never get a token like this.Instead, we would get 00 and 9 as two separate tokens. + // We also do not need to check for negatives because any prefix operator would be part of a + // parent unary expression. + if (node.kind === SyntaxKind.NumericLiteral + && sourceText.charCodeAt(tokenPos) === CharacterCodes._0 + && isOctalDigit(sourceText.charCodeAt(tokenPos + 1))) { + + if (isInStrictMode) { + grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode); + } + else if (languageVersion >= ScriptTarget.ES5) { + grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); } } diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index c04cf3d8c23..00966b12f2a 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -300,7 +300,7 @@ module ts { return ch >= CharacterCodes._0 && ch <= CharacterCodes._9; } - function isOctalDigit(ch: number): boolean { + export function isOctalDigit(ch: number): boolean { return ch >= CharacterCodes._0 && ch <= CharacterCodes._7; }