Add a dedicated 'EndOfFile' token to a SourceFile.

This is important for incremental parsing, as it is where we can attach parse errors at the end of
the file to.  Also, it helps with things like emitting comments at the end of the file.
This commit is contained in:
Cyrus Najmabadi
2014-12-02 16:09:41 -08:00
parent 6e945afdf5
commit 5a7500ca5e
46 changed files with 636 additions and 3 deletions

View File

@@ -3443,6 +3443,7 @@ module ts {
// Start new file on new line
writeLine();
emitDetachedComments(node);
// emit prologue directives prior to __extends
var startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
if (!extendsEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitExtends) {
@@ -3474,6 +3475,8 @@ module ts {
emitCaptureThisForNodeIfNecessary(node);
emitLinesStartingAt(node.statements, startIndex);
}
emitLeadingComments(node.endOfFileToken);
}
function emitNode(node: Node): void {

View File

@@ -313,8 +313,10 @@ module ts {
case SyntaxKind.FinallyBlock:
case SyntaxKind.FunctionBlock:
case SyntaxKind.ModuleBlock:
case SyntaxKind.SourceFile:
return children((<Block>node).statements);
case SyntaxKind.SourceFile:
return children((<SourceFile>node).statements) ||
child((<SourceFile>node).endOfFileToken);
case SyntaxKind.VariableStatement:
return children(node.modifiers) ||
children((<VariableStatement>node).declarations);
@@ -4294,6 +4296,9 @@ module ts {
sourceFile.amdModuleName = referenceComments.amdModuleName;
sourceFile.statements = parseList(ParsingContext.SourceElements, /*checkForStrictMode*/ true, parseSourceElement);
Debug.assert(token === SyntaxKind.EndOfFileToken);
sourceFile.endOfFileToken = parseTokenNode();
sourceFile.externalModuleIndicator = getExternalModuleIndicator();
sourceFile.nodeCount = nodeCount;

View File

@@ -786,6 +786,7 @@ module ts {
// Source files are declarations when they are external modules.
export interface SourceFile extends Declaration {
statements: NodeArray<ModuleElement>;
endOfFileToken: Node;
filename: string;
text: string;

View File

@@ -722,6 +722,9 @@ module ts {
public filename: string;
public text: string;
public statements: NodeArray<Statement>;
public endOfFileToken: Node;
// These methods will have their implementation provided by the implementation the
// compiler actually exports off of SourceFile.
public getLineAndCharacterFromPosition: (position: number) => LineAndCharacter;
@@ -743,7 +746,6 @@ module ts {
public nodeCount: number;
public identifierCount: number;
public symbolCount: number;
public statements: NodeArray<Statement>;
public version: string;
public isOpen: boolean;
public languageVersion: ScriptTarget;