我在使用 nearley.js 為決議器構建語法時遇到了這個錯誤。我有三個檔案:grammar.ne、grammar.js 和 parser.js。完整的錯誤如下:
$ ./.config/build.sh
> error: Two output files share the same path but have different contents: .build/grammar.js.map
> error: Two output files share the same path but have different contents: .build/grammar.js
exit status 1
以下是每個檔案的內容:
語法.ne:
main -> (statement "\n"):
statement -> "foo" | "bar"
語法.js:
// Generated automatically by nearley, version 2.20.1
// http://github.com/Hardmath123/nearley
import Lexer from './lexer';
(function() {
function id(x) { return x[0]; }
var grammar = {
Lexer: Lexer,
ParserRules: [
{ "name": "main$ebnf$1$subexpression$1", "symbols": ["statement", { "literal": "\n" }] },
{ "name": "main$ebnf$1", "symbols": ["main$ebnf$1$subexpression$1"] },
{ "name": "main$ebnf$1$subexpression$2", "symbols": ["statement", { "literal": "\n" }] },
{ "name": "main$ebnf$1", "symbols": ["main$ebnf$1", "main$ebnf$1$subexpression$2"], "postprocess": function arrpush(d) { return d[0].concat([d[1]]); } },
{ "name": "main", "symbols": ["main$ebnf$1"] },
{ "name": "statement$string$1", "symbols": [{ "literal": "f" }, { "literal": "o" }, { "literal": "o" }], "postprocess": function joiner(d) { return d.join(''); } },
{ "name": "statement", "symbols": ["statement$string$1"] },
{ "name": "statement$string$2", "symbols": [{ "literal": "b" }, { "literal": "a" }, { "literal": "r" }], "postprocess": function joiner(d) { return d.join(''); } },
{ "name": "statement", "symbols": ["statement$string$2"] }
]
, ParserStart: "main"
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = grammar;
} else {
grammar = grammar;
}
})();
const nearley = require("nearley");
const grammar = require("./grammar.js");
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed("foo\n");
console.log(JSON.stringify(parser.results));
我在網上找到的任何東西都沒有幫助。這是在 TypeScript repl 中構建的,如果有幫助的話,我有一個用 TypeScript 撰寫的詞法分析器。
uj5u.com熱心網友回復:
我弄清楚了這個問題。在我的 package.json 中,"es2015"當我應該使用"commonjs". 然后我將檔案擴展名更改grammar.js為.cjs,這樣就消除了所有自動生成的代碼錯誤。我還在包 json 中添加了一個腳本npx nearleyc grammar.ne -o grammar.cjs && node parser.cjs,它允許我更快地執行編譯語法檔案,并使用新的.cjs擴展名將其編譯為 CommonJS 模塊;這也允許我同時運行測驗檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473578.html
標籤:javascript 解析 近利
上一篇:決議一個迭代而不列出每個塊
下一篇:美麗的湯決議器限制
