ECMAScript 脚本或模块的源文本首先被转换为一串输入元素,这些元素是 tokens、line terminators、comments 或 white space。源文本从左至右扫描,反复取最长的代码点序列作为下一个输入元素。
The source text of an ECMAScript Script or Module is first converted into a sequence of input elements, which are tokens, line terminators, comments, or white space. The source text is scanned from left to right, repeatedly taking the longest possible sequence of code points as the next input element.
有几种情况,词法输入元素的识别对消耗输入元素的语法语境很敏感。这要求词法具有多个目标符号。InputElementRegExpOrTemplateTail 目标用于允许使用 RegularExpressionLiteral,TemplateMiddle 或TemplateTail 的语法上下文中。InputElementRegExp 目标符号用于所有允许使用 RegularExpressionLiteral 但不允许使用 TemplateMiddle 或 TemplateTail 的语法上下文中。InputElementTemplateTail 目标用于所有允许使用 TemplateMiddle 或 TemplateTail 但不允许使用 RegularExpressionLiteral 的语法语境中。在所有其他语境中,InputElementDiv 被用作词法目标符号。
There are several situations where the identification of lexical input elements is sensitive to the syntactic grammar context that is consuming the input elements. This requires multiple goal symbols for the lexical grammar. The InputElementRegExpOrTemplateTail goal is used in syntactic grammar contexts where a RegularExpressionLiteral, a TemplateMiddle, or a TemplateTail is permitted. The InputElementRegExp goal symbol is used in all syntactic grammar contexts where a RegularExpressionLiteral is permitted but neither a TemplateMiddle, nor a TemplateTail is permitted. The InputElementTemplateTail goal is used in all syntactic grammar contexts where a TemplateMiddle or a TemplateTail is permitted but a RegularExpressionLiteral is not permitted. In all other contexts, InputElementDiv is used as the lexical goal symbol.
NOTE
使用多个词法目标可确保不存在会影响自动分号插入的词法歧义。 例如,没有语法上下文允许同时使用前导除法或除法分配(division-assignment)以及前导 RegularExpressionLiteral。这不受分号插入的影响(请参见 11.9)。在如下示例中:
a = b
/hi/g.exec(c).map(d);
如果 LineTerminator 之后的第一个非空白,非注释代码点为 U+002F(SOLIDUS),并且语法上下文允许进行除法或除法分配,则LineTerminator上不会插入分号。 也就是说,以上示例会被解释为:
a = b / hi / g.exec(c).map(d);
The use of multiple lexical goals ensures that there are no lexical ambiguities that would affect automatic semicolon insertion. For example, there are no syntactic grammar contexts where both a leading division or division-assignment, and a leading RegularExpressionLiteral are permitted. This is not affected by semicolon insertion (see 11.9); in examples such as the following:
a = b /hi/g.exec(c).map(d);
where the first non-whitespace, non-comment code point after a LineTerminator is U+002F (SOLIDUS) and the syntactic context allows division or division-assignment, no semicolon is inserted at the LineTerminator. That is, the above example is interpreted in the same way as:
a = b / hi / g.exec(c).map(d);
Syntax(语法)
InputElementDiv ::
WhiteSpace
LineTerminator
Comment
CommonToken
DivPunctuator
RightBracePunctuator
InputElementRegExp ::
WhiteSpace
LineTerminator
Comment
CommonToken
RightBracePunctuator
RegularExpressionLiteral
InputElementRegExpOrTemplateTail ::
WhiteSpace
LineTerminator
Comment
CommonToken
RegularExpressionLiteral
TemplateSubstitutionTail
InputElementTemplateTail ::
WhiteSpace
LineTerminator
Comment
CommonToken
DivPunctuator
TemplateSubstitutionTail**
