Skip to content

Instantly share code, notes, and snippets.

@KFlash
Last active October 27, 2020 08:34
Show Gist options
  • Save KFlash/fa461f7f0cd080e65227eb9a0a683938 to your computer and use it in GitHub Desktop.
Save KFlash/fa461f7f0cd080e65227eb9a0a683938 to your computer and use it in GitHub Desktop.
export function skipWhitespace(parser: ParserState, context: Context, source: string, cp: number): void {
let state = WhitespaceState.LineStart;
loop: while (parser.pos < parser.length) {
switch (whitespaceTbl[cp]) {
/* general whitespace */
case Char.Tab:
case Char.VerticalTab:
case Char.FormFeed:
case Char.Space:
state |= WhitespaceState.SameLine;
parser.pos++;
break;
/* line terminators */
case Char.CarriageReturn:
parser.columnOffset = parser.pos++;
parser.line++;
state |= WhitespaceState.NewLine | WhitespaceState.LastIsCR;
break;
case Char.LineFeed:
parser.columnOffset = parser.pos++;
parser.line++;
if (state & WhitespaceState.LastIsCR) parser.pos++;
state = (state | 0b00000000000000000000000000000101) ^ WhitespaceState.LastIsCR;
break;
/* normal comments */
case Char.Slash: {
const next = source.charCodeAt(parser.pos + 1);
if (next === Char.Slash) {
state = skipSingleLine(parser, state);
break;
}
if (next === Char.Asterisk) {
state = skipMultilineComment(parser, state);
break;
}
break loop;
}
/* HTML single line comment */
case Char.LessThan:
if (context & Context.Module) break loop;
if (
source.charCodeAt(parser.pos + 3) === Char.Hyphen &&
source.charCodeAt(parser.pos + 2) === Char.Hyphen &&
source.charCodeAt(parser.pos + 1) === Char.Exclamation
) {
state = skipSingleLine(parser, state);
break;
}
break loop;
/* HTML close */
case Char.Hyphen:
if (context & Context.Module || (state & 0b00000000000000000000000000001001) === 0) break loop;
if (
source.charCodeAt(parser.pos + 2) === Char.GreaterThan &&
source.charCodeAt(parser.pos + 1) === Char.Hyphen
) {
state = skipSingleLine(parser, state);
break;
}
default:
if ((cp & ~1) === Char.LineSeparator) {
parser.pos++;
state = (state | 0b00000000000000000000000000000101) ^ WhitespaceState.LastIsCR;
continue;
}
if (isZsSpace(cp)) {
parser.pos++;
continue;
}
break loop;
}
cp = source.charCodeAt(parser.pos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment