checkpoint
authoradam <adam@megacz.com>
Sun, 16 Jul 2006 07:42:25 +0000 (03:42 -0400)
committeradam <adam@megacz.com>
Sun, 16 Jul 2006 07:42:25 +0000 (03:42 -0400)
darcs-hash:20060716074225-5007d-a2ecb46e6c337998e5183c071c1fd1327bf81a1f.gz

src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/Tree.java
src/edu/berkeley/sbp/bind/BindingFunctor.java
src/edu/berkeley/sbp/bind/RawBindingFunctor.java
src/edu/berkeley/sbp/bind/TreeFunctor.java [new file with mode: 0644]
src/edu/berkeley/sbp/meta/ArrayBuildingTreeFunctor.java
src/edu/berkeley/sbp/meta/Grammar.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/misc/Demo.java
src/edu/berkeley/sbp/util/GraphViz.java

index 1c8fbc5..645850b 100644 (file)
@@ -19,8 +19,8 @@ public abstract class Forest<T> implements GraphViz.ToGraphViz {
     public void expand(HashSet<Tree<T>> ht) { expand(ht, new HashSet<Forest<T>>(), null); }
 
     /** create a new forest */
-    public static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children, boolean unwrap) {
-        return new One<T>(loc, head, children, unwrap);
+    public static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
+        return new One<T>(loc, head, children, lift);
     }
 
     // Package-Private //////////////////////////////////////////////////////////////////////////////
