Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Optional semicolons - wrong rule or wrong example? Date: Wed, 17 Dec 2014 00:10:51 +0100 Organization: PointedEars Software (PES) Lines: 183 Message-ID: <4276616.1vbvTFgubW@PointedEars.de> References: <4ca7db7d-5a3b-457d-8601-aa9c72afab36@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1418771452 12770 eJwFwQkBACAIBLBKIHBKHN7+EdxMwKirMKitLTzyTocL4eXGjGgbaecbtIPzcHmIVu2QxwcxTxH0 (16 Dec 2014 23:10:52 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 16 Dec 2014 23:10:52 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNxMERACEIA8CWzEAIliMo/Zdwt4+lBaLlwXAO5wgja8MuMD1vm6qun5cFtEfV6r/HmdnV6/gSKJeIwf0AY2wV5w== Cancel-Lock: sha1:G05RYeFzwOHTBVdYRnQxJG/KFZ8= X-NNTP-Posting-Host: eJwFwYEBwCAIA7CXhqOVnQPW/n/CErwMnp0EE4ZvdkmlnO5mRxkxWyctQ6d5yfnyWR4tEj9HNRJS Xref: csiph.com comp.lang.javascript:26342 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): ,- | | 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 , 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. -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.