1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Position;
10 import java.lang.reflect.*;
12 /** a node in the GSS */
14 implements Invokable<Position, Boolean, Result>,
19 /** which GSS.Phase this Node belongs to */
20 public GSS.Phase phase() { return phase; }
21 public Iterator<Result> iterator() { return results.iterator(); }
22 public Parser.Table.State state() { return state; }
24 public int toInt() { return idx; }
26 public boolean merge(Result r) {
27 if (results.contains(r)) return true;
29 if (fromEmptyReduction) return false;
30 state.invokeReductions(phase().getToken(), this, false, r);
34 //////////////////////////////////////////////////////////////////////
36 private static int node_idx = 0;
37 private final int idx = node_idx++;
39 private final GSS.Phase phase;
40 private final Parser.Table.State state;
41 private final boolean fromEmptyReduction;
42 //private FastSet<Result> results = new FastSet<Result>();
43 private HashSet<Result> results = new HashSet<Result>();
45 public final void invoke(Position r, Boolean emptyProductions, Result only) {
46 if (emptyProductions != (r.pos==0)) return;
47 if (r.pos==0) finish(r, r.zero(phase().getLocation().createRegion(phase().getLocation())), phase());
48 else reduce(r, r.pos-1, phase(), only);
51 private void reduce(Position r, int pos, GSS.Phase target, Result only) {
52 Forest[] holder = r.holder;
53 Forest old = holder[pos];
54 for(Result res : results)
55 if (only == null || res == only) {
56 Node child = res.parent();
57 holder[pos] = res.getForest();
58 if (pos>0) child.reduce(r, pos-1, target, null);
61 new Reduction(child, r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
62 target.reductionQueue.add(reduction);
69 void finish(Position r, Forest forest, GSS.Phase target) {
70 Parser.Table.State state0 = (Parser.Table.State)state.gotoSetNonTerminals.get(r.owner());
71 Result result = new Result(forest, this, r);
72 target.newNodeFromReduction(result, state0, r.pos<=0, r);
75 Node(GSS.Phase phase, Result result, State state, boolean fromEmptyReduction) {
78 this.fromEmptyReduction = fromEmptyReduction;
80 if (phase.hash.get(state, result.phase()) != null) throw new Error("severe problem!");
81 phase.hash.put(state, result.phase(), this);
83 for(Object s : state.also)
84 phase.newNode(result, (State)s, fromEmptyReduction);
86 state.invokeReductions(phase().token, this, true, null);
87 if (!fromEmptyReduction)
88 state.invokeReductions(phase().getToken(), this, false, null);
91 // GraphViz //////////////////////////////////////////////////////////////////////////////
93 public GraphViz.Node toGraphViz(GraphViz gv) {
94 if (gv.hasNode(this)) return gv.createNode(this);
95 GraphViz.Node n = gv.createNode(this);
96 n.label = ""+state.toStringx();
97 n.shape = "rectangle";
98 boolean hasparents = false;
99 //for(Node parent : parents()) { hasparents = true; n.edge(parent, ""); }
100 //for(Forest result : resultMap) n.edge(result, "");
101 n.color = !hasparents ? "blue" : /*state.evil ? "red" :*/ "green";
102 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
105 public boolean isTransparent() { return false; }
106 public boolean isHidden() { return false; }