From 2510a5f9075deba01b1df5a92c34ece4c32b5133 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Thu, 4 Dec 2014 06:40:36 -0800 Subject: [PATCH] Simplify scanner by removing need for a 'onComment' callback. --- src/compiler/parser.ts | 32 ++++++++++++++++++++------------ src/compiler/scanner.ts | 9 +-------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 58c715d721f..b3aaef75096 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -951,10 +951,8 @@ module ts { } export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile { - var scanner: Scanner; var token: SyntaxKind; var parsingContext: ParsingContext; - var commentRanges: TextRange[]; var identifiers: Map = {}; var identifierCount = 0; var nodeCount = 0; @@ -1136,10 +1134,6 @@ module ts { parseErrorAtPosition(pos, 0, message); } - function onComment(pos: number, end: number) { - if (commentRanges) commentRanges.push({ pos: pos, end: end }); - } - function getNodePos(): number { return scanner.getStartPos(); } @@ -4173,14 +4167,25 @@ module ts { } function processReferenceComments(): ReferenceComments { + var triviaScanner = createScanner(languageVersion, /*skipTrivia*/false, sourceText); var referencedFiles: FileReference[] = []; var amdDependencies: string[] = []; var amdModuleName: string; - commentRanges = []; - token = scanner.scan(); - for (var i = 0; i < commentRanges.length; i++) { - var range = commentRanges[i]; + // Keep scanning all the leading trivia in the file until we get to something that + // isn't trivia. Any single line comment will be analyzed to see if it is a + // reference comment. + while (true) { + var kind = triviaScanner.scan(); + if (kind === SyntaxKind.WhitespaceTrivia || kind === SyntaxKind.NewLineTrivia || kind === SyntaxKind.MultiLineCommentTrivia) { + continue; + } + if (kind !== SyntaxKind.SingleLineCommentTrivia) { + break; + } + + var range = { pos: triviaScanner.getTokenPos(), end: triviaScanner.getTextPos() }; + var comment = sourceText.substring(range.pos, range.end); var referencePathMatchResult = getFileReferenceFromReferencePath(comment, range); if (referencePathMatchResult) { @@ -4211,7 +4216,7 @@ module ts { } } } - commentRanges = undefined; + return { referencedFiles, amdDependencies, @@ -4247,7 +4252,6 @@ module ts { return syntacticDiagnostics; } - scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError, onComment); var rootNodeFlags: NodeFlags = 0; if (fileExtensionIs(filename, ".d.ts")) { rootNodeFlags = NodeFlags.DeclarationFile; @@ -4273,6 +4277,10 @@ module ts { sourceFile.amdDependencies = referenceComments.amdDependencies; sourceFile.amdModuleName = referenceComments.amdModuleName; + // Create and prime the scanner before parsing the source elements. + var scanner = createScanner(languageVersion, /*skipTrivia*/ true, sourceText, scanError); + nextToken(); + sourceFile.statements = parseList(ParsingContext.SourceElements, /*checkForStrictMode*/ true, parseSourceElement); Debug.assert(token === SyntaxKind.EndOfFileToken); sourceFile.endOfFileToken = parseTokenNode(); diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index fcb8c9b0c31..9b20480a36a 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -463,7 +463,7 @@ module ts { ch > CharacterCodes.maxAsciiCharacter && isUnicodeIdentifierPart(ch, languageVersion); } - export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback, onComment?: CommentCallback): Scanner { + export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback): Scanner { var pos: number; // Current position (end position of text of current token) var len: number; // Length of text var startPos: number; // Start position of whitespace before current token @@ -899,9 +899,6 @@ module ts { pos++; } - if (onComment) { - onComment(tokenPos, pos); - } if (skipTrivia) { continue; @@ -934,10 +931,6 @@ module ts { error(Diagnostics.Asterisk_Slash_expected); } - if (onComment) { - onComment(tokenPos, pos); - } - if (skipTrivia) { continue; }