checkpoint
authoradam <adam@megacz.com>
Sun, 15 Jan 2006 22:28:07 +0000 (17:28 -0500)
committeradam <adam@megacz.com>
Sun, 15 Jan 2006 22:28:07 +0000 (17:28 -0500)
darcs-hash:20060115222807-5007d-ce736c9b21b3c24ab4d9fceed416fcb36068beef.gz

src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/misc/MetaGrammar.java

index 32b8ba9..2eddfc4 100644 (file)
@@ -12,16 +12,15 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     // 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) */
@@ -177,20 +176,20 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     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 {
@@ -198,16 +197,14 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         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);
@@ -224,8 +221,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         /*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);
index b7dca25..28b306f 100644 (file)
@@ -169,20 +169,15 @@ public class MetaGrammar extends StringWalker {
             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;