4595ebbec869a88a4ccbc9d1192313a8caf03c30
[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     /**
14      *  Since every cycle in a non-degenerate grammar contains at
15      *  least one Union, every instance of this class must be able to
16      *  display itself in both "long form" (list of the long forms of
17      *  its alternatives) and "short form" (some abbreviation).
18      *
19      *  @param shortForm the "short form" display; usually 
20      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
21      */
22     public Union() { this(null, false); }
23     public Union(String shortForm) { this(shortForm, false); }
24     public Union(String shortForm, boolean synthetic) {
25         this.shortForm = shortForm;
26         this.synthetic = synthetic;
27     }
28
29     final String shortForm;
30     final boolean synthetic;
31     private final List<Sequence> alternatives = new ArrayList<Sequence>();
32
33     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
34     public boolean contains(Sequence s) { return alternatives.contains(s); }
35
36     /** adds an alternative */
37     public void add(Sequence s) {
38         alternatives.add(s);
39
40         // FIXME: does this make sense?
41         for(Sequence n : s.needs) add(n);
42         for(Sequence n : s.hates) add(n);
43     }
44
45     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
46
47     // FIXME
48     public static Union epsilon = new Union("()");
49     static { epsilon.add(Sequence.empty); }
50
51     // FIXME
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             // FIXME FIXME FIXME
58             if (new Walk.Cache().possiblyEpsilon(s))
59                 epsilonForm.merge(s.epsilonForm());
60         }
61         return epsilonForm;
62     }
63
64     // Display //////////////////////////////////////////////////////////////////////////////
65
66     public String getName() {
67         if (shortForm != null) return shortForm;
68         return "(anon_union)";
69     }
70     public String toString() {
71         if (shortForm != null) return shortForm;
72         StringBuffer sb = new StringBuffer();
73         sb.append("(");
74         bodyToString(sb, "", " | ");
75         sb.append(")");
76         return sb.toString();
77     }
78     public StringBuffer toString(StringBuffer sb) {
79         if (synthetic) return sb;
80         boolean first = true;
81         String before = StringUtil.pad(15, getName()) + " = ";
82         if (alternatives.size()==0) {
83             sb.append(before);
84         } else {
85             bodyToString(sb,
86                          before,
87                          "\n" + StringUtil.pad(15, "")        + " | ");
88             sb.append('\n');
89         }
90         return sb;
91     }
92     
93     private void bodyToString(StringBuffer sb, String before, String between) {
94         boolean first = true;
95         for(Sequence s : this) {
96             if (s.lame) continue;
97             // FIXME: what to do here about printing out negated sequences?
98             sb.append(first ? before : between);
99             first = false;
100             sb.append(s.toString());
101         }
102     }
103
104 }