Getting the AST ?

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Mon Feb 1 00:19:43 UTC 2016


Hi,

jdk9 samples for Nashorn Parser API (http://openjdk.java.net/jeps/236) 
that Attila mentioned are:

http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/evalcheck.js
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/findproto.js
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/findwith.js
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/flexijson.js
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/nashornastviewer.js
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/prettyprinter.js
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/withcheck.js
https://blogs.oracle.com/nashorn/entry/flexible_json_with_nashorn_parser

For jdk8u,  it is better to avoid nashorn internal classes. There is a 
JS "parse" API.  You can load("nashorn:parser.js") and use "parse" 
function defined in it.

http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/parser.js
http://hg.openjdk.java.net/jdk9/dev/nashorn/file/a618d3e89fde/samples/astviewer.js

Hope this helps,
-Sundar

On 1/31/2016 3:48 PM, Attila Szegedi wrote:
> If you don’t mind using a prerelease JDE, JDK9 version of Nashorn has a public JavaScript parser API: <http://download.java.net/jdk9//docs/jdk/api/nashorn/jdk/nashorn/api/tree/package-summary.html>
>
> In the JDK8 world, there’s no supported way, but you can try using jdk.nashorn.internal.parser.Parser at your own risk (of it changing in an incompatible way, or going away entirely, or becoming inaccessible in JDK9). It returns a jdk.nashorn.internal.ir.FunctionNode, an internal Nashorn AST class that represents both functions and the top-level program. All of the AST is in the jdk.nashorn.internal.ir package. It was never meant for public consumption, so it’s not as nice to work with as the JDK9 public API, e.g. there are some dubious choices in the class hierarchy etc. that you’ll need to live with.
>
> HTH,
>    Attila.
>
>> On Jan 30, 2016, at 5:19 PM, Geir Magnusson Jr. <geir at pobox.com> wrote:
>>
>> Hi,
>>
>> I’m a complete newbie to this area, so please forgive the lack of proper terminology in advance.
>>
>> I need to be able to transform arbitrary JS and thought that getting and transforming the AST would be a good way to do it.  I was looking at things like the Shape library and it felt … lacking, somehow.
>>
>> I figure nashorn is going to be better :)
>>
>> Is there any way (I’m ok if it isn’t portable) to use the JDK internals to do this?
>>
>> Thanks in advance.
>>
>> geir
>>



More information about the nashorn-dev mailing list