From afc57777f33ed66de2e8406427720d52a6e2a256 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Mon, 8 Dec 2014 18:36:25 -0800 Subject: [PATCH 1/2] Put the actual parsing logic at the top of createSourceFile instead of the bottom. This makes it vastly simpler to fix up that logic since you no longer have to go find the end of the function first. --- src/compiler/parser.ts | 92 ++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 52 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index dfee57c68b3..dc99bdbeb89 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1007,7 +1007,6 @@ module ts { } export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile { - var token: SyntaxKind; var parsingContext: ParsingContext; var identifiers: Map = {}; var identifierCount = 0; @@ -1016,8 +1015,7 @@ module ts { // Flags that dictate what parsing context we're in. For example: // Whether or not we are in strict parsing mode. All that changes in strict parsing mode is - // that some tokens that would be considered identifiers may be considered keywords. When - // rewinding, we need to store and restore this as the mode may have changed. + // that some tokens that would be considered identifiers may be considered keywords. // // When adding more parser context flags, consider which is the more common case that the // flag will be in. This should be hte 'false' state for that flag. The reason for this is @@ -1091,6 +1089,41 @@ module ts { // Note: any errors at the end of the file that do not precede a regular node, should get // attached to the EOF token. var parseErrorBeforeNextFinishedNode = false; + var sourceFile = createRootNode(SyntaxKind.SourceFile, 0, sourceText.length, + fileExtensionIs(filename, ".d.ts") ? NodeFlags.DeclarationFile : 0); + + sourceFile.getLineAndCharacterFromPosition = getLineAndCharacterFromSourcePosition; + sourceFile.getPositionFromLineAndCharacter = getPositionFromSourceLineAndCharacter; + sourceFile.getLineStarts = getLineStarts; + sourceFile.getSyntacticDiagnostics = getSyntacticDiagnostics; + + sourceFile.filename = normalizePath(filename); + sourceFile.text = sourceText; + + sourceFile.referenceDiagnostics = []; + sourceFile.parseDiagnostics = []; + sourceFile.grammarDiagnostics = []; + sourceFile.semanticDiagnostics = []; + + processReferenceComments(); + + // Create and prime the scanner before parsing the source elements. + var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError); + var token = nextToken(); + + sourceFile.statements = parseList(ParsingContext.SourceElements, /*checkForStrictMode*/ true, parseSourceElement); + Debug.assert(token === SyntaxKind.EndOfFileToken); + sourceFile.endOfFileToken = parseTokenNode(); + + sourceFile.externalModuleIndicator = getExternalModuleIndicator(); + + sourceFile.nodeCount = nodeCount; + sourceFile.identifierCount = identifierCount; + sourceFile.version = version; + sourceFile.isOpen = isOpen; + sourceFile.languageVersion = languageVersion; + sourceFile.identifiers = identifiers; + return sourceFile; function setContextFlag(val: Boolean, flag: ParserContextFlags) { if (val) { @@ -4338,7 +4371,7 @@ module ts { : parseStatement(); } - function processReferenceComments(): ReferenceComments { + function processReferenceComments(): void { var triviaScanner = createScanner(languageVersion, /*skipTrivia*/false, sourceText); var referencedFiles: FileReference[] = []; var amdDependencies: string[] = []; @@ -4389,11 +4422,9 @@ module ts { } } - return { - referencedFiles, - amdDependencies, - amdModuleName - }; + sourceFile.referencedFiles = referencedFiles; + sourceFile.amdDependencies = amdDependencies; + sourceFile.amdModuleName = amdModuleName; } function getExternalModuleIndicator() { @@ -4423,49 +4454,6 @@ module ts { Debug.assert(syntacticDiagnostics !== undefined); return syntacticDiagnostics; } - - var rootNodeFlags: NodeFlags = 0; - if (fileExtensionIs(filename, ".d.ts")) { - rootNodeFlags = NodeFlags.DeclarationFile; - } - - var sourceFile = createRootNode(SyntaxKind.SourceFile, 0, sourceText.length, rootNodeFlags); - - sourceFile.getLineAndCharacterFromPosition = getLineAndCharacterFromSourcePosition; - sourceFile.getPositionFromLineAndCharacter = getPositionFromSourceLineAndCharacter; - sourceFile.getLineStarts = getLineStarts; - sourceFile.getSyntacticDiagnostics = getSyntacticDiagnostics; - - sourceFile.filename = normalizePath(filename); - sourceFile.text = sourceText; - - sourceFile.referenceDiagnostics = []; - sourceFile.parseDiagnostics = []; - sourceFile.grammarDiagnostics = []; - sourceFile.semanticDiagnostics = []; - - var referenceComments = processReferenceComments(); - sourceFile.referencedFiles = referenceComments.referencedFiles; - sourceFile.amdDependencies = referenceComments.amdDependencies; - sourceFile.amdModuleName = referenceComments.amdModuleName; - - // Create and prime the scanner before parsing the source elements. - var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError); - nextToken(); - - sourceFile.statements = parseList(ParsingContext.SourceElements, /*checkForStrictMode*/ true, parseSourceElement); - Debug.assert(token === SyntaxKind.EndOfFileToken); - sourceFile.endOfFileToken = parseTokenNode(); - - sourceFile.externalModuleIndicator = getExternalModuleIndicator(); - - sourceFile.nodeCount = nodeCount; - sourceFile.identifierCount = identifierCount; - sourceFile.version = version; - sourceFile.isOpen = isOpen; - sourceFile.languageVersion = languageVersion; - sourceFile.identifiers = identifiers; - return sourceFile; } function isLeftHandSideExpression(expr: Expression): boolean { From 89cd1e28f2cf261a9b64b994a503d29920762c56 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Mon, 8 Dec 2014 18:58:13 -0800 Subject: [PATCH 2/2] Remove unused type. --- src/compiler/parser.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index dc99bdbeb89..588b1f53974 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -48,12 +48,6 @@ module ts { return node; } - interface ReferenceComments { - referencedFiles: FileReference[]; - amdDependencies: string[]; - amdModuleName: string; - } - export function getSourceFileOfNode(node: Node): SourceFile { while (node && node.kind !== SyntaxKind.SourceFile) node = node.parent; return node;