1

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 by Identifier a divided by Identifier b or
  • Block followed by RegularExpressionLiteral /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?

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • What makes you think {} is an expression, or that this is just a single statement? An opening brace is usually interpreted as the start of a block. – Sacho Nov 24 '14 at 11:11

1 Answers1

3

No, {} is not an expression in this case. Quoting the relevant parts of the annotated standard: http://es5.github.io/#x12.4 "An ExpressionStatement cannot start with an opening curly brace because that might make it ambiguous with a Block". In your case, {}/a/g is a block, followed by an expression statement consisting of a regular expression literal.

Sacho
  • 2,169
  • 1
  • 14
  • 13