Java adaptor

27 views
Skip to first unread message

visha...@gmail.com

unread,
May 15, 2018, 9:13:33 AM5/15/18
to PEG.js: Parser Generator for JavaScript
Hi,
I want to use peg.js at the java side to parse my data at server side. I tried using https://github.com/jeronimonunes/PEG which works for Java8 but not compatible with Java 6. I made a small change to make it compilable with Java6 but at runtime I am getting following error:

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "peg" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at Test.main(Test.java:32)
Exception in thread "main" java.lang.RuntimeException: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "peg" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
at Test.main(Test.java:37)
Caused by: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "peg" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at Test.main(Test.java:32)

Sample Code is:
public static void main(String[] args) throws Exception {
try {
InputStream pegIs = PEG.class.getResourceAsStream("/peg-0.10.0.min.js");
Reader pegReader = new InputStreamReader(pegIs);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Invocable invocable = (Invocable) engine;
Bindings scope = engine.createBindings();
engine.eval(pegReader,scope);
engine.eval("peg.options = function(){return {}}",scope);
scope.clear();
} catch (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}

Reply all
Reply to author
Forward
0 new messages