While you can definitely use the parser using the Nashorn JavaScript code, it is even nicer in Java to use it through the JAVA API
import jdk.nashorn.api.scripting.ScriptUtils; import jdk.nashorn.internal.runtime.Context; import jdk.nashorn.internal.runtime.ECMAException; import jdk.nashorn.internal.runtime.ErrorManager; import jdk.nashorn.internal.runtime.options.Options; // set up the environment Options options = new Options("nashorn"); options.set("anon.functions", true); options.set("parse.only", true); options.set("scripting", true); ErrorManager errors = new ErrorManager(); Context contextm = new Context(options, errors, Thread.currentThread().getContextClassLoader()); Context.setGlobal(contextm.createGlobal()); // then for each bit of parsing String parseTree = ScriptUtils.parse(some_code_string, "nashorn", false);
source share