Skip to content

Commit 2510a5f

Browse files
Simplify scanner by removing need for a 'onComment' callback.
1 parent 05dc934 commit 2510a5f

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
@@ -951,10 +951,8 @@ module ts {
951951
}
952952

953953
export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile {
954-
var scanner: Scanner;
955954
var token: SyntaxKind;
956955
var parsingContext: ParsingContext;
957-
var commentRanges: TextRange[];
958956
var identifiers: Map<string> = {};
959957
var identifierCount = 0;
960958
var nodeCount = 0;
@@ -1136,10 +1134,6 @@ module ts {
11361134
parseErrorAtPosition(pos, 0, message);
11371135
}
11381136

1139-
function onComment(pos: number, end: number) {
1140-
if (commentRanges) commentRanges.push({ pos: pos, end: end });
1141-
}
1142-
11431137
function getNodePos(): number {
11441138
return scanner.getStartPos();
11451139
}
@@ -4173,14 +4167,25 @@ module ts {
41734167
}
41744168

41754169
function processReferenceComments(): ReferenceComments {
4170+
var triviaScanner = createScanner(languageVersion, /*skipTrivia*/false, sourceText);
41764171
var referencedFiles: FileReference[] = [];
41774172
var amdDependencies: string[] = [];
41784173
var amdModuleName: string;
4179-
commentRanges = [];
4180-
token = scanner.scan();
41814174

4182-
for (var i = 0; i < commentRanges.length; i++) {
4183-
var range = commentRanges[i];
4175+
// Keep scanning all the leading trivia in the file until we get to something that
4176+
// isn't trivia. Any single line comment will be analyzed to see if it is a
4177+
// reference comment.
4178+
while (true) {
4179+
var kind = triviaScanner.scan();
4180+
if (kind === SyntaxKind.WhitespaceTrivia || kind === SyntaxKind.NewLineTrivia || kind === SyntaxKind.MultiLineCommentTrivia) {
4181+
continue;
4182+
}
4183+
if (kind !== SyntaxKind.SingleLineCommentTrivia) {
4184+
break;
4185+
}
4186+
4187+
var range = { pos: triviaScanner.getTokenPos(), end: triviaScanner.getTextPos() };
4188+
41844189
var comment = sourceText.substring(range.pos, range.end);
41854190
var referencePathMatchResult = getFileReferenceFromReferencePath(comment, range);
41864191
if (referencePathMatchResult) {
@@ -4211,7 +4216,7 @@ module ts {
42114216
}
42124217
}
42134218
}
4214-
commentRanges = undefined;
4219+
42154220
return {
42164221
referencedFiles,
42174222
amdDependencies,
@@ -4247,7 +4252,6 @@ module ts {
42474252
return syntacticDiagnostics;
42484253
}
42494254

4250-
scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError, onComment);
42514255
var rootNodeFlags: NodeFlags = 0;
42524256
if (fileExtensionIs(filename, ".d.ts")) {
42534257
rootNodeFlags = NodeFlags.DeclarationFile;
@@ -4273,6 +4277,10 @@ module ts {
42734277
sourceFile.amdDependencies = referenceComments.amdDependencies;
42744278
sourceFile.amdModuleName = referenceComments.amdModuleName;
42754279

4280+
// Create and prime the scanner before parsing the source elements.
4281+
var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError);
4282+
nextToken();
4283+
42764284
sourceFile.statements = parseList(ParsingContext.SourceElements, /*checkForStrictMode*/ true, parseSourceElement);
42774285
Debug.assert(token === SyntaxKind.EndOfFileToken);
42784286
sourceFile.endOfFileToken = parseTokenNode();

src/compiler/scanner.ts

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

466-
export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback, onComment?: CommentCallback): Scanner {
466+
export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback): Scanner {
467467
var pos: number; // Current position (end position of text of current token)
468468
var len: number; // Length of text
469469
var startPos: number; // Start position of whitespace before current token
@@ -899,9 +899,6 @@ module ts {
899899
pos++;
900900

901901
}
902-
if (onComment) {
903-
onComment(tokenPos, pos);
904-
}
905902

906903
if (skipTrivia) {
907904
continue;
@@ -934,10 +931,6 @@ module ts {
934931
error(Diagnostics.Asterisk_Slash_expected);
935932
}
936933

937-
if (onComment) {
938-
onComment(tokenPos, pos);
939-
}
940-
941934
if (skipTrivia) {
942935
continue;
943936
}

0 commit comments

Comments
 (0)