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

蓝咒 提交于 2019-12-05 02:45:00

问题


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

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