mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Merge pull request #1367 from Microsoft/tokenRanges
Simplify scanner by removing need for a 'onComment' callback.
This commit is contained in:
@@ -945,10 +945,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<string> = {};
|
||||
var identifierCount = 0;
|
||||
var nodeCount = 0;
|
||||
@@ -1128,10 +1126,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();
|
||||
}
|
||||
@@ -4201,14 +4195,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) {
|
||||
@@ -4239,7 +4244,7 @@ module ts {
|
||||
}
|
||||
}
|
||||
}
|
||||
commentRanges = undefined;
|
||||
|
||||
return {
|
||||
referencedFiles,
|
||||
amdDependencies,
|
||||
@@ -4275,7 +4280,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;
|
||||
@@ -4301,6 +4305,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();
|
||||
|
||||
@@ -472,7 +472,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
|
||||
@@ -908,9 +908,6 @@ module ts {
|
||||
pos++;
|
||||
|
||||
}
|
||||
if (onComment) {
|
||||
onComment(tokenPos, pos);
|
||||
}
|
||||
|
||||
if (skipTrivia) {
|
||||
continue;
|
||||
@@ -943,10 +940,6 @@ module ts {
|
||||
error(Diagnostics.Asterisk_Slash_expected);
|
||||
}
|
||||
|
||||
if (onComment) {
|
||||
onComment(tokenPos, pos);
|
||||
}
|
||||
|
||||
if (skipTrivia) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user