Skip to content

Commit 15e6b64

Browse files
Merge branch 'master' into parserErrors2
2 parents 1285c46 + 51804ee commit 15e6b64

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

src/compiler/parser.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,8 @@ module ts {
972972
}
973973

974974
export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile {
975-
var scanner: Scanner;
976975
var token: SyntaxKind;
977976
var parsingContext: ParsingContext;
978-
var commentRanges: TextRange[];
979977
var identifiers: Map<string> = {};
980978
var identifierCount = 0;
981979
var nodeCount = 0;
@@ -1184,10 +1182,6 @@ module ts {
11841182
parseErrorAtPosition(pos, 0, message);
11851183
}
11861184

1187-
function onComment(pos: number, end: number) {
1188-
if (commentRanges) commentRanges.push({ pos: pos, end: end });
1189-
}
1190-
11911185
function getNodePos(): number {
11921186
return scanner.getStartPos();
11931187
}
@@ -4267,14 +4261,25 @@ module ts {
42674261
}
42684262

42694263
function processReferenceComments(): ReferenceComments {
4264+
var triviaScanner = createScanner(languageVersion, /*skipTrivia*/false, sourceText);
42704265
var referencedFiles: FileReference[] = [];
42714266
var amdDependencies: string[] = [];
42724267
var amdModuleName: string;
4273-
commentRanges = [];
4274-
token = scanner.scan();
42754268

4276-
for (var i = 0; i < commentRanges.length; i++) {
4277-
var range = commentRanges[i];
4269+
// Keep scanning all the leading trivia in the file until we get to something that
4270+
// isn't trivia. Any single line comment will be analyzed to see if it is a
4271+
// reference comment.
4272+
while (true) {
4273+
var kind = triviaScanner.scan();
4274+
if (kind === SyntaxKind.WhitespaceTrivia || kind === SyntaxKind.NewLineTrivia || kind === SyntaxKind.MultiLineCommentTrivia) {
4275+
continue;
4276+
}
4277+
if (kind !== SyntaxKind.SingleLineCommentTrivia) {
4278+
break;
4279+
}
4280+
4281+
var range = { pos: triviaScanner.getTokenPos(), end: triviaScanner.getTextPos() };
4282+
42784283
var comment = sourceText.substring(range.pos, range.end);
42794284
var referencePathMatchResult = getFileReferenceFromReferencePath(comment, range);
42804285
if (referencePathMatchResult) {
@@ -4305,7 +4310,7 @@ module ts {
43054310
}
43064311
}
43074312
}
4308-
commentRanges = undefined;
4313+
43094314
return {
43104315
referencedFiles,
43114316
amdDependencies,
@@ -4341,7 +4346,6 @@ module ts {
43414346
return syntacticDiagnostics;
43424347
}
43434348

4344-
scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError, onComment);
43454349
var rootNodeFlags: NodeFlags = 0;
43464350
if (fileExtensionIs(filename, ".d.ts")) {
43474351
rootNodeFlags = NodeFlags.DeclarationFile;
@@ -4367,6 +4371,10 @@ module ts {
43674371
sourceFile.amdDependencies = referenceComments.amdDependencies;
43684372
sourceFile.amdModuleName = referenceComments.amdModuleName;
43694373

4374+
// Create and prime the scanner before parsing the source elements.
4375+
var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError);
4376+
nextToken();
4377+
43704378
sourceFile.statements = parseList(ParsingContext.SourceElements, /*checkForStrictMode*/ true, parseSourceElement);
43714379
Debug.assert(token === SyntaxKind.EndOfFileToken);
43724380
sourceFile.endOfFileToken = parseTokenNode();

src/compiler/scanner.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ module ts {
472472
ch > CharacterCodes.maxAsciiCharacter && isUnicodeIdentifierPart(ch, languageVersion);
473473
}
474474

475-
export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback, onComment?: CommentCallback): Scanner {
475+
export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback): Scanner {
476476
var pos: number; // Current position (end position of text of current token)
477477
var len: number; // Length of text
478478
var startPos: number; // Start position of whitespace before current token
@@ -908,9 +908,6 @@ module ts {
908908
pos++;
909909

910910
}
911-
if (onComment) {
912-
onComment(tokenPos, pos);
913-
}
914911

915912
if (skipTrivia) {
916913
continue;
@@ -943,10 +940,6 @@ module ts {
943940
error(Diagnostics.Asterisk_Slash_expected);
944941
}
945942

946-
if (onComment) {
947-
onComment(tokenPos, pos);
948-
}
949-
950943
if (skipTrivia) {
951944
continue;
952945
}

0 commit comments

Comments
 (0)