首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个输出文件共享相同的路径,但内容不同。

两个输出文件共享相同的路径,但内容不同。
EN

Stack Overflow用户
提问于 2022-05-03 13:12:18
回答 1查看 311关注 0票数 0

我在使用nearley.js为解析器构建语法时遇到了这个错误。我有三个文件: grammar.ne、grammar.js和parser.js。完整的错误如下:

代码语言:javascript
复制
$ ./.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:

代码语言:javascript
复制
main -> (statement "\n"):+
statement -> "foo" | "bar"

grammar.js:

代码语言:javascript
复制
// 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;
  }
})();
代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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模块;这还允许我同时运行测试文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72099925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档