perf: minor tokenizer tweaks (#13652)

This commit is contained in:
Mickey Rose
2021-08-09 21:20:44 +02:00
committed by GitHub
parent 8a09993e39
commit da1d166ea6
4 changed files with 34 additions and 21 deletions

View File

@@ -23,6 +23,28 @@ export function isNewLine(code: number): boolean {
export const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
export const skipWhiteSpaceInLine =
/(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
// Skip whitespace and single-line comments, including /* no newline here */.
// After this RegExp matches, its lastIndex points to a line terminator, or
// the start of multi-line comment (which is effectively a line terminator),
// or the end of string.
export const skipWhiteSpaceToLineBreak = new RegExp(
// Unfortunately JS doesn't support Perl's atomic /(?>pattern)/ or
// possessive quantifiers, so we use a trick to prevent backtracking
// when the look-ahead for line terminator fails.
"(?=(" +
// Capture the whitespace and comments that should be skipped inside
// a look-ahead assertion, and then re-match the group as a unit.
skipWhiteSpaceInLine.source +
"))\\1" +
// Look-ahead for either line terminator, start of multi-line comment,
// or end of string.
/(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source,
"y", // sticky
);
// https://tc39.github.io/ecma262/#sec-white-space
export function isWhitespace(code: number): boolean {
switch (code) {