protected void toStringWork(Object p, Object stop, StringBuffer buf) { if (!adaptor.isNil(p)) { String text = adaptor.getText(p); if (text == null) { text = " " + String.valueOf(adaptor.getType(p)); } buf.append(text); // ask the node to go to string } if (p == stop) { return; } int n = adaptor.getChildCount(p); if (n > 0 && !adaptor.isNil(p)) { buf.append(" "); buf.append(Token.DOWN); } for (int c = 0; c < n; c++) { Object child = adaptor.getChild(p, c); toStringWork(child, stop, buf); } if (n > 0 && !adaptor.isNil(p)) { buf.append(" "); buf.append(Token.UP); } }
protected void toDOTDefineEdges(Object tree, TreeAdaptor adaptor, StringTemplate treeST) { if (tree == null) { return; } int n = adaptor.getChildCount(tree); if (n == 0) { // must have already dumped as child from previous // invocation; do nothing return; } String parentName = "n" + getNodeNumber(tree); // for each child, do a parent -> child edge using unique node names String parentText = adaptor.getText(tree); for (int i = 0; i < n; i++) { Object child = adaptor.getChild(tree, i); String childText = adaptor.getText(child); String childName = "n" + getNodeNumber(child); StringTemplate edgeST = _edgeST.getInstanceOf(); edgeST.setAttribute("parent", parentName); edgeST.setAttribute("child", childName); edgeST.setAttribute("parentText", fixString(parentText)); edgeST.setAttribute("childText", fixString(childText)); treeST.setAttribute("edges", edgeST); toDOTDefineEdges(child, adaptor, treeST); } }
public Object next() { if (firstTime) { // initial condition firstTime = false; if (adaptor.getChildCount(tree) == 0) { // single node tree (special) nodes.add(eof); return tree; } return tree; } // if any queued up, use those first if (nodes != null && nodes.size() > 0) return nodes.remove(); // no nodes left? if (tree == null) return eof; // next node will be child 0 if any children if (adaptor.getChildCount(tree) > 0) { tree = adaptor.getChild(tree, 0); nodes.add(tree); // real node is next after DOWN return down; } // if no children, look for next sibling of tree or ancestor Object parent = adaptor.getParent(tree); // while we're out of siblings, keep popping back up towards root while (parent != null && adaptor.getChildIndex(tree) + 1 >= adaptor.getChildCount(parent)) { nodes.add(up); // we're moving back up tree = parent; parent = adaptor.getParent(tree); } // no nodes left? if (parent == null) { tree = null; // back at root? nothing left then nodes.add(eof); // add to queue, might have UP nodes in there return nodes.remove(); } // must have found a node with an unvisited sibling // move to it and return it int nextSiblingIndex = adaptor.getChildIndex(tree) + 1; tree = adaptor.getChild(parent, nextSiblingIndex); nodes.add(tree); // add to queue, might have UP nodes in there return nodes.remove(); }
protected Object visitChild(int child) { Object node = null; // save state nodeStack.push(currentNode); indexStack.push(new Integer(child)); if (child == 0 && !adaptor.isNil(currentNode)) { addNavigationNode(Token.DOWN); } // visit child currentNode = adaptor.getChild(currentNode, child); currentChildIndex = 0; node = currentNode; // record node to return addLookahead(node); walkBackToMostRecentNodeWithUnvisitedChildren(); return node; }
protected void toDOTDefineNodes(Object tree, TreeAdaptor adaptor, StringTemplate treeST) { if (tree == null) { return; } int n = adaptor.getChildCount(tree); if (n == 0) { // must have already dumped as child from previous // invocation; do nothing return; } // define parent node StringTemplate parentNodeST = getNodeST(adaptor, tree); treeST.setAttribute("nodes", parentNodeST); // for each child, do a "<unique-name> [label=text]" node def for (int i = 0; i < n; i++) { Object child = adaptor.getChild(tree, i); StringTemplate nodeST = getNodeST(adaptor, child); treeST.setAttribute("nodes", nodeST); toDOTDefineNodes(child, adaptor, treeST); } }