got rid of Body.creator
authoradam <adam@megacz.com>
Sun, 8 Jan 2006 09:40:10 +0000 (04:40 -0500)
committeradam <adam@megacz.com>
Sun, 8 Jan 2006 09:40:10 +0000 (04:40 -0500)
darcs-hash:20060108094010-5007d-732ae891c2be5b21b26a6f741021c2dfb7b2c96d.gz

src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/misc/CharToken.java

index e675f63..705208f 100644 (file)
@@ -18,13 +18,12 @@ public abstract class Forest<T> {
 
     /** expand this forest into a set of trees */
     public abstract HashSet<Tree<T>>  expand(boolean toss);
-    public abstract boolean empty();
 
-    static <T> Forest<T>   singleton(Token.Location loc, Sequence creator)                       { return create(loc, null, new Forest[] { }, creator, false, true); }
-    static <T> Forest<T>   singleton(Token.Location loc, Forest<T> body, Sequence creator)       { return create(loc, null, new Forest[] { body }, creator, false, true); }
-    static <T> Forest<T>   leaf(Token.Location loc, T tag,                     Sequence creator) { return create(loc, tag, null, creator, false, false); }
-    public static <T> Forest<T>   create(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
-        return new MultiForest<T>(loc, tag, tokens, creator, unwrap, singleton);
+    static        <T> Forest<T> singleton(Token.Location loc)                       { return create(loc, null, new Forest[] { }, false, true); }
+    static        <T> Forest<T> singleton(Token.Location loc, Forest<T> body)       { return create(loc, null, new Forest[] { body },  false, true); }
+    static        <T> Forest<T> leaf(Token.Location loc, T tag) { return create(loc, tag, null, false, false); }
+    public static <T> Forest<T> create(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+        return new MultiForest<T>(loc, tag, tokens, unwrap, singleton);
     }
 
     // Body //////////////////////////////////////////////////////////////////////////////
@@ -34,17 +33,15 @@ public abstract class Forest<T> {
         private final Token.Location    location;
         private final T                 tag;
         private final Forest<T>[]       tokens;
-        private final Sequence          creator;
         private final boolean           unwrap;
         private final boolean           singleton;
 
-        private Body(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
+        private Body(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
             this.location = loc;
             this.tag = tag;
             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
             if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
             if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
-            this.creator = creator;
             this.unwrap = unwrap;
             this.singleton = singleton;
         }
@@ -120,11 +117,6 @@ public abstract class Forest<T> {
      *  viewed, it becomes immutable
      */
     static class Ref<T> extends IterableForest<T> {
-        public boolean empty() {
-            if (res!=null) return res.empty();
-            for(Forest f : hp) if (!f.empty()) return false;
-            return true;
-        }
         private FastSet<Forest> hp = new FastSet<Forest>();
         private Forest res = null;
         public Ref() { }
@@ -151,11 +143,10 @@ public abstract class Forest<T> {
     // Implementations //////////////////////////////////////////////////////////////////////////////
 
     private static class MultiForest<T> extends IterableForest<T> {
-        public boolean empty() { return results.size()>0; }
         private final FastSet<Body<T>> results;
         private MultiForest(FastSet<Body<T>> results) { this.results = results; }
-        public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
-            this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, creator, unwrap, singleton));
+        public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+            this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, unwrap, singleton));
         }
         public Iterator<Body<T>> iterator() { return results.iterator(); }
 
index c270a90..3609182 100644 (file)
@@ -37,7 +37,7 @@ public abstract class Parser<T extends Token, R> {
         GSS gss = new GSS();
         Token.Location loc = input.getLocation();
         GSS.Phase current = gss.new Phase(null, this, null, input.next(1, 0, 0), loc, null);
-        current.newNode(null, Forest.leaf(null, null, null), pt.start, true);
+        current.newNode(null, Forest.leaf(null, null), pt.start, true);
         int count = 1;
         for(;;) {
             loc = input.getLocation();
index 610af58..adf93fa 100644 (file)
@@ -170,7 +170,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final Object result;
         public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
-            return (Forest<T>)Forest.leaf(loc, result, this);
+            return (Forest<T>)Forest.leaf(loc, result);
         }
         static class Drop extends Constant {
             public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
@@ -185,7 +185,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final int idx;
         public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
         public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
-        public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
+        public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
     }
 
     public static class Unwrap extends Sequence {
@@ -194,13 +194,13 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
         public <T> Forest<T> postReduce(Token.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, this, true, false);
+            if (drops==null) return Forest.create(loc, null, args, true, false);
             int count = 0;
             for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
             Forest<T>[] args2 = new Forest[count];
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
-            return Forest.create(loc, null, args2, this, true, false);            
+            return Forest.create(loc, null, args2, true, false);            
         }
     }
 
@@ -220,7 +220,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
             //System.out.println("reduce \""+tag+"\"");
-            return Forest.create(loc, (T)tag, args2, this, false, false);
+            return Forest.create(loc, (T)tag, args2, false, false);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {
             int len = sb.length();
index fb02aaf..2df1a4a 100644 (file)
@@ -17,7 +17,7 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
         public CharToStringParser(Union u) { super(u); }
         public Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
         public Forest<String> shiftedToken(CharToken ct, Token.Location loc) {
-            return Forest.create(loc, ct.result(), null, null, false, false);
+            return Forest.create(loc, ct.result(), null, false, false);
         }
     }