两个输出文件共享相同的路径但具有不同的内容

Two output files share the same path but have different contents

我在使用 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$subexpression", "symbols": ["statement", { "literal": "\n" }] },
      { "name": "main$ebnf", "symbols": ["main$ebnf$subexpression"] },
      { "name": "main$ebnf$subexpression", "symbols": ["statement", { "literal": "\n" }] },
      { "name": "main$ebnf", "symbols": ["main$ebnf", "main$ebnf$subexpression"], "postprocess": function arrpush(d) { return d[0].concat([d[1]]); } },
      { "name": "main", "symbols": ["main$ebnf"] },
      { "name": "statement$string", "symbols": [{ "literal": "f" }, { "literal": "o" }, { "literal": "o" }], "postprocess": function joiner(d) { return d.join(''); } },
      { "name": "statement", "symbols": ["statement$string"] },
      { "name": "statement$string", "symbols": [{ "literal": "b" }, { "literal": "a" }, { "literal": "r" }], "postprocess": function joiner(d) { return d.join(''); } },
      { "name": "statement", "symbols": ["statement$string"] }
    ]
    , 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 编写的词法分析器,如果有帮助的话。

我想通了这个问题。在我的 package.json 中,当我应该使用 "commonjs" 时,我使用了模块 "es2015"。然后我将 grammar.js 的文件扩展名更改为 .cjs,这消除了所有 automatically-generated 代码错误。我还在包 json npx nearleyc grammar.ne -o grammar.cjs && node parser.cjs 中添加了一个脚本,这使我可以更快地执行编译语法文件,并使用新的 .cjs 扩展将其编译为 CommonJS 模块;这也允许我同时 运行 测试文件。