checkpoint harmony
[sbp.git] / src / edu / berkeley / sbp / GSS.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.util.*;
4 import edu.berkeley.sbp.Sequence.Position;
5 import edu.berkeley.sbp.Parser.Table.State;
6 import edu.berkeley.sbp.Parser.Table.Reduction;
7 import java.io.*;
8 import java.util.*;
9 import java.lang.reflect.*;
10
11 /** implements Tomita's Graph Structured Stack */
12 class GSS {
13
14     public GSS() { }
15
16     private Phase.Node[] reducing_list = null;
17     public int resets = 0;
18     public int waits = 0;
19
20     HashMapBag<Integer,Sequence>       inhibited = new HashMapBag<Integer,Sequence>();
21     HashMapBag<Sequence,Phase.Waiting> waiting   = new HashMapBag<Sequence,Phase.Waiting>();
22     HashMapBag<Integer,Sequence>       performed = new HashMapBag<Integer,Sequence>();
23     
24     /** FIXME */
25     public  Forest.Ref finalResult;
26
27     /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
28     public class Phase implements Invokable<State, Forest, GSS.Phase.Node>, IntegerMappable {
29
30         /** the token immediately after this phase */
31         final Token token;
32
33         private final int pos;
34
35         boolean reducing;
36         private IntPairMap<Phase.Node> hash;  /* ALLOC */
37         private boolean closed;
38         private boolean good;
39         private Phase next = null;
40         private Phase prev;
41         private Token.Location location;
42         public final Parser parser;
43
44         private Forest forest;
45
46         public Phase(Phase prev, Parser parser, Phase previous, Token token, Token.Location location, Forest forest) {
47             this.prev = prev;
48             this.forest = forest;
49             this.parser = parser;
50             this.pos = previous==null ? 0 : previous.pos+1;
51             this.token = token;
52             this.location = location;
53             inhibited.clear();
54             reset();
55         }
56
57         public void reset() {
58             waiting.clear();
59             performed.clear();
60             hash = new IntPairMap<Phase.Node>();
61             good = false;
62             closed = false;
63             reducing = false;
64             finalResult = null;
65             if (prev != null) prev.shift(this, forest);
66         }
67
68       
69         public boolean isDone() throws ParseFailed {
70             if (token != null) return false;
71             if (token==null && finalResult==null)
72                 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()), getLocation());
73             return true;
74         }
75
76         public Token.Location getLocation() { return location; }
77
78         /** add a new node (merging with existing nodes if possible)
79          *  @param parent             the parent of the new node
80          *  @param result             the SPPF result corresponding to the new node
81          *  @param state              the state that the new node is in
82          *  @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
83          *  @param start              the earliest part of the input contributing to this node (used to make merging decisions)
84          */
85         public boolean newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
86             Node p = hash.get(state, parent==null?null:parent.phase());
87             if (p != null)  return newNode2(p, parent, pending, state, fromEmptyReduction);
88             else            return newNode3(parent, pending, state, fromEmptyReduction);
89         }
90         public void newNode(Node parent, Forest pending, State state, boolean fromEmptyReduction, Reduction reduction) {
91             int pos = parent==null?0:parent.phase()==null?0:parent.phase().pos;
92             Sequence owner = reduction==null ? null : reduction.position.owner();
93             if (reduction!=null) {
94                 if (inhibited.contains(pos, owner)) return;
95                 if (owner.needs != null)
96                     for(Sequence s : owner.needs)
97                         if (!performed.contains(pos, s)) {
98                             waiting.add(s, new Waiting(parent, pending, state, fromEmptyReduction, reduction));
99                             return;
100                         }
101                 if ((owner.needed != null && owner.needed.size()>0) ||
102                     (owner.hated != null && owner.hated.size()>0) ||
103                     (owner.hates != null && owner.hates.size()>0))
104                     performed.add(pos, owner);
105             }
106             if (!owner.lame)
107                 newNode(parent, pending, state, fromEmptyReduction);
108             if (reduction!=null) inhibit(reduction, parent==null?0:parent.phase().pos);
109             if (reduction != null) {
110                 boolean redo = true;
111                 while(redo) {
112                     redo = false;
113                     for(Waiting w : waiting.getAll(owner)) {
114                         if (w.parent==parent || (parent!=null&&w.parent!=null&&w.parent.phase()==parent.phase())) {
115                             waiting.remove(owner, w);
116                             w.perform();
117                             redo = true;
118                             break;
119                         }
120                     }
121                 }
122             }
123         }
124         private boolean newNode2(Node p, Node parent, Forest pending, State state, boolean fromEmptyReduction) {
125             p.holder.merge(pending);
126             if (p.parents().contains(parent)) return true;
127             p.parents().add(parent, true);
128             if (p!=parent && !fromEmptyReduction && reducing) p.performReductions(parent);
129             return true;
130         }
131         private boolean newNode3(Node parent, Forest pending, State state, boolean fromEmptyReduction) {
132             do {
133                 if (token != null && state.canShift(token)) break;
134                 if (state.isAccepting()) break;
135                 if (token==null) break;
136                 if (!state.canReduce(token)) return false;
137                 //if (count > 1) break;
138                 //if (r.numPop == 0) break;
139                 //r.reduce(pending, parent, null, Phase.this, null);
140                 //return;
141             } while(false);
142
143             Node n = new Node(parent, pending, state);  // ALLOC
144             if (reducing) {
145                 n.performEmptyReductions();
146                 if (!fromEmptyReduction) n.performReductions(parent);
147             }
148             return true;
149         }
150
151         public void uninhibit(int p, Sequence s) {
152             if (s.hated!=null)
153                 for(Sequence s2 : s.hated)
154                     inhibited.remove(p, s2);
155         }
156
157         public void inhibit(Reduction r, int p) {
158             if (r.position.owner().hated == null) return;
159             // remember that dead states are still allowed to shift -- just not allowed to reduce
160             boolean reset = false;
161             for(Sequence seq : r.position.owner().hated) {
162                 if (performed.contains(p,seq)) {
163                     uninhibit(p, seq);
164                     //System.out.println("\nresetting due to " + r.position.owner() + " killing " + seq);
165                     //inhibited.clear();
166                     inhibited.add(p, seq);
167                     //inhibited = new HashMapBag<Integer,Sequence>();
168                     reset = true;
169                     resets++;
170                     throw new Reset();
171                 }
172                 inhibited.add(p, seq);
173             }
174         }
175         
176         /** perform all reduction operations */
177         public void reduce() {
178             try {
179                 reducing = true;
180                 if (reducing_list==null || reducing_list.length < hash.size())
181                     reducing_list = new Phase.Node[hash.size() * 4];
182                 hash.toArray(reducing_list);
183                 int num = hash.size();
184                 for(int i=0; i<num; i++) {
185                     Node n = reducing_list[i];
186                     n.performEmptyReductions();
187                     // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
188                 }
189                 for(int i=0; i<num; i++) {
190                     Node n = reducing_list[i];
191                     reducing_list[i] = null;
192                     n.performReductions();
193                 }
194             } catch (Reset r) {
195                 reset();
196                 reduce();
197             }
198         }
199
200         class Reset extends RuntimeException { }
201
202         public void invoke(State st, Forest result, Node n) {
203             good |= next.newNode(n, result, st, false);
204         }
205
206         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
207         public void shift(Phase next, Forest result) throws ParseFailed {
208             // this massively improves GC performance
209             if (prev!=null) prev.hash = null;
210             this.next = next;
211             closed = true;
212             Forest res = null;
213             boolean ok = false;
214             for(Phase.Node n : hash.values()) {
215                 if (token == null && n.state.isAccepting()) {
216                     if (finalResult==null) finalResult = new Forest.Ref();
217                     finalResult.merge(n.holder);
218                 }
219                 if (token == null) continue;
220                 n.state.invokeShifts(token, this, result, n);
221             }
222
223             if (!good && token!=null)
224                 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected character")+" "+ANSI.purple(token)+" encountered at "+ANSI.green(getLocation())+"\n", token, hash.values()),
225                                         getLocation());
226             if (token==null && finalResult==null)
227                 throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()),
228                                         getLocation());
229         }
230
231
232         public class Waiting {
233             Node parent;
234             Forest pending;
235             State state;
236             boolean fromEmptyReduction;
237             Reduction reduction;
238             public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Reduction reduction) {
239                 waits++;
240                 this.parent = parent;
241                 this.pending = pending;
242                 this.state = state;
243                 this.fromEmptyReduction = fromEmptyReduction;
244                 this.reduction = reduction;
245             }
246             public void perform() {
247                 //System.out.println("performing: " + reduction.position);
248                 newNode(parent, pending, state, fromEmptyReduction, reduction);
249             }
250         }
251        
252         // Node /////////////////////////////////////////////////////////////////////////////////
253
254         /** a node in the GSS */
255         public final class Node extends FastSet<Node> implements Invokable<Reduction, Node, Node> {
256
257             private Forest.Ref holder = null;
258             private boolean allqueued = false;
259
260             /** what state this node is in */
261             public final State state;
262
263             /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
264             public  Phase phase() { return Phase.this; }
265             public  Forest.Ref holder() { return holder==null ? (holder = new Forest.Ref()) : holder; }
266             public  Forest pending() { return Phase.this.closed ? holder().resolve() : holder; }
267             public  FastSet<Node> parents() { return this; }
268
269             public void performReductions() {
270                 if (allqueued) return;
271                 allqueued = true;
272                 state.invokeReductions(token, this, this, null);
273             }
274
275             public void performReductions(Node n2) {
276                 if (!allqueued) performReductions();
277                 else            state.invokeReductions(token, this, this, n2);
278             }
279
280             public final void invoke(Reduction r, Node n, Node n2) {
281                 if (n==null || n2==null || r.position.pos==0) {
282                     if (r.position.pos==0) {
283                         if (n==null) n = this;
284                         else return;
285                     }
286                     if (n==null) return;
287                     Forest[] holder = new Forest[r.position.pos];
288                     if (r.position.pos==0) n.finish(r, r.zero(), n.phase(), holder);
289                     else                   r.reduce(n, r.position.pos-1, n.phase(), holder);
290                 } else {
291                     Forest[] holder = new Forest[r.position.pos];
292                     if (r.position.pos<=0) throw new Error("called wrong form of reduce()");
293                     int pos = r.position.pos-1;
294                     Forest old = holder[pos];
295                     holder[pos] = n.pending();
296                     if (pos==0) {
297                         System.arraycopy(holder, 0, r.position.holder, 0, holder.length);
298                         Forest rex = r.position.rewrite(n.phase().getLocation());
299                         n2.finish(r, rex, n.phase(), holder);
300                     } else {
301                         r.reduce(n2, pos-1, n.phase(), holder);
302                     }
303                     holder[pos] = old;
304                 }
305             }
306
307             public void finish(Reduction r, Forest result, GSS.Phase target, Forest[] holder) {
308                 State state0 = state.gotoSetNonTerminals.get(r.position.owner());
309                 if (result==null) throw new Error();
310                 if (state0!=null)
311                     target.newNode(this, result, state0, r.position.pos<=0, r);
312             }
313
314             public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
315
316             private Node(Node parent, Forest pending, State state) {
317                 this.state = state;
318                 this.holder().merge(pending);
319                 Phase start = parent==null ? null : parent.phase();
320                 if (parent != null) parents().add(parent, true);
321                 if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
322                 Phase.this.hash.put(state, start, this);
323             }
324         }
325
326         public int toInt() { return pos+1; }
327         public int size() { return hash==null ? 0 : hash.size(); }
328     }
329
330 }