public boolean ambiguous() { return false; }
public /*protected*/ static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
+ private final Input.Region location;
+ private final T tag;
+ private final Forest<T>[] tokens;
+ private final boolean unwrap;
+
public boolean isTransparent() { return false; }
public boolean isHidden() { return false; }
public GraphViz.Node toGraphViz(GraphViz gv) {
ivbc.invoke(this, b, c);
}
- private final Input.Region location;
- private final T tag;
- private final Forest<T>[] tokens;
- private final boolean unwrap;
-
private MyBody(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap) {
this.location = loc;
this.tag = tag;
implements Iterable<Tree<T>>,
GraphViz.ToGraphViz {
- final T head;
- Tree<T>[] children;
- final Input.Region location;
+ private final T head;
+ private final Tree<T>[] children;
+ private final Input.Region location;
+ private final boolean lift;
public T head() { return head; }
- public int numChildren() { return children.length; }
- public Iterable<Tree<T>> children() { return new ArrayIterator(children); }
- public Iterator<Tree<T>> iterator() { return new ArrayIterator(children); }
- public Tree<T> child(int i) { return children[i]; }
+
+ private Tree<T> lifted() { return children[children.length-1]; }
+
+ public int numChildren() {
+ if (lift && children.length > 0)
+ return (children.length-1)+lifted().numChildren();
+ return children.length;
+ }
+ public Iterable<Tree<T>> children() { return this; }
+ public Iterator<Tree<T>> iterator() {
+ if (lift)
+ return new ConcatenateIterator(new ArrayIterator(children, 0, children.length-1),
+ children[children.length-1].iterator());
+ return new ArrayIterator(children);
+ }
+ public Tree<T> child(int i) {
+ if (lift && i >= children.length-1)
+ return children[children.length-1].child(i-(children.length-1));
+ return children[i];
+ }
public Input.Region getRegion() { return location; }
public Tree(Input.Region loc, T head) { this(loc, head, null); }
- public Tree(Input.Region loc, T head, Tree<T>[] children) {
+ public Tree(Input.Region loc, T head, Tree<T>[] children) { this(loc, head, children, false); }
+ public Tree(Input.Region loc, T head, Tree<T>[] children, boolean lift) {
this.location = loc;
this.head = head;
+ this.lift = lift;
Tree<T>[] children2 = children==null ? new Tree[0] : new Tree[children.length];
if (children != null) System.arraycopy(children, 0, children2, 0, children.length);
this.children = children2;
}
- protected String headToString() { return head==null?null:head.toString(); }
+ protected String headToString() { return head()==null?null:head().toString(); }
protected String headToJava() {
// FIXME
- if (head==null) return null;
- if (head instanceof ToJava) {
+ if (head()==null) return null;
+ if (head() instanceof ToJava) {
StringBuffer sb = new StringBuffer();
- ((ToJava)head).toJava(sb);
+ ((ToJava)head()).toJava(sb);
return sb.toString();
}
- return (head==null?"null":("\""+StringUtil.toJavaString(head.toString())+"\""));
+ return (head()==null?"null":("\""+StringUtil.toJavaString(head().toString())+"\""));
}
protected String left() { return "{"; }
protected String right() { return "}"; }
public final class ArrayIterator<T> implements Iterator<T>, Iterable<T> {
private final T[] array;
- private int i = 0;
+ private int start;
+ private int len;
+ private int i = 0;
- public ArrayIterator(T[] array) { this.array = array; }
+ public ArrayIterator(T[] array) { this(array, 0, array.length); }
+ public ArrayIterator(T[] array, int start, int len) {
+ this.start = start;
+ this.len = len;
+ this.array = array;
+ this.i = start;
+ }
public void remove() { throw new Error(); }
- public boolean hasNext() { return i<array.length; }
+ public boolean hasNext() { return i<start+len; }
public T next() { return array[i++]; }
public Iterator<T> iterator() { return this; }
}