Skip to content

Commit 51804ee

Browse files
Merge pull request microsoft#1367 from Microsoft/tokenRanges
Simplify scanner by removing need for a 'onComment' callback.
2 parents 824edea + 2510a5f commit 51804ee

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
@@ -945,10 +945,8 @@ module ts {
945945
}
946946

947947
export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile {
948-
var scanner: Scanner;
949948
var token: SyntaxKind;
950949
var parsingContext: ParsingContext;
951-
var commentRanges: TextRange[];
952950
var identifiers: Map<string> = {};
953951
var identifierCount = 0;
954952
var nodeCount = 0;
@@ -1128,10 +1126,6 @@ module ts {
11281126
parseErrorAtPosition(pos, 0, message);
11291127
}
11301128

1131-
function onComment(pos: number, end: number) {
1132-
if (commentRanges) commentRanges.push({ pos: pos, end: end });
1133-
}
1134-
11351129
function getNodePos(): number {
11361130
return scanner.getStartPos();
11371131
}
@@ -4201,14 +4195,25 @@ module ts {
42014195
}
42024196

42034197
function processReferenceComments(): ReferenceComments {
4198+
var triviaScanner = createScanner(languageVersion, /*skipTrivia*/false, sourceText);
42044199
var referencedFiles: FileReference[] = [];
42054200
var amdDependencies: string[] = [];
42064201
var amdModuleName: string;
4207-
commentRanges = [];
4208-
token = scanner.scan();
42094202

4210-
for (var i = 0; i < commentRanges.length; i++) {
4211-
var range = commentRanges[i];
4203+
// Keep scanning all the leading trivia in the file until we get to something that
4204+
// isn't trivia. Any single line comment will be analyzed to see if it is a
4205+
// reference comment.
4206+
while (true) {
4207+
var kind = triviaScanner.scan();
4208+
if (kind === SyntaxKind.WhitespaceTrivia || kind === SyntaxKind.NewLineTrivia || kind === SyntaxKind.MultiLineCommentTrivia) {
4209+
continue;
4210+
}
4211+
if (kind !== SyntaxKind.SingleLineCommentTrivia) {
4212+
break;
4213+
}
4214+
4215+
var range = { pos: triviaScanner.getTokenPos(), end: triviaScanner.getTextPos() };
4216+
42124217
var comment = sourceText.substring(range.pos, range.end);
42134218
var referencePathMatchResult = getFileReferenceFromReferencePath(comment, range);
42144219
if (referencePathMatchResult) {
@@ -4239,7 +4244,7 @@ module ts {
42394244
}
42404245
}
42414246
}
4242-
commentRanges = undefined;
4247+
42434248
return {
42444249
referencedFiles,
42454250
amdDependencies,
@@ -4275,7 +4280,6 @@ module ts {
42754280
return syntacticDiagnostics;
42764281
}
42774282

4278-
scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError, onComment);
42794283
var rootNodeFlags: NodeFlags = 0;
42804284
if (fileExtensionIs(filename, ".d.ts")) {
42814285
rootNodeFlags = NodeFlags.DeclarationFile;
@@ -4301,6 +4305,10 @@ module ts {
43014305
sourceFile.amdDependencies = referenceComments.amdDependencies;
43024306
sourceFile.amdModuleName = referenceComments.amdModuleName;
43034307

4308+
// Create and prime the scanner before parsing the source elements.
4309+
var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError);
4310+
nextToken();
4311+
43044312
sourceFile.statements = parseList(ParsingContext.SourceElements, /*checkForStrictMode*/ true, parseSourceElement);
43054313
Debug.assert(token === SyntaxKind.EndOfFileToken);
43064314
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)