Skip to content

Introduce scanning fast paths #59244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1882,17 +1882,10 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}

const ch = codePointUnchecked(pos);
if (pos === 0) {
// Special handling for shebang
if (ch === CharacterCodes.hash && isShebangTrivia(text, pos)) {
pos = scanShebangTrivia(text, pos);
if (skipTrivia) {
continue;
}
else {
return token = SyntaxKind.ShebangTrivia;
}
}

if (CharacterCodes.a <= (ch | 32) && (ch | 32) <= CharacterCodes.z) {
const identifierKind = scanIdentifier(ch, languageVersion);
return identifierKind!;
}

switch (ch) {
Expand All @@ -1913,10 +1906,10 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}
return token = SyntaxKind.NewLineTrivia;
}
case CharacterCodes.space:
case CharacterCodes.tab:
case CharacterCodes.verticalTab:
case CharacterCodes.formFeed:
case CharacterCodes.space:
case CharacterCodes.nonBreakingSpace:
case CharacterCodes.ogham:
case CharacterCodes.enQuad:
Expand Down Expand Up @@ -2302,13 +2295,24 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
pos++;
return token = SyntaxKind.Unknown;
case CharacterCodes.hash:
if (pos !== 0 && text[pos + 1] === "!") {
error(Diagnostics.can_only_be_used_at_the_start_of_a_file, pos, 2);
pos++;
return token = SyntaxKind.Unknown;
// Special handling for shebang
const charAfterHash = codePointUnchecked(pos + 1);
if (charAfterHash === CharacterCodes.exclamation) {
if (pos !== 0) {
error(Diagnostics.can_only_be_used_at_the_start_of_a_file, pos, 2);
pos++;
return token = SyntaxKind.Unknown;
}

pos = scanShebangTrivia(text, pos);
if (skipTrivia) {
continue;
}
else {
return token = SyntaxKind.ShebangTrivia;
}
}

const charAfterHash = codePointUnchecked(pos + 1);
if (charAfterHash === CharacterCodes.backslash) {
pos++;
const extendedCookedChar = peekExtendedUnicodeEscape();
Expand Down