简体   繁体   English

`InputElementDiv`在ECMAScript词法语法中代表什么?

[英]What does `InputElementDiv` stand for in ECMAScript lexical grammar

The lexical grammar of ECMAScript lists the following token classes for lexical analyzer (lexer): ECMAScript 的词法语法列出了词法分析器(词法分析器)的以下标记类:

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

While I understand the nested classes like WhiteSpace , LineTerminator , I don't understand what the top level classes are: InputElementDiv , InputElementRegExp , InputElementRegExpOrTemplateTail and InputElementTemplateTail . 虽然我理解像WhiteSpaceLineTerminator这样的嵌套类,但我不明白顶级类是什么: InputElementDivInputElementRegExpInputElementRegExpOrTemplateTailInputElementTemplateTail Can anyone please clarify? 有人可以澄清一下吗?

Definitely not obvious, I had my own struggle decoding all this at one point. 绝对不是很明显,我一直在努力解码这一切。 The important note is in https://www.ecma-international.org/ecma-262/8.0/index.html#sec-ecmascript-language-lexical-grammar . 重要说明见https://www.ecma-international.org/ecma-262/8.0/index.html#sec-ecmascript-language-lexical-grammar Specifically: 特别:

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. InputElementRegExpOrTemplateTail目标用于语法语法上下文,其中允许使用RegularExpressionLiteral,TemplateMiddle或TemplateTail。 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. InputElementRegExp目标符号用于允许使用RegularExpressionLite的所有语法语法上下文,但不允许使用TemplateMiddle和TemplateTail。 The InputElementTemplateTail goal is used in all syntactic grammar contexts where a TemplateMiddle or a TemplateTail is permitted but a RegularExpressionLiteral is not permitted. InputElementTemplateTail目标用于允许TemplateMiddle或TemplateTail但不允许使用RegularExpressionLiteral的所有语法语法上下文中。 In all other contexts, InputElementDiv is used as the lexical goal symbol. 在所有其他上下文中,InputElementDiv用作词汇目标符号。

with the key part up front: 前面的关键部分:

There are several situations where the identification of lexical input elements is sensitive to the syntactic grammar context 在某些情况下,词汇输入元素的识别对句法语法上下文很敏感

Keep in mind that this is the lexical grammar definition, so all it aims to do is produce a set of tokens. 请记住,这是词法语法定义,因此它所要做的就是生成一组标记。

So let's break that down more. 所以让我们更多地打破它。 Consider a snippet like this: 考虑一下这样的片段:

/foo/g

With no context given, there are two ways to interpret this: 如果没有给出上下文,有两种方法可以解释这个:

  1. DivPunctuator IdentifierName DivPunctuator IdentifierName DivPunctuator IdentifierName DivPunctuator IdentifierName

     "/" "foo" "/" "g" 
  2. RegularExpressionLiteral RegularExpressionLiteral

     "/foo/g" 

From the standpoint of a lexer, it does not have enough information to know which of these to select. 从词法分析器的角度来看,它没有足够的信息来知道选择哪一个。 This means the lexer needs to have a flag like expectRegex or something, that toggles the behavior not just based on the current sequence of characters, but also on previously encountered tokens. 这意味着词法分析器需要有一个像expectRegex类的标志,它不仅可以根据当前的字符序列切换行为,还可以切换以前遇到过的标记。 Something needs to say "expect an operator next" or "expect a regex literal next". 有些事情需要说“期待接下来的运营商”或“期待下一个正则表达式文字”。

The same is true for the following 以下情况也是如此

}foo${
  1. RightBracePunctuator IdentifierName Punctuator RightBracePunctuator IdentifierName 标点符号

     "}" "foo$" "{" 
  2. TemplateMiddle TemplateMiddle

     "}foo${" 

A second toggle needs to be used for this case. 在这种情况下需要使用第二个切换。

So that leaves us with a nice table of the 4 options that you've seen 所以这给我们留下了一张你见过的4个选项的好桌子

| expectRegex | expectTemplate | InputElement                     |
| ----------- | -------------- | -------------------------------- |
| false       | false          | InputElementDiv                  |
| false       | true           | InputElementTemplateTail         |
| true        | false          | InputElementRegExp               |
| true        | true           | InputElementRegExpOrTemplateTail |

And the spec then covers when these flags toggle: 然后规范涵盖了这些标志切换时:

  • InputElementRegExpOrTemplateTail : This goal is used in syntactic grammar contexts where a RegularExpressionLiteral, a TemplateMiddle, or a TemplateTail is permitted. InputElementRegExpOrTemplateTail :此目标用于允许使用RegularExpressionLiteral,TemplateMiddle或TemplateTail的语法语法上下文中。
  • InputElementRegExp : This goal symbol is used in all syntactic grammar contexts where a RegularExpressionLiteral is permitted but neither a TemplateMiddle, nor a TemplateTail is permitted. InputElementRegExp :此目标符号用于允许使用RegularExpressionLiteral但不允许使用TemplateMiddle和TemplateTail的所有语法语法上下文中。
  • InputElementTemplateTail : This goal is used in all syntactic grammar contexts where a TemplateMiddle or a TemplateTail is permitted but a RegularExpressionLiteral is not permitted. InputElementTemplateTail :此目标用于允许TemplateMiddle或TemplateTail但不允许使用RegularExpressionLiteral的所有语法语法上下文中。
  • InputElementDiv : This goal is used in all other contexts. InputElementDiv :此目标在所有其他上下文中使用。

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

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