@@ -42,21 +42,21 @@ public abstract class Forest<T> implements GraphViz.ToGraphViz {
         private final Forest<T>[]       children;
 
         /** if true, the last child's children are considered children of this node */
-        private final boolean           unwrap;
+        private final boolean           lift;
 
-        private One(Input.Region loc, T head, Forest<T>[] children, boolean unwrap) {
+        private One(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
             this.location = loc;
             this.head = head;
             this.children = children==null ? emptyForestArray : new Forest[children.length];
             if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
             if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
-            this.unwrap = unwrap;
+            this.lift = lift;
         }
 
         public Tree<T> expand1() throws Ambiguous {
             Tree<T>[] ret = new Tree[children.length];
             for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
-            return new Tree<T>(location, head, ret, unwrap);
+            return new Tree<T>(location, head, ret, lift);
         }
 
         void gather(HashSet<Forest<T>> hf) {
@@ -69,7 +69,7 @@ public abstract class Forest<T> implements GraphViz.ToGraphViz {
         }
         private void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
             if (i==children.length) {
-                ht.add(new Tree<T>(location, head, ta, unwrap));
+                ht.add(new Tree<T>(location, head, ta, lift));
             } else {
                 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
                 children[i].expand(ht2, ignore, bogus);
@@ -98,7 +98,7 @@ public abstract class Forest<T> implements GraphViz.ToGraphViz {
             if (edges) return;
             edges = true;
             for(int i=0; i<children.length; i++) {
-                if (i==children.length-1 && unwrap && !children[i].ambiguous()) {
+                if (i==children.length-1 && lift && !children[i].ambiguous()) {
                     children[i].edges(n);
                 } else {
                     n.edge(children[i], null);
index d2bfc4b..2f72029 100644 (file)
@@ -52,6 +52,8 @@ public class Tree<T>
 
     public Tree(Input.Region loc, T head)                     { this(loc, head, null); }
     public Tree(Input.Region loc, T head, Tree<T>[] children) { this(loc, head, children, false); }
+
+    /** package-private constructor, allows setting the "lift" bit */
     Tree(Input.Region loc, T head, Tree<T>[] children, boolean lift) {
         this.location = loc;
         this.head = head;
@@ -84,16 +86,11 @@ public class Tree<T>
         if (gv.hasNode(this)) return gv.createNode(this);
         GraphViz.Node n = gv.createNode(this);
         n.label = head()==null ? "" : head().toString();
-        //n.color = "red";
         for(Tree t : this) n.edge(t, null);
         return n;
     }
     public boolean isTransparent() { return false; }
     public boolean isHidden() { return false; }
 
-
-    // TreeFunctor /////////////////////////////////////////////////////////////////////////////
-
-    public static interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> { }
-    
+   
 }
index cfe324d..8a4d042 100644 (file)
@@ -6,7 +6,7 @@ import java.io.*;
 import java.util.*;
 import java.lang.reflect.*;
 
-public class BindingFunctor<T> implements Tree.TreeFunctor<T,Object>, ToJava {
+public class BindingFunctor<T> implements TreeFunctor<T,Object>, ToJava {
     private Binding _binding;
     private String _toString;
     public String toString() {
@@ -28,7 +28,7 @@ public class BindingFunctor<T> implements Tree.TreeFunctor<T,Object>, ToJava {
         ArrayList ret = new ArrayList();
         for(Tree tc : t) {
             if (tc.head() != null && tc.head() instanceof Functor)
-                ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
+                ret.add(((TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
             else if (tc.numChildren() == 0)
                 ret.add(tc.head());
             else {
index a7c670f..52817d1 100644 (file)
@@ -6,7 +6,7 @@ import java.io.*;
 import java.util.*;
 import java.lang.reflect.*;
 
-public class RawBindingFunctor<T> implements Tree.TreeFunctor<T,Object>, ToJava {
+public class RawBindingFunctor<T> implements TreeFunctor<T,Object>, ToJava {
     private Binding _binding;
     private String _toString;
     public RawBindingFunctor(String toString, Binding binding) {
diff --git a/src/edu/berkeley/sbp/bind/TreeFunctor.java b/src/edu/berkeley/sbp/bind/TreeFunctor.java
new file mode 100644 (file)
index 0000000..671901a
--- /dev/null
@@ -0,0 +1,10 @@
+package edu.berkeley.sbp.bind;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.bind.*;
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+
+public interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> {
+}
index f173e9c..f1477ed 100644 (file)
@@ -10,7 +10,7 @@ import java.lang.annotation.*;
 import java.lang.reflect.*;
 import java.io.*;
 
-public class ArrayBuildingTreeFunctor<T> implements Tree.TreeFunctor<T,T[]>, ToJava {
+public class ArrayBuildingTreeFunctor<T> implements TreeFunctor<T,T[]>, ToJava {
 
     public void toJava(StringBuffer sb) { sb.append("new ArrayBuildingTreeFunctor()"); }
     public String toString() { return ""; }
index d9462e0..a80ac28 100644 (file)
@@ -19,7 +19,7 @@ public class Grammar {
      *  @param gbr a GrammarBindingResolver that resolves grammatical reductions into tree-node-heads
      */
     public static Union create(Tree t, String s, Grammar.Bindings gbr) {
-        Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
+        TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
         MetaGrammarBindings.GrammarNode g = (MetaGrammarBindings.GrammarNode)red.invoke(t.children());
         return g.build(s, gbr);
     }
index e3299cb..8f322a5 100644 (file)
@@ -92,7 +92,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
 
         try {
             Tree t = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream("tests/"+fileName)).expand1();
-            Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
+            TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
             String oldprefix = prefix;
             prefix = as;
             GrammarNode gn = (GrammarNode)red.invoke(t);
@@ -394,7 +394,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
         }
         public Context(Tree t, Grammar.Bindings rm) {
             this.rm = rm;
-            Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
+            TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
             this.grammar = (GrammarNode)red.invoke(t);
         }
         public Union peek(String name) { return map.get(name); }
index b118bbf..a9601b9 100644 (file)
@@ -46,7 +46,7 @@ public class Demo {
         Tree tree = mathParser.parse(new CharInput(new StringReader(s[1]))).expand1();
 
         // below is ugly voodoo which will go away very soon.  ignore it.
-        Tree.TreeFunctor tf = (Tree.TreeFunctor)tree.head();
+        TreeFunctor tf = (TreeFunctor)tree.head();
         Math.Expr e = (Math.Expr)tf.invoke(tree);
         // above is ugly voodoo which will go away very soon.  ignore it.
 
index b3132d7..b6db224 100644 (file)
@@ -145,9 +145,9 @@ public class GraphViz {
     }
 
     public static interface ToGraphViz {
-        public Node    toGraphViz(GraphViz gv);
-        public boolean isTransparent();
-        public boolean isHidden();
+        Node    toGraphViz(GraphViz gv);
+        boolean isTransparent();
+        boolean isHidden();
     }
 
     public void show() throws IOException {