2016-12-05 94 views

回答

1

它必须是编译器的休息,但指定了多个一般为Expression类型,以便解析器可以正常恢复和将它存储在它的树中。

结帐this comment from the source

// We allow arbitrary expressions here, even though the grammar only allows string 
// literals. We check to ensure that it is only a string literal later in the grammar 
// check pass. 

The text <code>import foo from wat</code>, where <code>wat</code> has a red underline indicating a syntax error (i.e. "String literal expected.")

文件中的所有文本必须属于在其对应的树的某个节点。在上述情况下,我们希望在名为watIdentifier上报告错误。

由于我们在解析后报告语法错误,所以无论表达式是什么,它都需要在importSpecifier上。否则,我们如何知道节点应该是?由ImportDeclaration拥有的上下文允许我们在解析后知道我们需要报告解析错误。

正如您所提到的,这需要花费 - 所有importSpecifier的用户必须处理更一般的情况,其中importSpecifier是一个表达式。这绝对是痛苦的,我不幸在这方面没有很好的解决方法。

+0

谢谢Daniel。然而,这个评论描述了什么,但没有描述它为什么这样做。看看代码,看起来好像是因为'import'和'export'都使用了这个函数。也许改进它的一种方法是使其返回正确类型的通用函数? 为什么我要问是因为'ImportDeclaration.moduleSpecifier'不是'StringLiteral'令人困惑,让人想知道还有什么可以做的,并且需要执行额外的转换才能获得'.text'。 – unional

+0

我的编辑更清楚了吗? –

+0

我明白了。你的意思是说,如果'importSpecifier'是'StringLiteral',那么整个导入语句将无法解析,并且整个语句会出错?目前,我们可以将错误隔离到'importSpecifier'表达式,并在屏幕截图中报告错误。 – unional