Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26342

Re: Optional semicolons - wrong rule or wrong example?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Optional semicolons - wrong rule or wrong example?
Date 2014-12-17 00:10 +0100
Organization PointedEars Software (PES)
Message-ID <4276616.1vbvTFgubW@PointedEars.de> (permalink)
References <4ca7db7d-5a3b-457d-8601-aa9c72afab36@googlegroups.com>

Show all headers | View raw


Peter wrote:
^^^^^
This is Usenet; welcome.  Please append your last name, Peter #74656.
And avoid the buggy Google Groups interface if you can.

> In the 6th edition of "JavaScript: The Definitive Guide",
> in chapter 2.5 (Optional Semicolons), the following rule
> can be found:
> 
> "JavaScript treats a line break as a semicolon if the next
> nonspace character cannot be interpreted as a continuation of
> the current statement".
> 
> and the following example is given:
> 
> var a
> a
> =
> 3
> console.log(a)
> 
> According to the author, the above shall be treated as:
> 
> var a; a = 3; console.log(a);

It is.

> He justifies it as follows:
> 
> "JavaScript does treat the first line break as a semicolon because it
> cannot parse the code var a a without a semicolon."

Correct, but imprecise.

> but I can't agree with his claim. To start with,
> why does he assume these two lines:
> 
> var a
> a
> 
> would be interpreted as:
> 
> "var a a" instead of "var aa"?

Common sense already dictates that.  The newline is a separator; one must 
assume it has meaning.  Simply ignoring the separator would change the 
meaning of the source code, which is not a good idea.

> Where would the extra space between two characters "a" come from?

Newline, see above.

> I'm not saying he's wrong, but the rule says nothing about it.

He is wrong on several other things, though.  Take this book with a handful 
of salt.  It is only referred by the FAQ as the best of the bad ones (in the 
opinion of some, excluding me), and none have been recommended as "good" to 
date.
 
> Let's quote it once again and try to apply to our example,
> step by step:
> 
> "JavaScript treats a line break as a semicolon if the next
> nonspace character cannot be interpreted as a continuation of
> the current statement".

First of all, know that the author of that book does not have a clue what 
JavaScript is.  Somewhat of a result, neither do you – yet.

He is writing „JavaScript“, but he is actually describing several ECMAScript 
implementations in Web browsers which may or may not have all of the 
described features in common, without being aware of it.  He is _not_ 
describing several other ECMAScript implementations, and may be wrong with 
regard to the common features of those that he thinks he is describing, in 
Web browsers and elsewhere, being blissfully unaware of it.  Somewhat of a 
result, so were you – until now.

> I see no reason why those three nonspace characters following "var a"

If you think about it a bit more, there are more than three of them.

> cannot be interpreted as a continuation of it. Which part of
> the rule would it violate to concatenate them with the current statement
> ("var a") to form "var aa=3"?

Nothing; see the bottom of this posting for an explanation.

> Nothing in the rule seems to force an implicit semicolon right after "var 
> a".

Correct.  The claimed rule is wrong.

> To make a long story short, why is the discussed example
> supposed to be interpreted as:
> 
> var a; a = 3; console.log(a);
> 
> instead of:
> 
> var aa = 3; console.log(a); ?

Let us assume that common sense is applied and the newline is parsed as if 
it was a space character.  This leaves us with

  var a a

Now, the rule(s) that dictate(s) that this must be parsed as

  var a; a

instead when the separator *was* a newline, can be found in the 
ECMAScript(!) Language Specification and its grammar (the HTML rendering of 
it is referred here; see the top of the document for the definitive [PDF] 
version):

,-<http://ecma-international.org/ecma-262/5.1/#sec-12.2>
|
| VariableStatement :
|   var VariableDeclarationList ;
|
| VariableDeclarationList :
|   VariableDeclaration
|   VariableDeclarationList , VariableDeclaration
|
| VariableDeclarationListNoIn :
|   VariableDeclarationNoIn
|   VariableDeclarationListNoIn , VariableDeclarationNoIn
|
| VariableDeclaration :
|   Identifier Initialiser_opt
| 
| VariableDeclarationNoIn : Identifier InitialiserNoIn_opt
| 
| Initialiser : = AssignmentExpression
| 
| InitialiserNoIn : = AssignmentExpressionNoIn

