On Aug 30, 6:20 am, Thomas 'PointedEars' Lahn <
PointedE...@web.de>
wrote:
> BGB wrote:
> > On 8/29/2012 1:56 AM, RobG wrote:
> >> The syntax for the do..while statement (ECMA-262 12.6.1[1]) is:
>
> >> do /Statement/ while ( /Expression/ );
>
> >> Noting the trailing semicolon, I expected the following to throw an
> >> error:
>
> >> do {} while (false) var x = 3;
>
> >> since there is no semicolon to separate the two statements. However,
> >> I've tested a number of browsers and no error is thrown. So either the
> >> browsers I tested are all tolerant of the error (possible in one or
> >> two but unlikely in half a dozen) or automatic semicolon insertion is
> >> at work.
[..]
[..]
>
> What actually happens is very simple:
>
> ,-[ECMA-262, 5.1 Edition]
> |
> | 7.9 Automatic Semicolon Insertion
> |
> | Certain ECMAScript statements (empty statement, variable statement,
> | expression statement, do-while statement, continue statement, break
> | statement, return statement, and throw statement) must be terminated with
> | semicolons. Such semicolons may always appear explicitly in the source
> ^^^ ^^^^^^^^^^^^^^^^^
> | text. For convenience, however, such semicolons may be omitted from the
> ^^^^^^^^^^^^^^
> | source text in certain situations. These situations are described by
> | saying that semicolons are automatically inserted into the source code
> ^^^^^^^^^^^^^^^^^^^^^^^^^^ (MUST)
> | token stream in those situations.
> ^^^^^^^^^^^^^^^^^^^
Since "certain situations" are not explained (other than by the
circuitous logic that they're those where semicolons are inserted), I
understood that section as a preamble to 7.9.1 which sets out a number
of "certain situations" and 3 rules for insertion. It is those 3 rules
that don't cover the do..while case I posted, they seem to require
either a new line or that a '}' is involved in order to insert a
semicolon.
e.g. 7.9.1 #1 covers the following cases:
var x = 3
var y = 4
A semicolon is inserted after the first line because it's followed by
a newline character and then something that can't be part of the
variable declaration statement.
(function(){alert('hi')})
--------------------------^
The missing semicolon indicated is inserted because the following
character is '}' and a semicolon is inserted at the end because it's
the end of the input stream.
> IOW,
>
> do {} while (false) var x = 3;
>
> *is* equivalent to
>
> do {} while (false); var x = 3;
>
> because `var' delimits a /VariableStatement/ and the parser is allowed, in
> order to produce
>
> /Program/
> → /SourceElements_opt/
> → /SourceElements/ /SourceElement/
> → /SourceElement/ /SourceElement/
> → /Statement/ /Statement/
> → /IterationStatement/ /VariableStatement/
> → do /Statement/ while ( /Expression/ ); /VariableStatement/
> → do /Block/ while ( /Expression/ ); /VariableStatement/
> → …
> → do {} while ( true ); var x = 3;
>
> to insert the semicolon before the /VariableStatement/. It's not a bug,
> it's a feature, and a standards-compliant feature at that.
I suppose I'm still missing something because my understanding of this
explanation is that a semicolon should be inserted for
/VariableStatement/ /VariableStatement/
as it's one of the "certain statements", but it isn't. If the newline
is removed from the variable statement example above:
var x = 3 var y = 4
then a syntax error is thrown. I can't see how the second "var" can be
interpreted as anything other than the start of a variable statement,
and therefore that a semicolon can (or should) be inserted before it
(per your logic above).
The only explanation I can think of is that the "v" at the start of
the second "var" is interpreted as the start of an identifier, so
either a semicolon, comma[1] or newline (which would force semicolon
insertion) is required. Because of the ambiguity, an error results, so
the whole "var" token is not read and the second variable statement
isn't identified.
1. Allowing for the fact that if a comma is inserted, it only gets one
step further because the var keyword can only appear at the start of a
variable declaration statement.
Regarding references, I use the Github version of ES5 because it also
links to other resources and has some handy features that aren't
available in the ECMA version. As far as I know, the text is unaltered
from the official version.
--
Rob