简体   繁体   English

如何使用Rhino(Java SE 6 ScriptEngine API)建立PEG.js 0.7.0解析器

[英]How to build PEG.js 0.7.0 parser using Rhino (Java SE 6 ScriptEngine API)

I've been building parsers using a Maven Plugin (ie calling PEG.js from Java code) successfully using PEG.js version 0.6.1, but now while trying to upgrade to the new version, it's failing with the error message: 我一直在使用Maven插件(即从Java代码调用PEG.js)并成功使用PEG.js 0.6.1版构建解析器,但是现在尝试升级到新版本时,它失败并显示错误消息:

sun.org.mozilla.javascript.internal.EcmaError: SyntaxError: missing ; before statement (#3213(eval)#1) in #3213(eval) at line number 1

While debugging I've noticed that the parser that comes out of the PEG.buildParser() function call looks syntactically wrong, whereas of course it doesn't happen while calling it from a browser. 在调试时,我注意到PEG.buildParser()函数调用产生的解析器在语法上看起来是错误的,但是从浏览器调用它时当然不会发生。

Here is how I'm calling it: 这是我如何称呼它:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine jsEngine = manager.getEngineByName("JavaScript");

jsEngine.eval(new InputStreamReader(this.getClass().getResourceAsStream(PEGJS_LIB)));

Invocable invok = (Invocable) jsEngine;
Object peg = jsEngine.get("PEG");

invok.invokeMethod(peg, "buildParser", grammarSource);

Where, grammarSource , in my test-case, is the basic grammar used as an example in the website , and PEGJS_LIB = peg-0.7.0.js . 在我的测试用例中, grammarSource网站中用作示例的基本语法, PEGJS_LIB = peg-0.7.0.js

Any ideas are welcome! 欢迎任何想法! I've run out of them here... 我在这里用光了...

I tracked down the cause for this issue. 我跟踪了此问题的原因。 'PEG.compiler.emitter.Codie.pushRaw.unindent()' line 3681 destroys the templates. 'PEG.compiler.emitter.Codie.pushRaw.unindent()'行3681破坏了模板。 I have not fully determined how important this function is for the parser, but it seems like a 'nice to have'. 我还没有完全确定此功能对解析器的重要性,但是看起来“很不错”。 I replaced the body of the function locally and Rhino worked as expected. 我在本地替换了函数的主体,Rhino正常工作。

function unindent(code, level, unindentFirst) {
  return code
}

I know David is not interested in supporting Rhino, but I thought I should share this on this thread so others might be able to solve the problem as needed. 我知道David对支持Rhino不感兴趣,但是我认为我应该在此主题上分享这一点,以便其他人可以根据需要解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM