Simplify checking for octal literals in parser

This commit is contained in:
Jason Freeman 2014-08-07 12:49:52 -07:00
parent 92f7c98adb
commit cc7ca33eef
2 changed files with 16 additions and 16 deletions

View File

@ -1055,25 +1055,25 @@ module ts {
function parseLiteralNode(): LiteralExpression {
var node = <LiteralExpression>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);
}
}

View File

@ -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;
}