X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=33a91bbb6153a917f66591ee2ddbbabed17d9ccf;hp=61a18e8746aefe9730e1487db017dfbe29f32b62;hb=2bee2b032b0422d212e17082cc4dcbf28cb897ac;hpb=a7ba8d8a5f0cb7fbb5bf67f1a95f1cad5226c507 diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 61a18e8..33a91bb 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -45,6 +45,9 @@ public abstract class Sequence extends Element implements Iterable { public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops) { return new RewritingSequence(tag, e, drops); } + public static Sequence regionRewritingSequence(Functor tagfunctor, Element[] e, boolean[] drops) { + return new RegionRewritingSequence(tagfunctor, e, drops); } + //////////////////////////////////////////////////////////////////////////////// public Element follow = null; @@ -85,7 +88,7 @@ public abstract class Sequence extends Element implements Iterable { return epsilonForm; } - protected abstract Forest postReduce(Input.Location loc, Forest[] args, Position p); + protected abstract Forest postReduce(Input.Region loc, Forest[] args, Position p); // Position ////////////////////////////////////////////////////////////////////////////// @@ -127,8 +130,8 @@ public abstract class Sequence extends Element implements Iterable { // Position ///////////////////////////////////////////////////////////////////////////////// - final Forest rewrite(Input.Location loc) { return rewrite(loc, true); } - private final Forest rewrite(Input.Location loc, boolean epsilonCheck) { + final Forest rewrite(Input.Region loc) { return rewrite(loc, true); } + private final Forest rewrite(Input.Region loc, boolean epsilonCheck) { if (epsilonCheck && this==firstp()) return epsilonForm(); for(int i=0; i { private final Object result; public Constant(Element[] e, Object result) { super(e); this.result = result; } Sequence _clone() { return new Constant(elements, result); } - public Forest postReduce(Input.Location loc, Forest[] args, Position p) { + public Forest postReduce(Input.Region loc, Forest[] args, Position p) { return (Forest)Forest.leaf(loc, result, p); } static class Drop extends Constant { @@ -199,7 +202,7 @@ public abstract class Sequence extends Element implements Iterable { 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 Forest postReduce(Input.Location loc, Forest[] args, Position p) { return (Forest)Forest.singleton(loc, args[idx], p); } + public Forest postReduce(Input.Region loc, Forest[] args, Position p) { return (Forest)Forest.singleton(loc, args[idx], p); } Sequence _clone() { return new Singleton(elements,idx); } } @@ -210,7 +213,7 @@ public abstract class Sequence extends Element implements Iterable { 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 Forest postReduce(Input.Location loc, Forest[] args, Position p) { + public Forest postReduce(Input.Region loc, Forest[] args, Position p) { for(int i=0; i { } } + + + static class RegionRewritingSequence extends RewritingSequence { + private Functor tagf; + public RegionRewritingSequence(Functor tagfunctor, Element[] e, boolean[] drops) { + super(null, e, drops); + this.tagf = tagfunctor; + } + public Forest postReduce(Input.Region loc, Forest[] args, Position p) { + this.tag = tagf.invoke(loc); + Forest 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); } @@ -234,7 +253,7 @@ public abstract class Sequence extends Element implements Iterable { this.drops = drops == null ? new boolean[e.length] : drops; for(int i=0; i Forest postReduce(Input.Location loc, Forest[] args, Position p) { + public Forest postReduce(Input.Region loc, Forest[] args, Position p) { Forest[] args2 = new Forest[count]; int j = 0; for(int i=0; i