X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=6b615dcd4460f7ffa3a0133474120924e0947294;hp=32b8ba94668e9eb33b86aa727858cf5e361d3cbf;hb=5d18f5606c9296e6b0c5749f05fc68f358ace2f6;hpb=b648ea33534fea67c9926a67b38d2e992c2f5752 diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 32b8ba9..6b615dc 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -12,16 +12,15 @@ public abstract class Sequence extends Element implements Iterable { // Static Constructors ////////////////////////////////////////////////////////////////////////////// - public abstract Sequence and(Sequence s); - public abstract Sequence not(Sequence s); - - private void needs(Sequence s) { s.needed.add(this); needs.add(s); } - private void hates(Sequence s) { s.hated.add(this); hates.add(s); } - - void clone(Sequence seq) { - for(Sequence s : seq.hates) hates(s); - for(Sequence s : seq.needs) needs(s); - noFollow = seq.noFollow; + 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(); + for(Sequence s : needs) { ret.needs.add(s); s.needed.add(ret); } + for(Sequence s : hates) { ret.hates.add(s); s.hated.add(ret); } + ret.noFollow = noFollow; + return ret; } /** the empty sequence (matches the empty string) */ @@ -47,13 +46,11 @@ public abstract class Sequence extends Element implements Iterable { //////////////////////////////////////////////////////////////////////////////// public Element noFollow = null; - public String name = null; - public void setName(String name) { this.name = name; } - public final Topology noFollow() { return noFollow==null ? null : noFollow.toAtom(); } + public final Topology noFollow() { return noFollow==null ? null : Atom.toAtom(noFollow); } Topology toAtom() { if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this); - return elements[0].toAtom(); + return Atom.toAtom(elements[0]); } protected final Element[] elements; @@ -75,12 +72,10 @@ 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; } @@ -126,12 +121,9 @@ 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 { static class Constant extends Sequence { private final Object result; public Constant(Element[] e, Object result) { super(e); this.result = result; } - public Sequence and(Sequence s) { Sequence ret = new Constant(elements, result); ret.needs(s); ret.clone(this); return ret; } - public Sequence not(Sequence s) { Sequence ret = new Constant(elements, result); ret.hates(s); ret.clone(this); return ret; } + Sequence _clone() { return new Constant(elements, result); } public Forest postReduce(Input.Location loc, Forest[] args) { return (Forest)Forest.leaf(loc, result); } static class Drop extends Constant { + Sequence _clone() { return new Drop(elements, lame); } public Drop(Element[] e, boolean lame) { super(e, null); this.lame = lame; } - public Sequence and(Sequence s) { Sequence ret = new Drop(elements, lame); ret.needs(s); ret.clone(this); return ret; } - public Sequence not(Sequence s) { Sequence ret = new Drop(elements, lame); ret.hates(s); ret.clone(this); return ret; } } - static class Empty extends Sequence.Constant.Drop { public Empty() { super(new Element[] { }, false); } } + static class Empty extends Sequence.Constant.Drop { + Sequence _clone() { return new Empty(); } + public Empty() { super(new Element[] { }, false); } } } static class Singleton extends Sequence { @@ -198,16 +190,14 @@ public abstract class Sequence extends Element implements Iterable { 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 Sequence and(Sequence s) { Sequence ret = new Singleton(elements, idx); ret.needs(s); ret.clone(this); return ret; } - public Sequence not(Sequence s) { Sequence ret = new Singleton(elements, idx); ret.hates(s); ret.clone(this); return ret; } + Sequence _clone() { return new Singleton(elements,idx); } } public static class Unwrap extends Sequence { private boolean[] drops; public Unwrap(Element[] e) { super(e); this.drops = null; } public Unwrap(Element[] e, boolean[] drops) { super(e); this.drops = drops; } - public Sequence and(Sequence s) { Sequence ret = new Unwrap(elements, drops); ret.needs(s); ret.clone(this); return ret; } - public Sequence not(Sequence s) { Sequence ret = new Unwrap(elements, drops); ret.hates(s); ret.clone(this); return ret; } + Sequence _clone() { return new Unwrap(elements, drops); } public Forest postReduce(Input.Location loc, Forest[] args) { for(int i=0; i { /*private*/public final Object tag; private final boolean[] drops; private int count = 0; - public Sequence and(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops); ret.needs(s); ret.clone(this); return ret; } - public Sequence not(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops); ret.hates(s); ret.clone(this); return ret; } + Sequence _clone() { return new RewritingSequence(tag, elements, drops); } public RewritingSequence(Object tag, Element[] e) { this(tag, e, null); } public RewritingSequence(Object tag, Element[] e, boolean[] drops) { super(e);