If comments are safe, then why doesn't `x = 0; x+/*cmt*/+;` or `var f/*cmt*/oo = 'foo';` work?

拥有回忆 提交于 2019-12-03 20:10:37

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.

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

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!