I've been using NodeTransformer.java as an example to write code which
walks through the JS Node tree.  The one thing I'm held up with is
getting the names from an Object Literal.  For example, in the
following JS:

var User = {
        id : 123,
        balance : 12.50,
        name : "John Doe"
};

When I print the Node tree this is all I get:

[49] VAR
        [-1] NAME User
                [-1] OBJECTLIT
                        [-1] NUMBER
                        [-1] NUMBER
                        [-1] STRING John Do

It only returns the values.  I would expect to get the names (id,
balance, name) and values  Here's my Java code:

public static void readTree(ScriptOrFnNode tree, Node parent, int
indent){
   Node node = null;
   int funcIndex = 0;

   // Generate indent padding
   String padding = "";
   for(int i = 0; i < indent; i++){
      padding += "\t";
   }

   // Loop through sibling nodes
   for (;;) {

      // Get node
      Node previous = null;
      if (node == null) {
         node = parent.getFirstChild();
      } else{
         previous = node;
         node = node.getNext();
      }
      if (node == null) {
         break;
      }

      int type = node.getType();
      String val = "";

      if(type == Token.STRING || type == Token.NAME || type ==
Token.FUNCTION){
         val = node.getString();
      }

      System.out.println(padding +"["+ node.getLineno() +"] "+
Token.name(type) +" "+ val);

      if(type == Token.FUNCTION){
         ScriptOrFnNode func = tree.getFunctionNode(funcIndex++);
         readTree(func, func, indent + 1);
      }

      // Get children
      readTree(tree, node, indent + 1);
   }
}


What am I doing wrong here?
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to