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