Issue #2559 - Implement hashbang grammar

This adds special-case #! comment handling at the top level, treating
any hashbang line as a single-line comment in the parser.
This only applies to Eval(), module script or global context.
This commit is contained in:
Moonchild 2024-08-29 11:40:42 +02:00 committed by roytam1
commit 7cc9551e79
5 changed files with 46 additions and 1 deletions

View file

@ -1300,6 +1300,21 @@ TokenStream::putIdentInTokenbuf(const char16_t* identStart)
return true;
}
void
TokenStream::consumeOptionalHashbangComment() {
int c = userbuf.getRawChar();
if (c == '#') {
if (matchChar('!')) {
// Hashbang; ignore rest of line as comment.
while ((c = getChar()) != EOF && c != '\n')
continue;
}
}
ungetChar(c);
cursor = (cursor - 1) & ntokensMask;
}
enum FirstCharKind {
// A char16_t has the 'OneChar' kind if it, by itself, constitutes a valid
// token that cannot also be a prefix of a longer token. E.g. ';' has the