corrected the reporting+alignment of error locations
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.Sequence.Position;
4 import edu.berkeley.sbp.util.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /**
10  *   <font color=blue>
11  *   An efficient representation of a collection of trees (Tomita's
12  *   shared packed parse forest).
13  *   </font>
14  */
15 public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
16
17     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
18     public abstract Tree<NodeType> expand1() throws Ambiguous;
19
20     /** expand this forest into a set of trees */
21     public void expand(HashSet<Tree<NodeType>> ht) { expand(ht, new HashSet<Forest<NodeType>>(), null); }
22
23     static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
24         return new One<NodeType>(loc, head, children, lift);
25     }
26
27     /** create a new forest */
28     public static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children) {
29         return Forest.create(loc, head, children, false); }
30
31     // Package-Private //////////////////////////////////////////////////////////////////////////////
32
33     abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
34     abstract void gather(HashSet<Forest<NodeType>> ignore);
35     abstract void edges(GraphViz.Node n);
36     boolean ambiguous() { return false; }
37     
38     abstract Input.Region getRegion();
39
40     // One //////////////////////////////////////////////////////////////////////////////
41
42     /** A "single" forest with a head and child subforests */    
43     private static class One<NodeType> extends Forest<NodeType> {
44
45         private final Input.Region      location;
46         private final NodeType                head;
47         private final Forest<NodeType>[]       children;
48
49         /** if true, the last child's children are considered children of this node */
50         private final boolean           lift;
51
52         Input.Region getRegion() { return location; }
53
54         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
55             this.location = loc;
56             this.head = head;
57             this.children = children==null ? emptyForestArray : new Forest[children.length];
58             if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
59             if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
60             this.lift = lift;
61         }
62
63         public Tree<NodeType> expand1() throws Ambiguous {
64             Tree<NodeType>[] ret = new Tree[children.length];
65             for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
66             return new Tree<NodeType>(location, head, ret, lift);
67         }
68
69         void gather(HashSet<Forest<NodeType>> hf) {
70             hf.add(this);
71             for(Forest<NodeType> f : children) f.gather(hf);
72         }
73         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
74             if (ignore.contains(this)) { ht.add(bogus); return; }
75             expand(0, new Tree[children.length], ht, ignore, bogus);
76         }
77         private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
78             if (i==children.length) {
79                 ht.add(new Tree<NodeType>(location, head, ta, lift));
80             } else {
81                 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
82                 children[i].expand(ht2, ignore, bogus);
83                 for(Tree<NodeType> tc : ht2) {
84                     ta[i] = tc;
85                     expand(i+1, ta, ht, ignore, bogus);
86                     ta[i] = null;
87                 }
88             }
89         }
90
91         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
92
93         public boolean isTransparent() { return false; }
94         public boolean isHidden() { return false; }
95         public GraphViz.Node toGraphViz(GraphViz gv) {
96             if (gv.hasNode(this)) return gv.createNode(this);
97             GraphViz.Node n = gv.createNode(this);
98             n.label = headToString()==null?"":headToString();
99             n.directed = true;
100             edges(n);
101             return n;
102         }
103         boolean edges = false; // FIXME ??
104         public void edges(GraphViz.Node n) {
105             if (edges) return;
106             edges = true;
107             for(int i=0; i<children.length; i++) {
108                 if (i==children.length-1 && lift && !children[i].ambiguous()) {
109                     children[i].edges(n);
110                 } else {
111                     n.edge(children[i], null);
112                 }
113             }
114         }
115
116         protected String  headToString()         { return head==null?null:head.toString(); }
117         protected String  headToJava()           { return "null"; }
118         protected String  left()                 { return "{"; }
119         protected String  right()                { return "}"; }
120         protected boolean ignoreSingleton()      { return false; }
121     }
122
123
124     // Many //////////////////////////////////////////////////////////////////////////////
125
126     /** An "ambiguity node"; this is immutable once it has been "looked at" */
127     static class Many<NodeType> extends Forest<NodeType> {
128
129         HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
130         private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
131         private boolean touched = false;
132
133         public Many() { }
134
135         Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
136
137         public Tree<NodeType> expand1() throws Ambiguous {
138             touched();
139             if (hp.size() > 1) {
140                 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
141                 Iterator<Forest<NodeType>> ih = hp.iterator();
142                 ih.next().gather(hf0);
143                 for(Forest<NodeType> f : hp) {
144                     HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
145                     f.gather(hf1);
146                     hf0.retainAll(hf1);
147                 }
148                 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
149                 expand(ht, hf0, new Tree(null, "*"));
150                 throw new Ambiguous((Forest<?>)this,
151                                     (HashSet<Tree<?>>)(Object)ht);
152             }
153             return hp.iterator().next().expand1();
154         }
155         
156         void gather(HashSet<Forest<NodeType>> ht) {
157             touched();
158             ht.add(this);
159             for(Forest<NodeType> f : hp) f.gather(ht);
160         }
161
162         private void touched() {
163             if (touched) return;
164             touched = true;
165             /*
166             FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
167             for(Forest f : hp)
168                 if (f instanceof Forest.One) f2.add(f);
169                 else for(Forest ff : ((Forest.Many<NodeType>)f))
170                     f2.add(ff);
171             hp = f2;
172             */
173         }
174         public boolean contains(Forest f) {
175             touched();
176             return hp.contains(f);
177         }
178         public void merge(Forest p) { 
179             if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
180             if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
181             hp.add(p, true);
182         }
183         boolean ambiguous() {
184             touched();
185             if (hp.size()==0) return false;
186             if (hp.size()==1) return hp.iterator().next().ambiguous();
187             return true;
188         }
189
190         void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
191             touched();
192             if (ignore.contains(this)) { ht.add(bogus); return; }
193             for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
194         }
195
196
197         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
198
199         public boolean isTransparent() { return hp.size()==1; }
200         public boolean isHidden() { return hp.size()==0; }
201         public void edges(GraphViz.Node n) {
202             if (hp.size()==1) { hp.iterator().next().edges(n); return; }
203             for(Forest f : hp) f.edges(n);
204         }
205         public GraphViz.Node toGraphViz(GraphViz gv) {
206             if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
207             if (gv.hasNode(this)) return gv.createNode(this);
208             GraphViz.Node n = gv.createNode(this);
209             n.label = "?";
210             n.color = "red";
211             for(Forest f : hp) n.edge(f, null);
212             return n;
213         }
214     }
215
216     // Statics //////////////////////////////////////////////////////////////////////////////
217
218     private static Tree[] tree_hint = new Tree[0];
219     private static String[] string_hint = new String[0];
220     private static final Forest[] emptyForestArray = new Forest[0];
221
222     protected String  headToString()    { return null; }
223     protected String  headToJava()      { return "null"; }
224     protected String  left()            { return "<?"; }
225     protected String  right()           { return "?>"; }
226     protected boolean ignoreSingleton() { return true; }
227 }