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);
59 else new Reduction(child, r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
64 void finish(Position r, Forest forest, GSS.Phase target) {
65 Parser.Table.State state0 = (Parser.Table.State)state.gotoSetNonTerminals.get(r.owner());
66 Result result = new Result(forest, this, r);
67 target.newNodeFromReduction(result, state0, r.pos<=0, r);
70 Node(GSS.Phase phase, Result result, State state, boolean fromEmptyReduction) {
73 this.fromEmptyReduction = fromEmptyReduction;
75 if (phase.hash.get(state, result.phase()) != null) throw new Error("severe problem!");
76 phase.hash.put(state, result.phase(), this);
78 for(Object s : state.also)
79 phase.newNode(new Result(null, this, null), (State)s, fromEmptyReduction);
81 state.invokeReductions(phase().token, this, true, null);
82 if (!fromEmptyReduction)
83 state.invokeReductions(phase().getToken(), this, false, null);
86 // GraphViz //////////////////////////////////////////////////////////////////////////////
88 public GraphViz.Node toGraphViz(GraphViz gv) {
89 if (results.size()==0) return null;
90 if (gv.hasNode(this)) return gv.createNode(this);
91 GraphViz.Node n = gv.createNode(this);
92 n.label = ""+state.toStringx();
93 n.shape = "rectangle";
94 boolean hasparents = false;
95 for(Result r : results) n.edge(r, "");
96 n.color = state.doomed ? "red" : "green";
97 ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
100 public boolean isTransparent() { return false; }
101 public boolean isHidden() { return false; }