If comments are safe, then why doesn't `x = 0;

2019-04-01 06:50发布

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.

5条回答
We Are One
2楼-- · 2019-04-01 06:55

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".

查看更多
Explosion°爆炸
3楼-- · 2019-04-01 07:11

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.

查看更多
Animai°情兽
4楼-- · 2019-04-01 07:13

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

查看更多
太酷不给撩
5楼-- · 2019-04-01 07:17

Because comments are parsed at the lexical level, generally considered as whitespace.

查看更多
孤傲高冷的网名
6楼-- · 2019-04-01 07:20

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.

查看更多
登录 后发表回答