Mike Samuel wrote:
> 2009/11/19 David-Sarah Hopwood <[email protected]>:
>> Mike Samuel wrote:
>>> 2009/11/18  <[email protected]>:
>>>
>>>> ES5 does allow semicolon insertion in a prologue; see
>>>> https://mail.mozilla.org/pipermail/es5-discuss/2009-September/003246.html
[...]
>> I was thinking that if you restricted directive prologues in Valija,
>> then it would become easier to test whether a given Valija program is
>> in the Cajita subset.
>>
>> However, because Valija allows semicolon insertion according to the
>> ES5 specs, I don't think this helps, because you can't tell where a
>> prologue ends before having done the semicolon insertion.
>>
>> What you could do, to avoid duplicating the semi insertion logic, is
>> parse the program or function body to an AST, then in a separate pass
>> determine whether it is Cajita. That would require keeping enough
>> information in the AST to tell whether a string literal could be a
>> directive (i.e. has no escapes nor line continuations).

It looks like StringLiteral
<http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/parser/js/StringLiteral.java>
preserves the original source literal, so that test would be:

  /**
   * Whether this StringLiteral includes escapes or line continuations,
   * that would make it invalid as a directive in a Directive Prologue.
   * See ES5 section 14.1.
   */
  public boolean hasEscapesOrLineContinuations() {
    String betweenQuotes = jsLiteral.substring(1, jsLiteral.length() - 1);
    return !unescapeJsString(betweenQuotes).equals(betweenQuotes);
  }

> So we would preserve information in the AST about whether a semicolon
> was explicit or inserted?

I don't think that's necessary, because the definition of a directive
prologue doesn't depend on that. What I was suggesting was to either:

 - rewrite <ExpressionStmt>s in a directive prologue to <Directive>s, or
 - eliminate <Directive> and <DirectivePrologue> as separate constructs,
   and detect whether an <ExpressionStmt> is a directive in the places
   where <Directive> was used.

I know that's quite a bit of work, but I can't see another alternative
that is completely correct.

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to