Issue while Parsing JS files to retrieve JS function names.

129 views
Skip to first unread message

sunil kaja

unread,
Feb 25, 2013, 1:58:44 AM2/25/13
to
I am using Rhino 1.7R4 to parse JS files to retrieve function names.

Using the following snippet to parse JS file. I have got this code from some of the posts in this group.
Reader reader = null;
try {
String file = "foo.js";
reader = new FileReader(file);
AstNode node = new Parser().parse(reader, file, 1);
node.visit(new Printer());
} finally {
reader.close();
}
class Printer implements NodeVisitor {
public boolean visit(AstNode node) {
if (node instanceof FunctionNode) {
FunctionNode function = (FunctionNode) node;
//Need this functionality to do some custom processing
System.out.println("Has children" + function.getParent().hasChildren()); // Always returning false.
}
return true;
}
}

My issue is why function.getParent().hasChildren() always returning false. It should return me true every time in my case. I am not able to access siblings of fucntion node as well.

Am I doing any mistake while parsing, new Parser().parse(reader, file, 1) ?? or is it a bug in Rhino??

wflo...@googlemail.com

unread,
Apr 10, 2013, 12:41:53 AM4/10/13
to
I use the following code, and it works

if (node instanceof FunctionNode) {
FunctionNode functionNode = (FunctionNode)node;
if (functionNode.getFunctionName() != null) {
functionName = functionNode.getFunctionName().getIdentifier();
}
}

Version: rhino1_7R4
Reply all
Reply to author
Forward
0 new messages