checkpoint
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.util.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
10 public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>> {
11
12     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
14         Iterator<Tree<T>> it = expand(true).iterator();
15         if (!it.hasNext()) throw new ParseFailed();
16         return it.next();
17     }
18
19     /** expand this forest into a set of trees */
20     public HashSet<Tree<T>> expand(boolean toss) {
21         final HashSet<Tree<T>> hs = new HashSet<Tree<T>>();
22         TreeMaker<T> ret = new TreeMaker<T>(toss) {
23             public void add(Tree<T> t) { hs.add(t); }
24         };
25         visit(ret, null, null);
26         if (toss && hs.size() > 1) throw new Ambiguous(this);
27         return hs;
28     }
29
30     static        <T> Forest<T> singleton(Input.Location loc)                       { return create(loc, null, new Forest[] { }, false, true); }
31     static        <T> Forest<T> singleton(Input.Location loc, Forest<T> body)       { return create(loc, null, new Forest[] { body },  false, true); }
32     static        <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
33     public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
34         return new MyBody<T>(loc, tag, tokens, unwrap, singleton);
35     }
36
37     // Body //////////////////////////////////////////////////////////////////////////////
38
39     protected static interface Body<T> {
40         TreeMaker<T> expand(int i, TreeMaker<T> h);
41     }
42
43     protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */, Invokable<Tree<T>, TreeMaker<T>, Integer> {
44
45         public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
46             ivbc.invoke(this, b, c);
47         }
48
49         private final Input.Location    location;
50         private final T                 tag;
51         private final Forest<T>[]       tokens;
52         private final boolean           unwrap;
53         private final boolean           singleton;
54
55         private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
56             this.location = loc;
57             this.tag = tag;
58             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
59             if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
60             if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
61             this.unwrap = unwrap;
62             this.singleton = singleton;
63         }
64
65         public TreeMaker<T> expand(int i, TreeMaker<T> h) {
66             if (singleton) {
67                 tokens[0].visit(h, null, i);
68
69             } else if (i==tokens.length) {
70                 h.add(new Tree<T>(null, tag, h.toks.toArray(tree_hint)));
71
72             } else if (unwrap && i==tokens.length-1) {
73                 if (tokens[i] != null)
74                     tokens[i].visit(h, null, 0);
75
76             } else {
77                 //boolean hit = false;
78                 //tokens[i].visit(this, h, i);
79
80                 for(Tree<T> r : tokens[i].expand(h.toss)) {
81                     //hit = true;
82                     int old = h.toks.size();
83                     h.toks.add(r);
84                     expand(i+1, h);
85                     while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
86                 }
87
88                 //if (!hit) throw new Error();
89             }
90             return h;
91         }
92         public void invoke(Tree<T> r, TreeMaker<T> h, Integer i) {
93             //hit = true;
94             int old = h.toks.size();
95             h.toks.add(r);
96             expand(i+1, h);
97             while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
98             //ivbc.invoke(this, b, c);
99         }
100
101         protected String  headToString()         { return null; }
102         protected String  headToJava()           { return null; }
103         protected String  left()                 { return "{"; }
104         protected String  right()                { return "}"; }
105         protected boolean ignoreSingleton()      { return false; }
106     }
107
108
109     // Ref //////////////////////////////////////////////////////////////////////////////
110
111     /**
112      *  This class represents a partially complete collection of
113      *  forests to be viewed as a forest at some later date; once
114      *  viewed, it becomes immutable
115      */
116     static class Ref<T> extends Forest<T> {
117         private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
118         public Ref() { }
119         public void merge(Forest p) { if (p!=this) hp.add(p, true); }
120         public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
121             if (hp==null) return;
122             for(Forest<T> f : hp)
123                 f.visit(ivbc, b, c);
124         }
125         public Forest resolve() { return this; }
126     }
127
128     public abstract <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
129     private static abstract class TreeMaker<T> /*extends HashSet<Tree<T>>*/ implements Invokable<Forest.Body<T>,Boolean,Integer> {
130         public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
131         private boolean toss;
132         public TreeMaker(boolean toss) { this.toss = toss; }
133         public abstract void add(Tree<T> t);
134         public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
135             if (i==null) {
136                 ArrayList<Tree<T>> toks = this.toks;
137                 this.toks = new ArrayList<Tree<T>>();
138                 bod.expand(0, this);
139                 this.toks = toks;
140             } else {
141                 bod.expand(i, this);
142             }
143         }
144     }
145
146     // Statics //////////////////////////////////////////////////////////////////////////////
147
148     private static Tree[] tree_hint = new Tree[0];
149     private static final Forest[] emptyForestArray = new Forest[0];
150
151     protected String  headToString()    { return null; }
152     protected String  headToJava()      { return null; }
153     protected String  left()            { return "<?"; }
154     protected String  right()           { return "?>"; }
155     protected boolean ignoreSingleton() { return true; }
156 }