class NameAnalyzer {
/**
* Extract a list of subexpressions that act as right hand sides.
*/
private List<Node> getRhsSubexpressions(Node n) {
switch (n.getType()) {
case Token.EXPR_RESULT:
// process body
return getRhsSubexpressions(n.getFirstChild());
case Token.FUNCTION:
// function nodes have no rhs
return Collections.emptyList();
case Token.NAME:
{
// parent is a var node. rhs is first child
Node rhs = n.getFirstChild();
if (rhs != null) {
return Lists.newArrayList(rhs);
} else {
return Collections.emptyList();
}
}
case Token.ASSIGN:
{
// add lhs and rhs expressions - lhs may be a complex expression
Node lhs = n.getFirstChild();
Node rhs = lhs.getNext();
return Lists.newArrayList(lhs, rhs);
}
case Token.VAR:
{
// recurse on all children
List<Node> nodes = Lists.newArrayList();
for (Node child : n.children()) {
nodes.addAll(getRhsSubexpressions(child));
}
return nodes;
}
default:
throw new IllegalArgumentException("AstChangeProxy::getRhs " + n);
}
}
}