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 //////////////////////////////////////////////////////////////////////////////
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) {
}
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);
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);
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;
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> { }
-
+
}
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() {
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 {
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) {
--- /dev/null
+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> {
+}
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 ""; }
* @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);
}
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);
}
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); }
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.
}
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 {