Nashorn comment nodes in the syntax tree
Sundararajan Athijegannathan
sundararajan.athijegannathan at oracle.com
Fri Jul 22 07:38:01 UTC 2016
Hi,
Yes, comments are swallowed (by design). But, I initially though that
disturbs line & column number info. [i.e., position of various trees].
i.e., that was the bug you reported. That does not seem to be the case.
I wrote a simple sample and checked with jjs in jdk9 build. You can
check start and end line/column numbers of subtrees (function,
expression statement) are correct.
Also, please note that jdk.nashorn.internal.* packages are not APIs and
therefore please avoid using those. Please use nashorn parser API
defined by JEP 236 [ http://openjdk.java.net/jeps/236 ]
#-scripting needed
var Parser = Java.type("jdk.nashorn.api.tree.Parser")
var p = Parser.create();
var cu = p.parse("t.js", <<EOF
/* start script comment */
function hello(){
return 'world';
}
//some comment
hello();
EOF, print);
var src = cu.sourceElements;
var itr = src.iterator();
var lm = cu.lineMap;
while (itr.hasNext()) {
var tree = itr.next();
var start = tree.startPosition;
var end = tree.endPosition;
print(tree.kind,
lm.getLineNumber(start),
lm.getColumnNumber(start),
lm.getLineNumber(end),
lm.getColumnNumber(end));
}
Output:
FUNCTION 3 2 5 1
EXPRESSION_STATEMENT 8 4 8 11
PS. please check line & column w.r.t beginning of test script parsed.
i.e., the heredoc starting point.
-Sundar
More information about the nashorn-dev
mailing list