70a3e45753a8e4912a2edf343a8131922d5cd592
[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(String shortForm) { this(shortForm, false); }
23     public Union(String shortForm, boolean synthetic) {
24         this.shortForm = shortForm;
25         this.synthetic = synthetic;
26     }
27
28     /** display form for the Union (ie not including the RHS) */
29     final String shortForm;
30
31     /** this is just a hint to use when printing out the grammar in visual form */
32     final boolean synthetic;
33
34     /** the actual alternatives */
35     private final List<Sequence> alternatives = new ArrayList<Sequence>();
36
37     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
38     public boolean contains(Sequence s) { return alternatives.contains(s); }
39
40     /** adds an alternative */
41     public void add(Sequence s) {
42         alternatives.add(s);
43
44         // FIXME: does this make sense?
45         for(Sequence n : s.needs) add(n);
46         for(Sequence n : s.hates) add(n);
47     }
48
49     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
50
51     // FIXME
52     public static Union epsilon = new Union("()");
53     static { epsilon.add(Sequence.empty); }
54
55     // FIXME
56     private Forest.Ref epsilonForm = null;
57     Forest epsilonForm() {
58         if (epsilonForm != null) return epsilonForm;
59         epsilonForm = new Forest.Ref();
60         for(Sequence s : this) {
61             // FIXME FIXME FIXME
62             if (new Walk.Cache().possiblyEpsilon(s))
63                 epsilonForm.merge(s.epsilonForm());
64         }
65         return epsilonForm;
66     }
67
68     // Display //////////////////////////////////////////////////////////////////////////////
69
70     public String toString() { return shortForm; }
71     public StringBuffer toString(StringBuffer sb) {
72         if (synthetic) return sb;
73         boolean first = true;
74         if (alternatives.size()==0) {
75             sb.append(StringUtil.pad(15, shortForm) + " = ");
76         } else for(Sequence s : this) {
77             sb.append(StringUtil.pad(15, first ? shortForm : "") + (first ? " = " : "  | "));
78             first = false;
79             sb.append(s.toString());
80             sb.append('\n');
81         }
82         sb.append('\n');
83         return sb;
84     }
85
86 }