checkpoint
[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 /**
11  *  <font color=green>an element which can produce one of several alternatives</font>.
12  *  <p>
13  *
14  *  Unlike the other Elements, Union is not immutable once
15  *  constructed.  To simulate this desirable feature, it is immutable
16  *  <i>once examined</i> by taking its iterator or calling contains().
17  */
18 public class Union extends Element implements Iterable<Sequence> {
19
20     private final String name;
21     private final boolean synthetic;
22     private boolean viewed = false;
23
24     private final List<Sequence> alternatives = new ArrayList<Sequence>();
25
26     public Union(String name) { this(name, false); }
27
28     /**
29      *  Since every cycle in a non-degenerate grammar contains at
30      *  least one Union, every instance of this class must be able to
31      *  display itself in both "long form" (list of the long forms of
32      *  its alternatives) and "short form" (some name).
33      *
34      *  @param shortForm the "short form" display; for display purposes only
35      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
36      */
37     public Union(String name, boolean synthetic) {
38         this.name = name;
39         this.synthetic = synthetic;
40     }
41
42     public boolean contains(Sequence s) {
43         viewed = true;
44         return alternatives.contains(s);
45     }
46
47     public Iterator<Sequence> iterator() {
48         viewed = true;
49         return alternatives.iterator();
50     }
51
52     /** adds an alternative */
53     public void add(Sequence s) {
54         if (viewed)
55             throw new RuntimeException("attempt to add a Sequence to a Union that has already been examined");
56         if (alternatives.contains(s)) return;
57         alternatives.add(s);
58     }
59
60
61     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
62
63     // FIXME
64     private Forest.Many epsilonForm = null;
65     Forest epsilonForm() {
66         if (epsilonForm != null) return epsilonForm;
67         epsilonForm = new Forest.Many();
68         for(Sequence s : this) {
69             // FIXME FIXME FIXME
70             if (new Walk.Cache().possiblyEpsilon(s))
71                 epsilonForm.merge(s.epsilonForm());
72         }
73         return epsilonForm;
74     }
75
76
77     // Display //////////////////////////////////////////////////////////////////////////////
78
79     public String getName() {
80         if (name != null) return name;
81         return "(anon_union)";
82     }
83     public String toString() {
84         viewed = true;
85         if (name != null) return name;
86         StringBuffer sb = new StringBuffer();
87         sb.append("(");
88         bodyToString(sb, "", " | ");
89         sb.append(")");
90         return sb.toString();
91     }
92
93     /** display this union in long/expanded form */
94     public StringBuffer toString(StringBuffer sb) {
95         viewed = true;
96         if (synthetic) return sb;
97         boolean first = true;
98         String before = StringUtil.pad(15, getName()) + " = ";
99         if (alternatives.size()==0) {
100             sb.append(before);
101         } else {
102             bodyToString(sb,
103                          before,
104                          "\n" + StringUtil.pad(15, "")        + " | ");
105             sb.append('\n');
106         }
107         return sb;
108     }
109     
110     private void bodyToString(StringBuffer sb, String before, String between) {
111         viewed = true;
112         boolean first = true;
113         for(Sequence s : this) {
114             // FIXME: what to do here about printing out negated sequences?
115             sb.append(first ? before : between);
116             first = false;
117             sb.append(s.toString());
118         }
119     }
120
121 }