- public GraphViz.Node toGraphViz(GraphViz gv) {
- 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; }
-
- public static interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> {
- }
-
- public static class ArrayBuildingTreeFunctor<T> implements TreeFunctor<T,T[]>, ToJava {
-
- public void toJava(StringBuffer sb) { sb.append("new Tree.ArrayBuildingTreeFunctor()"); }
- public String toString() { return ""; }
-
- public T[] invoke(Iterable<Tree<T>> t) {
- ArrayList ret = new ArrayList();
- for(Tree tc : t) {
- if (tc.head() != null && tc.head() instanceof Functor)
- ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc.children()));
- else if (tc.numChildren() == 0)
- ret.add(tc.head());
- else {
- System.err.println("FIXME: don't know what to do about " + tc);
- ret.add(null);
- }
- }
- return (T[])ret.toArray(new Object[0]);
- }
- }
-
- public static class RawBindingFunctor<T> implements TreeFunctor<T,Object>, ToJava {
- private Binding _binding;
- private String _toString;
- public RawBindingFunctor(String toString, Binding binding) {
- this._binding = binding;
- this._toString = toString;
- }
- public String toString() {
- return _toString;
- }
- public void toJava(StringBuffer sb) {
- sb.append("new Tree.RawBindingFunctor(");
- sb.append("\"");
- sb.append(StringUtil.toJavaString(_toString));
- sb.append("\", ");
- _binding.toJava(sb);
- sb.append(")");
- }
- public Object invoke(Iterable<Tree<T>> t) {
- return _binding.invoke(new Object[] { t });
- }
- }
-
- public static class BindingFunctor<T> implements TreeFunctor<T,Object>, ToJava {
- private Binding _binding;
- private String _toString;
- public String toString() {
- return _toString;
- }
- public BindingFunctor(String toString, Binding binding) {
- this._binding = binding;
- this._toString = toString;
- }
- public void toJava(StringBuffer sb) {
- sb.append("new Tree.BindingFunctor(");
- sb.append("\"");
- sb.append(StringUtil.toJavaString(_toString));
- sb.append("\", ");
- _binding.toJava(sb);
- sb.append(")");
- }
- public Object invoke(Iterable<Tree<T>> t) {
- 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()));
- else if (tc.numChildren() == 0)
- ret.add(tc.head());
- else {
- System.err.println("FIXME: don't know what to do about " + tc);
- ret.add(null);
- }
- }
- Object[] o = (Object[])ret.toArray(new Object[0]);
- return _binding.invoke(o);
- }
- }