Search code examples
javajavascriptnashorn

Nashorn Parser API on JDK 8


Looking for a Java/JDK API for parsing JavaScript (including Nashorn Extensions) I stumbled upon this Gist, which according to an inline comment requires JDK 9 to run. Is there a reliable way to accomplish the same on current or planned releases of JDK 8?


Solution

  • Nashorn Parser API ( http://openjdk.java.net/jeps/236 ) is jdk9 specific API. On jdk8 or jdk8 update, there is support for script parser function.

    load("nashorn:parser.js");

    and call "parse" function from script. This function returns a JSON object that represents AST of the script parsed.

    See this sample: http://hg.openjdk.java.net/jdk8u/jdk8u-dev/nashorn/file/bfea11f8c8f2/samples/astviewer.js