Bug in Nashorn Parser API (order of statements)

A. Sundararajan sundararajan.athijegannathan at oracle.com
Mon Mar 16 07:48:05 UTC 2015


Hi,

Thanks for reporting this issue. I filed a bug 
https://bugs.openjdk.java.net/browse/JDK-8075207

Thanks,
-Sundar

On Sunday 15 March 2015 09:41 PM, Remi Forax wrote:
> Hi guys, Hi Sundar,
> it seems that the parser groups all the function declarations before 
> all the statements
> instead of following the order of the file.
>
> [forax at localhost jsjs]$ cat bug_parser.js
> var File = Java.type("java.io.File");
> var Parser = Java.type("jdk.nashorn.api.tree.Parser");
> var Tree = Java.type("jdk.nashorn.api.tree.Tree");
>
> // do we have a script file passed? if not, use current script
> var sourceName = arguments.length == 0? __FILE__ : arguments[0];
>
> var parser = Parser.create("-scripting");
> // parse script to get CompilationUnitTree of it
> var ast = parser.parse(new File(sourceName), null);
>
> for each(element in ast.getSourceElements()) {
>   print(element.getClass());
> }
> [forax at localhost jsjs]$ cat test/test5.js
> var hello = "hello jsjs";
>
> function print_hello() {
>     print(hello);
> }
>
> var hello = "hello jsjs 2";
>
>
> [forax at localhost jsjs]$ jjs bug_parser.js -- test/test5.js
> class jdk.nashorn.api.tree.FunctionDeclarationTreeImpl
> class jdk.nashorn.api.tree.VariableTreeImpl
> class jdk.nashorn.api.tree.VariableTreeImpl
>
> the correct output should be:
> class jdk.nashorn.api.tree.VariableTreeImpl
> class jdk.nashorn.api.tree.FunctionDeclarationTreeImpl
> class jdk.nashorn.api.tree.VariableTreeImpl
>
> regards,
> Rémi
>
>



More information about the nashorn-dev mailing list