/** * Adds a child or function to the end of the block. Sets the parent of the child to this node, * and fixes up the start position of the child to be relative to this node. Sets the length of * this node to include the new child. * * @param kid the child * @throws IllegalArgumentException if kid is {@code null} */ public void addChild(AstNode kid) { assertNotNull(kid); int end = kid.getPosition() + kid.getLength(); setLength(end - this.getPosition()); addChildToBack(kid); kid.setParent(this); }
/** * Permits AST nodes to be sorted based on start position and length. This makes it easy to sort * Comment and Error nodes into a set of other AST nodes: just put them all into a {@link * java.util.SortedSet}, for instance. * * @param other another node * @return -1 if this node's start position is less than {@code other}'s start position. If tied, * -1 if this node's length is less than {@code other}'s length. If the lengths are equal, * sorts abitrarily on hashcode unless the nodes are the same per {@link #equals}. */ public int compareTo(AstNode other) { if (this.equals(other)) return 0; int abs1 = this.getAbsolutePosition(); int abs2 = other.getAbsolutePosition(); if (abs1 < abs2) return -1; if (abs2 < abs1) return 1; int len1 = this.getLength(); int len2 = other.getLength(); if (len1 < len2) return -1; if (len2 < len1) return 1; return this.hashCode() - other.hashCode(); }
public boolean visit(AstNode node) { int tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); buffer.append(name).append(" "); buffer.append(node.getPosition()).append(" "); buffer.append(node.getLength()); if (tt == Token.NAME) { buffer.append(" ").append(((Name) node).getIdentifier()); } buffer.append("\n"); return true; // process kids }