X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=328550dce3099319150949cbc2f83ea96c71bd1d;hb=e214b29e31c1b1594695925eefc1fad411ce7081;hp=b31b307ad39b466b0ac54517cb5683a2b551b84d;hpb=6a2ea790f843e058c7e67d3c7d1deebadcfe1fd5;p=sbp.git diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index b31b307..328550d 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -12,11 +12,17 @@ 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); } + /** the empty sequence (matches the empty string) */ public static final Sequence empty = new Sequence.Constant.Empty(); /** after matching the sequence, do not add anything to the output tree */ - public static Sequence drop(Element[] e, HashSet and, HashSet not, boolean lame) { return new Constant.Drop(e, and, not, lame); } + public static Sequence drop(Element[] e, boolean lame) { return new Constant.Drop(e, null, null, lame); } /** after matching the sequence, insert a constant into the output tree */ public static Sequence constant(Element[] e, Object o, HashSet and, HashSet not) { return new Constant(e, o, and, not); } @@ -174,6 +180,8 @@ public abstract class Sequence extends Element implements Iterable { static class Constant extends Sequence { private final Object result; public Constant(Element[] e, Object result, HashSet and, HashSet not) { super(e, and, not); this.result = result; } + public Sequence and(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.hates(s); return ret; } public Forest postReduce(Input.Location loc, Forest[] args) { return (Forest)Forest.leaf(loc, result); } @@ -191,12 +199,16 @@ public abstract class Sequence extends Element implements Iterable { public Singleton(Element e, HashSet and, HashSet not) { this(new Element[] { e }, 0, and, not); } public Singleton(Element[] e, int idx, HashSet and, HashSet not) { super(e, and, not); 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, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new Singleton(elements, idx, needs, hates); ret.hates(s); return ret; } } public static class Unwrap extends Sequence { private boolean[] drops; public Unwrap(Element[] e, HashSet and, HashSet not) { super(e, and, not); this.drops = null; } public Unwrap(Element[] e, boolean[] drops, HashSet and, HashSet not) { super(e, and, not); this.drops = drops; } + public Sequence and(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.hates(s); return ret; } 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, needs, hates); ret.needs(s); return ret; } + public Sequence not(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops, needs, hates); ret.hates(s); return ret; } public RewritingSequence(Object tag, Element[] e, HashSet and, HashSet not) { this(tag, e, null, and, not); } public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet and, HashSet not) { super(e, and, not);