Bug in Nashorn Parser API (order of statements)

Remi Forax forax at univ-mlv.fr
Sun Mar 15 16:11:43 UTC 2015


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