ECMASCRIPT SPEC的一段文字不太理解

下面是文档的一段文本
https://tc39.es/ecma262/#sec-...
不理解说了一些什么,哪位大神帮忙讲解一下,最好带一点简单的例子,实在是太差了我

in certain cases, in order to avoid ambiguities, the syntactic grammar uses generalized productions that permit token sequences that do not form a valid ECMAScript Script or Module. For example, this technique is used for object literals and object destructuring patterns. In such cases a more restrictive supplemental grammar is provided that further restricts the acceptable token sequences. Typically, an early error rule will then define an error condition if "P is not covering an N", where P is a Parse Node (an instance of the generalized production) and N is a nonterminal from the supplemental grammar. Here, the sequence of tokens originally matched by P is parsed again using N as the goal symbol. (If N takes grammatical parameters, then they are set to the same values used when P was originally parsed.) An error occurs if the sequence of tokens cannot be parsed as a single instance of N, with no tokens left over. Subsequently, algorithms access the result of the parse using a phrase of the form "the N that is covered by P". This will always be a Parse Node (an instance of N, unique for a given P), since any parsing failure would have been detected by an early error rule.

阅读 2.1k
1 个回答

这段话是属于 5. Notational Conventions 大章节,算是前言的一部分,它在描述这整个 spec 文件的一条特别的行文约定.

这条行文约定大意说:一般而言,有关一组语法的规则(productions)在一章的开头就会罗列穷尽,但有可能有例外情况,本文件要换一种文字组织形式。之所以会有例外,是为了规避(行文上的)歧义,也即原文说的 in certain cases, in order to avoid ambiguities

12.15 Assignment Operators 章节就是应用了这一行文规则的段落.

image.png

一般而言,这段文字应该罗列穷尽关于 AssignmentExpression 的所有 production 规则了,但是实际上它没有,它有补充语法。

文件读者如果在 early error 小节里看到形如 "P is not covering an N" 这样的语句,那就意味着本章节运用了这套行文约定.

正好 12.15.1 里出现了这样的语句:

It is a Syntax Error if LeftHandSideExpression is not covering an AssignmentPattern.

这就是在提示读者,请进一步阅读补充语法 (supplemental syntax)部分, 12.15.5 Destructuring Assignment. 点进去看,哦,原来还有 AssignmentPattern 这样的 nonterminal symbol 没有在开头的 syntax 部分提到,它的规则是 blabla...

为什么有这个行文约定、要切换文字组织形式呢?说白了就是这段东西太长了,全部堆在开头的 syntax 部分看容易看花眼咯。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题