I am writing a JavaCC-based parser for JavaScript (ECMAScript 5.1) and have an interesting corner case with regular expressions. Here it is.
Consider the following statement:
{}/a/g
My question: From the specification point of view, should it be interpreted as
ObjectLiteral
divided byIdentifier a
divided byIdentifier b
orBlock
followed byRegularExpressionLiteral /a/g
?
For instance, Chrome interprets it as the latter and other parsers as well.
From the specification point of view, is /a/g
in {}/a/g
a regex or a division?
Here is what I think to be a relevant part of the specification:
There are no syntactic grammar contexts where both a leading division or division-assignment, and a leading RegularExpressionLiteral are permitted.
Quoting another answer on the relevant question:
The division operator must follow an expression, and a regular expression literal can't follow an expression, so in all other cases you can safely assume you're looking at a regular expression literal.
My understanding is that {}
is an expression. ObjectLiteral, even an empty one is a PrimaryExpression. So the following /a/g
must be a division, not a regular expression literal.
Or am I wrong with this?