我在使用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以下是每个文件的内容:
grammar.ne:
main -> (statement "\n"):+
statement -> "foo" | "bar"grammar.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里构建的,如果这有帮助的话,我有一个用TypeScript编写的lexer。
发布于 2022-05-04 13:34:51
我解决了这个问题。在我的package.json中,当我应该使用"commonjs"时,我已经使用了模块"es2015"。然后,我将grammar.js的文件扩展名更改为.cjs,从而消除了所有自动生成的代码错误。我还向包json npx nearleyc grammar.ne -o grammar.cjs && node parser.cjs添加了一个脚本,它允许我更快地执行语法文件的编译,并使用新的.cjs扩展将其编译为CommonJS模块;这还允许我同时运行测试文件。
https://stackoverflow.com/questions/72099925
复制相似问题