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

@ -4141,8 +4141,15 @@ Parser<ParseHandler>::statementList(YieldHandling yieldHandling)
return null();
bool canHaveDirectives = pc->atBodyLevel();
if (canHaveDirectives)
if (canHaveDirectives) {
tokenStream.clearSawOctalEscape();
}
bool canHaveHashbangComment = pc->atTopLevel();
if (canHaveHashbangComment) {
tokenStream.consumeOptionalHashbangComment();
}
bool afterReturn = false;
bool warnedAboutStatementsAfterReturn = false;
uint32_t statementBegin = 0;

View file

@ -470,6 +470,12 @@ class ParseContext : public Nestable<ParseContext>
return atBodyLevel() && sc_->isModuleContext();
}
// True if we are at the topmost level of an entire script or module. For
// example, in the comment on |atBodyLevel()| above, we would encounter |f1|
// and the outermost |if (cond)| at top level, and everything else would not
// be at top level.
bool atTopLevel() { return atBodyLevel() && sc_->isTopLevelContext(); }
void setIsStandaloneFunctionBody() {
isStandaloneFunctionBody_ = true;
}

View file

@ -289,6 +289,16 @@ class SharedContext
bool isEvalContext() { return kind_ == Kind::Eval; }
inline EvalSharedContext* asEvalContext();
bool isTopLevelContext() const {
switch (kind_) {
case Kind::Module:
case Kind::Global:
case Kind::Eval:
return true;
}
return false;
}
ThisBinding thisBinding() const { return thisBinding_; }
bool hasModuleGoal() const { return hasModuleGoal_; }

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

View file

@ -428,6 +428,13 @@ class MOZ_STACK_CLASS TokenStream
// asm.js reporter
void reportAsmJSError(uint32_t offset, unsigned errorNumber, ...);
/**
* Consume any hashbang comment at the start of a Script or Module, if one is
* present. Stops consuming just before any terminating LineTerminator or
* before an encoding error is encountered.
*/
void consumeOptionalHashbangComment();
JSAtom* getRawTemplateStringAtom() {
MOZ_ASSERT(currentToken().type == TOK_TEMPLATE_HEAD ||
currentToken().type == TOK_NO_SUBS_TEMPLATE);