Annex A of 5th Edition

Brendan Eich brendan at mozilla.com
Thu Aug 30 07:15:39 PDT 2012


>From "Joseph Spencer" <js.developer.undefined at gmail.com>
> 
> ExpressionStatement:  
> Is the prevention of the token { an error in this context?  Initially I
> thought it was to give parse flow over to the Block Production, until I
> realized that the following 'should' be a valid ExpressionStatement:
> {a:5}?5:4;

The spec uses lookahead restrictions on top of an LR(1) validated (ES3 era, needs revalidation) grammar. This is by design, not an error, and yes, it intentionally makes expression statements such as

 {lol:42} ? "always" : "never";

be early errors.

The simplicity and tried-and-true LR validation are worth it. Anyone being clever can parenthesize. But of course your example is contrived, and it's rare but not unheared of to want { at start of an expression statement.

Same goes for 'function', and IME that lookahead restriction bites a bit more often, but people know to parenthesize.

/be


More information about the es-discuss mailing list