Because of the “var” keyword, the VariableStatement production is to be 
applied.  The second “a” cannot be produced by the Initialiser or 
InitialiserNoIn production.  As a result, a syntactical correct statement 
can only be produced by

    VariableStatement
  → "var" VariableDeclarationList    ";"
  → "var" VariableDeclaration        ";"
  → "var" Identifier Initialiser_opt ";"
  → "var" Identifier                 ";"
  → "var" "a"                        ";"
  → "var a;"

So the semicolon is inserted after “var a”.

The same reasoning, based on different rules of the grammar, can be applied 
to the other two instances. 

(For clarity, I have exchanged the usual /Goal/ markup for marking up 
"Literals" instead.)

> It looks like either the rule is wrong/incomplete or the example
> is wrong.

The claimed rule is wrong because of oversimplification.  You can find such 
often in such books, particularly in those that contain “definitive” in 
their title.

> Can you please give the correct rule for optional semicolons

<http://ecma-international.org/ecma-262/5.1/#sec-7.9>, rule 1.

> and apply it to my example, step by step?

[x] done

Next time, do not ask a Yes-or-No question unless you want a Yes-or-No 
answer.

<http://www.catb.org/~esr/faqs/smart-questions.html>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Optional semicolons - wrong rule or wrong example? Peter <pilarp@poczta.onet.pl> - 2014-12-16 13:58 -0800
  Re: Optional semicolons - wrong rule or wrong example? "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-16 23:21 +0100
  Re: Optional semicolons - wrong rule or wrong example? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-12-17 00:10 +0100
    JavaScript: The Definitive Guide (was: Optional semicolons - wrong rule or wrong example?) "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-17 01:29 +0100
      Re: JavaScript: The Definitive Guide Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-12-20 23:00 +0100
        Re: JavaScript: The Definitive Guide Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-12-20 23:04 +0100
        Re: JavaScript: The Definitive Guide "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-12-28 23:52 +0100
          Re: JavaScript: The Definitive Guide Tim Streater <timstreater@greenbee.net> - 2014-12-28 23:52 +0000
            Re: JavaScript: The Definitive Guide Scott Sauyet <scott.sauyet@gmail.com> - 2015-01-01 21:46 -0800
              Re: JavaScript: The Definitive Guide "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-02 09:32 +0100
                Re: JavaScript: The Definitive Guide Andrew Poulos <ap_prog@hotmail.com> - 2015-01-03 07:54 +1100
                Re: JavaScript: The Definitive Guide "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-02 22:14 +0100
                Re: JavaScript: The Definitive Guide Andrew Poulos <ap_prog@hotmail.com> - 2015-01-03 11:46 +1100
                Re: JavaScript: The Definitive Guide "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-03 11:59 +0100
                Re: JavaScript: The Definitive Guide Andrew Poulos <ap_prog@hotmail.com> - 2015-01-03 23:46 +1100
                Re: JavaScript: The Definitive Guide "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-03 14:57 +0100
                Re: JavaScript: The Definitive Guide Andrew Poulos <ap_prog@hotmail.com> - 2015-01-04 09:31 +1100
                Re: JavaScript: The Definitive Guide "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-04 00:29 +0100
                Re: JavaScript: The Definitive Guide Andrew Poulos <ap_prog@hotmail.com> - 2015-01-04 20:17 +1100
                Re: JavaScript: The Definitive Guide "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-04 10:57 +0100
                OT: Mass, weight and friction (was: JavaScript: The Definitive Guide) "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-03 02:52 +0100
  Re: Optional semicolons - wrong rule or wrong example? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2014-12-18 14:39 +0100
    Re: Optional semicolons - wrong rule or wrong example? Tim Streater <timstreater@greenbee.net> - 2014-12-18 14:04 +0000

csiph-web