refactored Topology to make it a value (immutable) class
[sbp.git] / src / edu / berkeley / sbp / Union.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
9
10 /** an element which can produce one of several alternatives */
11 public class Union extends Element implements Iterable<Sequence> {
12
13     final String shortForm;
14     final boolean synthetic;
15     private final List<Sequence> alternatives = new ArrayList<Sequence>();
16
17     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
18
19     Topology toAtom() {
20         if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions");
21         Topology ret = null;
22         for(Sequence s : this) {
23             Topology a = s.toAtom();
24             if (ret==null) ret = a;
25             else           ret = ret.union(a);
26         }
27         if (ret==null) throw new RuntimeException("confusion on " + this);
28         return ret;
29     }
30
31     /** adds an alternative */
32     public void add(Sequence s) { alternatives.add(s); }
33
34     /**
35      *  Since every cycle in a non-degenerate grammar contains at
36      *  least one Union, every instance of this class must be able to
37      *  display itself in both "long form" (list of the long forms of
38      *  its alternatives) and "short form" (some abbreviation).
39      *
40      *  @param shortForm the "short form" display; usually 
41      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
42      */
43     public Union(String shortForm) { this(shortForm, false); }
44     public Union(String shortForm, boolean synthetic) {
45         this.shortForm = shortForm;
46         this.synthetic = synthetic;
47     }
48
49     public static Union epsilon = new Union("()");
50     static { epsilon.add(Sequence.empty); }
51
52     private Forest.Ref epsilonForm = null;
53     Forest epsilonForm() {
54         if (epsilonForm != null) return epsilonForm;
55         epsilonForm = new Forest.Ref();
56         for(Sequence s : this)
57             if (s.possiblyEpsilon(null))
58                 epsilonForm.merge(s.epsilonForm());
59         return epsilonForm;
60     }
61
62     // Display //////////////////////////////////////////////////////////////////////////////
63
64     public String toString() { return shortForm; }
65     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
66     public void toString(StringBuffer sb) {
67         if (synthetic) return;
68         boolean first = true;
69         if (alternatives.size()==0) {
70             sb.append(pad(15, shortForm) + "::= ");
71         } else for(Sequence s : this) {
72             sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : "  | "));
73             first = false;
74             sb.append(s.toString());
75             sb.append('\n');
76         }
77         sb.append('\n');
78     }
79
80     // SubUnion //////////////////////////////////////////////////////////////////////////////
81
82     /** FIXME this is kind of a hack */
83     public class Subset extends Union {
84         private final Set<Sequence> exclude;
85         public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
86         public Iterator<Sequence> iterator() {
87             final Iterator<Sequence> it = Union.this.iterator();
88             return new Iterator<Sequence>() {
89                 private Sequence next = it.hasNext() ? it.next() : null;
90                 public boolean hasNext() { return next != null; }
91                 public Sequence next() {
92                     Sequence ret = next;
93                     do {
94                         next = it.hasNext() ? it.next() : null;
95                     } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
96                     return ret;
97                 }
98                 public void remove() { throw new Error(); }
99             };
100         }
101     }
102
103 }