2016-12-05 93 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