@@ -1034,10 +1034,13 @@ module ts {
10341034
10351035 export function createSourceFile ( filename : string , sourceText : string , languageVersion : ScriptTarget , setParentNodes = false ) : SourceFile {
10361036 var parsingContext : ParsingContext ;
1037- var identifiers : Map < string > = { } ;
1037+ var identifiers : Map < string > ;
10381038 var identifierCount = 0 ;
10391039 var nodeCount = 0 ;
10401040 var lineStarts : number [ ] ;
1041+ var syntacticDiagnostics : Diagnostic [ ] ;
1042+ var scanner : Scanner ;
1043+ var token : SyntaxKind ;
10411044
10421045 // Flags that dictate what parsing context we're in. For example:
10431046 // Whether or not we are in strict parsing mode. All that changes in strict parsing mode is
@@ -1085,7 +1088,7 @@ module ts {
10851088 // Note: it should not be necessary to save/restore these flags during speculative/lookahead
10861089 // parsing. These context flags are naturally stored and restored through normal recursive
10871090 // descent parsing and unwinding.
1088- var contextFlags : ParserContextFlags = 0 ;
1091+ var contextFlags : ParserContextFlags ;
10891092
10901093 // Whether or not we've had a parse error since creating the last AST node. If we have
10911094 // encountered an error, it will be stored on the next AST node we create. Parse errors
@@ -1114,48 +1117,69 @@ module ts {
11141117 //
11151118 // Note: any errors at the end of the file that do not precede a regular node, should get
11161119 // attached to the EOF token.
1117- var parseErrorBeforeNextFinishedNode = false ;
1120+ var parseErrorBeforeNextFinishedNode : boolean ;
11181121
1119- var sourceFile = < SourceFile > createNode ( SyntaxKind . SourceFile , 0 ) ;
1120- if ( fileExtensionIs ( filename , ".d.ts" ) ) {
1121- sourceFile . flags = NodeFlags . DeclarationFile ;
1122- }
1123- sourceFile . end = sourceText . length ;
1124- sourceFile . filename = normalizePath ( filename ) ;
1125- sourceFile . text = sourceText ;
1122+ var sourceFile : SourceFile ;
1123+
1124+ return parseSourceFile ( sourceText , /*textChangeRange:*/ undefined , setParentNodes ) ;
1125+
1126+ function parseSourceFile ( text : string , textChangeRange : TextChangeRange , setParentNodes : boolean ) : SourceFile {
1127+ // Set our initial state before parsing.
1128+ sourceText = text ;
1129+ parsingContext = 0 ;
1130+ identifiers = { } ;
1131+ lineStarts = undefined ;
1132+ syntacticDiagnostics = undefined ;
1133+ contextFlags = 0 ;
1134+ parseErrorBeforeNextFinishedNode = false ;
1135+
1136+ sourceFile = < SourceFile > createNode ( SyntaxKind . SourceFile , 0 ) ;
1137+ sourceFile . referenceDiagnostics = [ ] ;
1138+ sourceFile . parseDiagnostics = [ ] ;
1139+ sourceFile . grammarDiagnostics = [ ] ;
1140+ sourceFile . semanticDiagnostics = [ ] ;
11261141
1127- sourceFile . getLineAndCharacterFromPosition = getLineAndCharacterFromSourcePosition ;
1128- sourceFile . getPositionFromLineAndCharacter = getPositionFromSourceLineAndCharacter ;
1129- sourceFile . getLineStarts = getLineStarts ;
1130- sourceFile . getSyntacticDiagnostics = getSyntacticDiagnostics ;
1142+ // Create and prime the scanner before parsing the source elements.
1143+ scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError ) ;
1144+ token = nextToken ( ) ;
11311145
1132- sourceFile . referenceDiagnostics = [ ] ;
1133- sourceFile . parseDiagnostics = [ ] ;
1134- sourceFile . grammarDiagnostics = [ ] ;
1135- sourceFile . semanticDiagnostics = [ ] ;
1146+ sourceFile . flags = fileExtensionIs ( filename , ".d.ts" ) ? NodeFlags . DeclarationFile : 0 ;
1147+ sourceFile . end = sourceText . length ;
1148+ sourceFile . filename = normalizePath ( filename ) ;
1149+ sourceFile . text = sourceText ;
11361150
1137- processReferenceComments ( ) ;
1151+ sourceFile . getLineAndCharacterFromPosition = getLineAndCharacterFromSourcePosition ;
1152+ sourceFile . getPositionFromLineAndCharacter = getPositionFromSourceLineAndCharacter ;
1153+ sourceFile . getLineStarts = getLineStarts ;
1154+ sourceFile . getSyntacticDiagnostics = getSyntacticDiagnostics ;
1155+ sourceFile . update = update ;
11381156
1139- // Create and prime the scanner before parsing the source elements.
1140- var scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError ) ;
1141- var token = nextToken ( ) ;
1157+ processReferenceComments ( sourceFile ) ;
11421158
1143- sourceFile . statements = parseList ( ParsingContext . SourceElements , /*checkForStrictMode*/ true , parseSourceElement ) ;
1144- Debug . assert ( token === SyntaxKind . EndOfFileToken ) ;
1145- sourceFile . endOfFileToken = parseTokenNode ( ) ;
1159+ sourceFile . statements = parseList ( ParsingContext . SourceElements , /*checkForStrictMode*/ true , parseSourceElement ) ;
1160+ Debug . assert ( token === SyntaxKind . EndOfFileToken ) ;
1161+ sourceFile . endOfFileToken = parseTokenNode ( ) ;
11461162
1147- sourceFile . externalModuleIndicator = getExternalModuleIndicator ( ) ;
1163+ setExternalModuleIndicator ( sourceFile ) ;
11481164
1149- sourceFile . nodeCount = nodeCount ;
1150- sourceFile . identifierCount = identifierCount ;
1151- sourceFile . languageVersion = languageVersion ;
1152- sourceFile . identifiers = identifiers ;
1165+ sourceFile . nodeCount = nodeCount ;
1166+ sourceFile . identifierCount = identifierCount ;
1167+ sourceFile . languageVersion = languageVersion ;
1168+ sourceFile . identifiers = identifiers ;
11531169
1154- if ( setParentNodes ) {
1155- fixupParentReferences ( sourceFile ) ;
1170+ if ( setParentNodes ) {
1171+ fixupParentReferences ( sourceFile ) ;
1172+ }
1173+
1174+ return sourceFile ;
1175+ }
1176+
1177+ function update ( newText : string , textChangeRange : TextChangeRange ) {
1178+ // Don't pass along the text change range for now. We'll pass it along once incremental
1179+ // parsing is enabled.
1180+ return parseSourceFile ( newText , /*textChangeRange:*/ undefined , /*setNodeParents*/ true ) ;
11561181 }
11571182
1158- return sourceFile ;
11591183
11601184 function setContextFlag ( val : Boolean , flag : ParserContextFlags ) {
11611185 if ( val ) {
@@ -4467,7 +4491,7 @@ module ts {
44674491 : parseStatement ( ) ;
44684492 }
44694493
4470- function processReferenceComments ( ) : void {
4494+ function processReferenceComments ( sourceFile : SourceFile ) : void {
44714495 var triviaScanner = createScanner ( languageVersion , /*skipTrivia*/ false , sourceText ) ;
44724496 var referencedFiles : FileReference [ ] = [ ] ;
44734497 var amdDependencies : string [ ] = [ ] ;
@@ -4523,16 +4547,15 @@ module ts {
45234547 sourceFile . amdModuleName = amdModuleName ;
45244548 }
45254549
4526- function getExternalModuleIndicator ( ) {
4527- return forEach ( sourceFile . statements , node =>
4550+ function setExternalModuleIndicator ( sourceFile : SourceFile ) {
4551+ sourceFile . externalModuleIndicator = forEach ( sourceFile . statements , node =>
45284552 node . flags & NodeFlags . Export
45294553 || node . kind === SyntaxKind . ImportDeclaration && ( < ImportDeclaration > node ) . moduleReference . kind === SyntaxKind . ExternalModuleReference
45304554 || node . kind === SyntaxKind . ExportAssignment
4531- ? node
4532- : undefined ) ;
4555+ ? node
4556+ : undefined ) ;
45334557 }
45344558
4535- var syntacticDiagnostics : Diagnostic [ ] ;
45364559 function getSyntacticDiagnostics ( ) {
45374560 if ( syntacticDiagnostics === undefined ) {
45384561 if ( sourceFile . parseDiagnostics . length > 0 ) {
0 commit comments