package edu.berkeley.sbp;
import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.Sequence.Position;
import edu.berkeley.sbp.util.*;
import java.io.*;
import java.util.*;
/** expand this forest into a set of trees */
public HashSet<Tree<T>> expand(boolean toss) {
+ //if (toss) scan();
final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
visit(new TreeMaker2<T>(toss, ret), null, null);
if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
public void addTree(Tree<T> t) { super.add(t); }
}
- static <T> Forest<T> singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
- static <T> Forest<T> singleton(Input.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
- static <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
- public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
- return new MyBody<T>(loc, tag, tokens, unwrap, singleton);
+ static <T> Forest<T> singleton(Input.Location loc, Position p) {
+ return create(loc, null, new Forest[] { }, false, true, p); }
+ static <T> Forest<T> singleton(Input.Location loc, Forest<T> body, Position p) {
+ return create(loc, null, new Forest[] { body }, false, true, p); }
+ static <T> Forest<T> leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, false, false, p); }
+
+ public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position p) {
+ return new MyBody<T>(loc, tag, tokens, unwrap, singleton, p);
}
// Body //////////////////////////////////////////////////////////////////////////////
private final Forest<T>[] tokens;
private final boolean unwrap;
private final boolean singleton;
+ private final Sequence.Position reduction;
- private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+ private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position reduction) {
this.location = loc;
this.tag = tag;
this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
this.unwrap = unwrap;
this.singleton = singleton;
+ this.reduction = reduction;
}
public void expand(final int i, final TreeMaker<T> h) {
GSS gss = new GSS();
Input.Location loc = input.getLocation();
GSS.Phase current = gss.new Phase<Tok>(null, this, null, input.next(1, 0, 0), loc, null);
- current.newNode(null, Forest.leaf(null, null), pt.start, true);
+ current.newNode(null, Forest.leaf(null, null, null), pt.start, true);
int count = 1;
for(;;) {
loc = input.getLocation();
return epsilonForm;
}
- protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args);
+ protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p);
// Position //////////////////////////////////////////////////////////////////////////////
if (holder[i]==null) holder[i] = elements[i].epsilonForm();
if (holder[i]==null) throw new Error("bad " + i);
}
- Forest<T> ret = Sequence.this.postReduce(loc, holder);
+ Forest<T> ret = Sequence.this.postReduce(loc, holder, this);
for(int k=0; k<pos; k++) holder[k] = null; // to help GC
return ret;
}
private final Object result;
public Constant(Element[] e, Object result) { super(e); this.result = result; }
Sequence _clone() { return new Constant(elements, result); }
- public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
- return (Forest<T>)Forest.leaf(loc, result);
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
+ return (Forest<T>)Forest.leaf(loc, result, p);
}
static class Drop extends Constant {
Sequence _clone() { return new Drop(elements, lame); }
private final int idx;
public Singleton(Element e) { this(new Element[] { e }, 0); }
public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
- public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) { return (Forest<T>)Forest.singleton(loc, args[idx], p); }
Sequence _clone() { return new Singleton(elements,idx); }
}
public Unwrap(Element[] e) { super(e); this.drops = null; }
public Unwrap(Element[] e, boolean[] drops) { super(e); this.drops = drops; }
Sequence _clone() { return new Unwrap(elements, drops); }
- public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
- if (drops==null) return Forest.create(loc, null, args, true, false);
+ if (drops==null) return Forest.create(loc, null, args, true, false, p);
int count = 0;
for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
Forest<T>[] args2 = new Forest[count];
int j = 0;
for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
- return Forest.create(loc, null, args2, true, false);
+ return Forest.create(loc, null, args2, true, false, p);
}
}
this.drops = drops == null ? new boolean[e.length] : drops;
for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
}
- public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
+ public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
Forest<T>[] args2 = new Forest[count];
int j = 0;
for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
//System.out.println("reduce \""+tag+"\"");
- return Forest.create(loc, (T)tag, args2, false, false);
+ return Forest.create(loc, (T)tag, args2, false, false, p);
}
public StringBuffer toString(StringBuffer sb, boolean spacing) {
int len = sb.length();