87aefda66b24e39b51476a6471d68776baafa960
[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     public boolean contains(Sequence s) { return alternatives.contains(s); }
19
20     Topology toAtom() {
21         if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions");
22         Topology ret = null;
23         for(Sequence s : this) {
24             Topology a = s.toAtom();
25             if (ret==null) ret = a;
26             else           ret = ret.union(a);
27         }
28         if (ret==null) throw new RuntimeException("confusion on " + this);
29         return ret;
30     }
31
32     /** adds an alternative */
33     public void add(Sequence s) {
34         alternatives.add(s);
35         for(Sequence n : s.needs) add(n);
36         for(Sequence n : s.hates) add(n);
37         if (/*!synthetic &&*/ shortForm!=null
38             //&& Character.isUpperCase(shortForm.charAt(0))
39             )
40             s.setName(toString());
41     }
42
43     /**
44      *  Since every cycle in a non-degenerate grammar contains at
45      *  least one Union, every instance of this class must be able to
46      *  display itself in both "long form" (list of the long forms of
47      *  its alternatives) and "short form" (some abbreviation).
48      *
49      *  @param shortForm the "short form" display; usually 
50      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
51      */
52     public Union(String shortForm) { this(shortForm, false); }
53     public Union(String shortForm, boolean synthetic) {
54         this.shortForm = shortForm;
55         this.synthetic = synthetic;
56     }
57
58     public static Union epsilon = new Union("()");
59     static { epsilon.add(Sequence.empty); }
60
61     private Forest.Ref epsilonForm = null;
62     Forest epsilonForm() {
63         if (epsilonForm != null) return epsilonForm;
64         epsilonForm = new Forest.Ref();
65         for(Sequence s : this)
66             if (s.possiblyEpsilon(null))
67                 epsilonForm.merge(s.epsilonForm());
68         return epsilonForm;
69     }
70
71     // Display //////////////////////////////////////////////////////////////////////////////
72
73     public String toString() { return shortForm; }
74     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
75     public void toString(StringBuffer sb) {
76         if (synthetic) return;
77         boolean first = true;
78         if (alternatives.size()==0) {
79             sb.append(pad(15, shortForm) + "::= ");
80         } else for(Sequence s : this) {
81             sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : "  | "));
82             first = false;
83             sb.append(s.toString());
84             sb.append('\n');
85         }
86         sb.append('\n');
87     }
88
89     // SubUnion //////////////////////////////////////////////////////////////////////////////
90
91     /** FIXME this is kind of a hack */
92     public class Subset extends Union {
93         private final Set<Sequence> exclude;
94         public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
95         public Iterator<Sequence> iterator() {
96             final Iterator<Sequence> it = Union.this.iterator();
97             return new Iterator<Sequence>() {
98                 private Sequence next = it.hasNext() ? it.next() : null;
99                 public boolean hasNext() { return next != null; }
100                 public Sequence next() {
101                     Sequence ret = next;
102                     do {
103                         next = it.hasNext() ? it.next() : null;
104                     } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
105                     return ret;
106                 }
107                 public void remove() { throw new Error(); }
108             };
109         }
110     }
111
112 }