简体   繁体   English

EcmaScript是否有任何规则在解析期间空白对于编译器很重要

[英]Does EcmaScript have any rule where whitespace is important for the compiler during parsing

Python uses whitespace to denote blocks: Python使用空格表示块:

for x in range(0, 50):
    print x

In JavaScript we use curly braces for that purpose: 在JavaScript中,我们为此使用花括号:

for (x in range(0, 50)) { print(x) }

I'm wondering if whitespaces has any significance in EcmaScript when parsing a program and creating AST? 我想知道在解析程序和创建AST时,空格是否在EcmaScript中有任何意义?

I've looked at the sources of TypeScript compiler and it seems to be ignoring whitespace when parsing the source code. 我看过TypeScript编译器的源代码,并且在解析源代码时似乎忽略了空格。

As @Bergi noted, the whitespace is important during lexical analysis which allows a scanner to know when a particular token ends. 正如@Bergi所指出的,空格在词法分析过程中很重要,它使扫描程序可以知道特定标记何时结束。 For example, this is what allow distinguishing newObject from new Object . 例如,这就是将newObjectnew Object区别开来的原因。 It's important for the productions that may not contain whitespaces. 这对于可能不包含空格的作品非常重要。 For example, since space cannot be derived from IdentifierPart it marks the end of the Identifier token . 例如,由于不能从IdentifierPart派生空间,因此它标记了Identifier令牌的结尾。 Whitespace is also defined as a separate production for all goal symbols, starting with the simplest one InputElementDiv : 空格也定义为所有目标符号的独立产生,从最简单的一个InputElementDiv

InputElementDiv::
    WhiteSpace

During syntactic analysis only the line-terminators are important for the automatic semicolon insertion process. 在语法分析过程中,只有行终止符对于自动分号插入过程很重要。 The rest of the white space is not relevant. 其余空白不相关。 Here is the quote from the spec : 这是规格的报价:

Moreover, line terminators, although not considered to be tokens, also become part of the stream of input elements and guide the process of automatic semicolon insertion (11.9). 此外,行终止符尽管不被视为标记,但也成为输入元素流的一部分,并指导自动分号插入的过程(11.9)。 Simple white space and single-line comments are discarded and do not appear in the stream of input elements for the syntactic grammar. 简单的空格和单行注释将被丢弃,并且不会出现在语法语法的输入元素流中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM