// 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) */
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 <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
return (Forest<T>)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 {
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) { return (Forest<T>)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 <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
if (drops==null) return Forest.create(loc, null, args, true, false);
/*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);
PreSequence ret = new PreSequence(o3, s);
ret.drops[o1.length] = o3.length>1;
return ret;
- } else if ("[".equals(head) || "[~".equals(head)) {
- boolean positive = "[".equals(head);
+ } else if ("[".equals(head)) {
Range[] rr = (Range[])walk(tree, 0);
- Range.Set ret = positive ? new Range.Set() : new Range.Set(new Range(true, true));
- if (rr != null)
- for(Range r : rr)
- if (positive) ret.add(r);
- else ret.remove(r);
+ Range.Set ret = new Range.Set();
+ if (rr!=null) for(Range r : rr) ret.add(r);
return set(ret);
}
else return super.walk(tree);
}
-
public Object walk(String tag, Object[] argo) {
if (argo.length==0) return super.walk(tag, argo);
if (argo==null) return tag;