public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops) {
return new RewritingSequence(tag, e, drops); }
+ public static Sequence regionRewritingSequence(Functor<Input.Region,Object> tagfunctor, Element[] e, boolean[] drops) {
+ return new RegionRewritingSequence(tagfunctor, e, drops); }
+
////////////////////////////////////////////////////////////////////////////////
public Element follow = null;
return epsilonForm;
}
- protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p);
+ protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
// Position //////////////////////////////////////////////////////////////////////////////
// Position /////////////////////////////////////////////////////////////////////////////////
- final <T> Forest<T> rewrite(Input.Location loc) { return rewrite(loc, true); }
- private final <T> Forest<T> rewrite(Input.Location loc, boolean epsilonCheck) {
+ final <T> Forest<T> rewrite(Input.Region loc) { return rewrite(loc, true); }
+ private final <T> Forest<T> rewrite(Input.Region loc, boolean epsilonCheck) {
if (epsilonCheck && this==firstp()) return epsilonForm();
for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
for(int i=pos; i<elements.length; i++) {
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, Position p) {
+ public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
return (Forest<T>)Forest.leaf(loc, result, p);
}
static class Drop extends Constant {
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, Position p) { return (Forest<T>)Forest.singleton(loc, args[idx], p); }
+ public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) { return args[idx]; }
Sequence _clone() { return new Singleton(elements,idx); }
}
public Unwrap(Element[] e, Object tag) { super(e); this.drops = null; this.tag = tag; }
public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
Sequence _clone() { return new Unwrap(elements, drops); }
- public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
+ public <T> Forest<T> postReduce(Input.Region 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, (T)tag, args, true, false, p);
+ if (drops==null) return Forest.create(loc, (T)tag, args, true, 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, (T)tag, args2, true, false, p);
+ return Forest.create(loc, (T)tag, args2, true, p);
+ }
+ }
+
+
+
+ static class RegionRewritingSequence extends RewritingSequence {
+ private Functor<Input.Region, Object> tagf;
+ public RegionRewritingSequence(Functor<Input.Region,Object> tagfunctor, Element[] e, boolean[] drops) {
+ super(null, e, drops);
+ this.tagf = tagfunctor;
+ }
+ public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
+ this.tag = tagf.invoke(loc);
+ Forest<T> ret = super.postReduce(loc, args, p);
+ this.tag = null;
+ return ret;
}
}
static class RewritingSequence extends Sequence {
- /*private*/public final Object tag;
+ /*private*/public /*final*/ Object tag;
private final boolean[] drops;
private int count = 0;
Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
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, Position p) {
+ public <T> Forest<T> postReduce(Input.Region 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, p);
+ return Forest.create(loc, (T)tag, args2, false, p);
}
public StringBuffer toString(StringBuffer sb, boolean spacing) {
int len = sb.length();