问题
This thread inspired the question. Here are the code samples again. I'm looking for an answer that tells exactly what is going on.
Both x = 0; x+/*cmt*/+;
and var f/*cmt*/oo = 'foo';
produce syntax errors, which renders the answers in this question wrong.
回答1:
From ECMAScript reference :
Comments behave like white space and are discarded except that, if a MultiLineComment contains a line terminator character, then the entire comment is considered to be a LineTerminator for purposes of parsing by the syntactic grammar.
回答2:
You're interrupting a word instead of a sentence. ++ and foo are words. People assume you won't be interrupting those.
Much the same as you can't put whitespace in the middle of words even though whitespace is "safe".
回答3:
Because comments are parsed at the lexical level, generally considered as whitespace.
回答4:
When compiling, the first step is to lexically break it up into individual tokens. Comments are one type of token, and operators are another. You're splitting the ++ operator token so that it's interpretted as two separate items.
回答5:
As many others have pointed out, the lexical parsing determines how things will become.
Let me point out some example:
ax + ay - 0x01; /* hello */
^----^---------------------- Identifier (variables)
^----^------------------- Operator
^----------------- literal constant (int)
^------------- Statement separator
^-^--^-^--- ^------------ Whitespace (ignored)
[_________]- Comments (ignored)
So the resulting token list will be:
identifier("ax");
operator("+");
identifier("ay");
operator("-");
const((int)0x01);
separator();
But if you do this:
a/* hello */x + ay - 0x01;
^-----------^---^----------- Identifier (variables)
^----^-------- Operator
^------ literal constant (int)
^-- Statement separator
^-^--^-^------- Whitespace (ignored)
[_________]---------------- Comments (ignored)
The resulting token list will be:
identifier("a");
identifier("x"); // Error: Unexpected identifier `x` at line whatever
operator("+");
identifier("ay");
operator("-");
const((int)0x01);
separator();
Then same happens when comments inserted inside an operator.
So you can see that comments behave just like whitespace.
In fact, I recently just read an article on writing a simple interpreter with JavaScript. It helped me with this answer. http://www.codeproject.com/Articles/345888/How-to-write-a-simple-interpreter-in-JavaScript
来源:https://stackoverflow.com/questions/12659275/if-comments-are-safe-then-why-doesnt-x-0-x-cmt-or-var-f-cmt-oo