X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FTree.java;h=220d1ddc0574977bf5a160c41e090328f8cc9f4f;hb=fa858dc4acddd3e32126ff2558e0860315a84758;hp=c2216cc2102a9ea36b7e224ba8896bf839fb34d3;hpb=244fe51f604d0286adbbb0ef983562ff852d6b25;p=sbp.git diff --git a/src/edu/berkeley/sbp/Tree.java b/src/edu/berkeley/sbp/Tree.java index c2216cc..220d1dd 100644 --- a/src/edu/berkeley/sbp/Tree.java +++ b/src/edu/berkeley/sbp/Tree.java @@ -7,52 +7,40 @@ import java.util.*; import java.lang.reflect.*; /** a tree (or node in a tree); see jargon.txt for details */ -public class Tree { +public class Tree extends PrintableTree> implements Iterable> { final T head; Tree[] children; - final Token.Location location; + Object[] labels; + final Input.Location location; - public T head() { return head; } + public T head() { return head; } public int numChildren() { return children.length; } - public Iterable> children() { return new ArrayIterator(children); } - public Tree child(int i) { return children[i]; } + public Iterable> children() { return new ArrayIterator(children); } + public Iterator> iterator() { return new ArrayIterator(children); } + public Tree child(int i) { return children[i]; } + public Object label(int i) { return labels[i]; } - public Token.Location getLocation() { return location; } + public Input.Location getLocation() { return location; } - public Tree(Token.Location loc, T head) { this(loc, head, null); } - public Tree(Token.Location loc, T head, Tree[] children) { + public Tree(Input.Location loc, T head) { this(loc, head, null); } + public Tree(Input.Location loc, T head, Tree[] children) { this(loc, head, children, null); } + public Tree(Input.Location loc, T head, Tree[] children, Object[] labels) { this.location = loc; this.head = head; + Tree[] children2 = children==null ? new Tree[0] : new Tree[children.length]; if (children != null) System.arraycopy(children, 0, children2, 0, children.length); this.children = children2; - } - /** append Java code to sb which evaluates to this instance */ - public void toJava(StringBuffer sb) { - sb.append("new Tree(null, "); - sb.append(head==null ? "null" : "\"" + StringUtil.toJavaString(head+"") + "\""); - sb.append(", new Tree[] { "); - for(int i=0; i 0) { ret.append(q); ret.append(" "); } - } - String tail = ret.toString().trim(); - String h = (head!=null && !head.toString().equals("")) ? (tail.length() > 0 ? head+":" : head+"") : ""; - if (tail.length() > 0) tail = "{" + tail + "}"; - return h + tail; - } - - + protected String headToString() { return head==null?null:head.toString(); } + protected String headToJava() { return head==null?null:StringUtil.toJavaString(head+""); } + protected String left() { return "{"; } + protected String right() { return "}"; } + protected boolean ignoreSingleton() { return false; } }