Exactly where is a RegularExpressionLiteral allowed?

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Tue Mar 24 19:01:01 PDT 2009

Waldemar Horwat wrote:
> David-Sarah Hopwood wrote:
>> I'll repeat my argument here for convenience:
>>   A DivisionPunctuator must be preceded by an expression.
>>   A RegularExpressionLiteral is itself an expression.
>> (This assumes that the omission of RegularExpressionLiteral from
>> Literal is a bug.)
>>   Therefore, for there to exist syntactic contexts in which either
>>   a DivisionPunctuator or a RegularExpressionLiteral could occur,
>>   it would have to be possible for an expression to immediately
>>   follow [*] another expression with no intervening operator.
>>   The only case in which that can occur is where a semicolon is
>>   automatically inserted between the two expressions.
>>   Assume that case: then the second expression cannot begin
>>   with [*] a token whose first character is '/', because that
>>   would have been interpreted as a DivisionPunctuator, and so
>>   no semicolon insertion would have occurred (because semicolon
>>   insertion only occurs where there would otherwise have been a
>>   syntax error); contradiction.
> Yes, I verified when we were writing ES3 that this was the only case
> where the syntactic grammar permitted a / to serve as both a division
> (or division-assignment) and a regexp literal.  The interaction of
> lexing and semicolon insertion would have been unclear (how can you say
> that the next token is invalid if you don't know how to lex it?), so we
> wrote the spec to explicitly resolve those in favor of division.

If that is what the note is intended to clarify, I think its current
wording is more confusing than helpful. It certainly confused me.
Anyway, there is no case in which a regexp needs to be parenthesized
to avoid lexical ambiguity.

How about replacing the current wording by something that specifically
discusses the semicolon insertion issue, with an example:

  There are two goal symbols for the lexical grammar. The InputElementDiv
  symbol is used in those syntactic grammar contexts where a leading
  division (/) or division-assignment (/=) operator is permitted. The
  InputElementRegExp symbol is used in other syntactic grammar contexts.

  There are no syntactic grammar contexts where both a leading division
  or division-assignment, and a leading RegularExpressionLiteral are
  permitted. This is not affected by semicolon insertion (section 7.9);
  in examples such as the following:

    a = b

  where the first non-whitespace, non-comment character after a
  LineTerminator is '/' and the syntactic context allows division or
  division-assignment, no semicolon is inserted at the LineTerminator.
  That is, this example is interpreted in the same way as:

    a = b / hi / g.exec(c).map(d);

David-Sarah Hopwood ⚥

More information about the Es-discuss mailing list