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