From 3fe308d2a6b642704942cb4b1ee8fe8438d22e3f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 3 Jun 2015 13:46:13 -0700 Subject: [PATCH] Added an explanation for the lookahead. --- src/compiler/parser.ts | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 72cd3bb620d..914952d5169 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3973,7 +3973,7 @@ module ts { case SyntaxKind.ThrowKeyword: return parseThrowStatement(); case SyntaxKind.TryKeyword: - // Include the next two for error recovery. + // Include 'catch' and 'finally' for error recovery. case SyntaxKind.CatchKeyword: case SyntaxKind.FinallyKeyword: return parseTryStatement(); @@ -3981,6 +3981,33 @@ module ts { return parseDebuggerStatement(); case SyntaxKind.AtToken: return parseDeclaration(); + + case SyntaxKind.ConstKeyword: + case SyntaxKind.EnumKeyword: + case SyntaxKind.ExportKeyword: + case SyntaxKind.ImportKeyword: + case SyntaxKind.InterfaceKeyword: + case SyntaxKind.PrivateKeyword: + case SyntaxKind.ProtectedKeyword: + case SyntaxKind.PublicKeyword: + case SyntaxKind.StaticKeyword: + if (getDeclarationFlags() & flags) { + return parseDeclaration(); + } + break; + + // 'declare', 'module', 'namespace', and 'type' are all legal JavaScript identifiers when ASI + // takes effect. In such cases, we cannot parse out the "expected" declarations. For instance, while + // + // namespace n + // + // can be none other than the beginning of a namespace declaration, JavaScript sees + // + // namespace + // n + // + // as the identifier 'namespace' on one line followed by the identifier 'n' on another. + // We need to look one token ahead to see if it permissible to try parsing a declaration. case SyntaxKind.DeclareKeyword: if (lookAhead(isFollowedByIdentifierOrKeywordOnSameLine) && getDeclarationFlags() & flags) { return parseDeclaration(); @@ -3997,19 +4024,6 @@ module ts { return parseDeclaration(); } break; - case SyntaxKind.ConstKeyword: - case SyntaxKind.EnumKeyword: - case SyntaxKind.ExportKeyword: - case SyntaxKind.ImportKeyword: - case SyntaxKind.InterfaceKeyword: - case SyntaxKind.PrivateKeyword: - case SyntaxKind.ProtectedKeyword: - case SyntaxKind.PublicKeyword: - case SyntaxKind.StaticKeyword: - if (getDeclarationFlags() & flags) { - return parseDeclaration(); - } - break; } return parseExpressionOrLabeledStatement(); }