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. <g...@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
> 

Reply via email to