X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=071c219d7118ac5a9af5eec3d807b993adc36874;hb=142b31e063c97d2d6dc141a9e687d49b7f2ab826;hp=3ac811e392283b8ed34f15ab6f070a32e2b84f16;hpb=1a249057cbfd2180910e46672eafee3af46ae470;p=sbp.git diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 3ac811e..071c219 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -12,8 +12,6 @@ public abstract class Sequence extends Element implements Iterable { // Static Constructors ////////////////////////////////////////////////////////////////////////////// - public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed.add(ret); return ret; } - public Sequence not(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); return ret; } abstract Sequence _clone(); Sequence dup() { Sequence ret = _clone(); @@ -53,6 +51,9 @@ public abstract class Sequence extends Element implements Iterable { return Atom.toAtom(elements[0]); } + public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed.add(ret); return ret; } + public Sequence not(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); return ret; } + protected final Element[] elements; final HashSet needed = new HashSet(); @@ -72,16 +73,14 @@ public abstract class Sequence extends Element implements Iterable { // DO NOT MESS WITH THE FOLLOWING LINE!!! private Forest.Ref epsilonForm = null; - private boolean eps = false; Forest epsilonForm() { - if (epsilonForm==null) { - epsilonForm = new Forest.Ref(); - epsilonForm.merge(firstp().rewrite2(null)); - } + if (epsilonForm!=null) return epsilonForm; + epsilonForm = new Forest.Ref(); + epsilonForm.merge(firstp().rewrite(null, false)); return epsilonForm; } - protected abstract Forest postReduce(Input.Location loc, Forest[] args); + protected abstract Forest postReduce(Input.Location loc, Forest[] args, Position p); // Position ////////////////////////////////////////////////////////////////////////////// @@ -123,18 +122,15 @@ public abstract class Sequence extends Element implements Iterable { // Position ///////////////////////////////////////////////////////////////////////////////// - final Forest rewrite(Input.Location loc) { - if (this==firstp()) return epsilonForm(); - return rewrite2(loc); - } - - final Forest rewrite2(Input.Location loc) { + final Forest rewrite(Input.Location loc) { return rewrite(loc, true); } + private final Forest rewrite(Input.Location loc, boolean epsilonCheck) { + if (epsilonCheck && this==firstp()) return epsilonForm(); for(int i=0; i ret = Sequence.this.postReduce(loc, holder); + Forest ret = Sequence.this.postReduce(loc, holder, this); for(int k=0; k { 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) { - return (Forest)Forest.leaf(loc, result); + public Forest postReduce(Input.Location loc, Forest[] args, Position p) { + return (Forest)Forest.leaf(loc, result, p); } static class Drop extends Constant { Sequence _clone() { return new Drop(elements, lame); } @@ -194,7 +190,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) { return (Forest)Forest.singleton(loc, args[idx]); } + public Forest postReduce(Input.Location loc, Forest[] args, Position p) { return (Forest)Forest.singleton(loc, args[idx], p); } Sequence _clone() { return new Singleton(elements,idx); } } @@ -203,15 +199,15 @@ public abstract class Sequence extends Element implements Iterable { 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 Forest postReduce(Input.Location loc, Forest[] args) { + public Forest postReduce(Input.Location loc, Forest[] args, Position p) { for(int i=0; i[] args2 = new Forest[count]; int j = 0; for(int i=0; i { this.drops = drops == null ? new boolean[e.length] : drops; for(int i=0; i Forest postReduce(Input.Location loc, Forest[] args) { + public Forest postReduce(Input.Location loc, Forest[] args, Position p) { Forest[] args2 = new Forest[count]; int j = 0; for(int i=0; i