added Element.toAtom()
[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     final String shortForm;
14     final boolean synthetic;
15     private final List<Sequence> alternatives = new ArrayList<Sequence>();
16
17     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
18
19     void reachable(HashSet<Sequence.Position> h) { for(Sequence s : alternatives) s.reachable(h); }
20
21     Topology toAtom() {
22         if (alternatives.size()==0) return null;
23         Topology ret = null;
24         for(Sequence s : this) {
25             Topology a = s.toAtom();
26             if (ret==null) ret = a.dup();
27             else           ret.add(a.dup());
28         }
29         return ret;
30     }
31
32     /** adds an alternative */
33     public void add(Sequence s) { alternatives.add(s); }
34
35     /**
36      *  Since every cycle in a non-degenerate grammar contains at
37      *  least one Union, every instance of this class must be able to
38      *  display itself in both "long form" (list of the long forms of
39      *  its alternatives) and "short form" (some abbreviation).
40      *
41      *  @param shortForm the "short form" display; usually 
42      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
43      */
44     public Union(String shortForm) { this(shortForm, false); }
45     public Union(String shortForm, boolean synthetic) {
46         this.shortForm = shortForm;
47         this.synthetic = synthetic;
48     }
49
50     public static Union epsilon = new Union("()");
51     static { epsilon.add(Sequence.empty); }
52
53     private Forest.Ref epsilonForm = null;
54     Forest epsilonForm() {
55         if (epsilonForm != null) return epsilonForm;
56         epsilonForm = new Forest.Ref();
57         for(Sequence s : this)
58             if (s.possiblyEpsilon(null))
59                 epsilonForm.merge(s.epsilonForm());
60         return epsilonForm;
61     }
62
63     // Display //////////////////////////////////////////////////////////////////////////////
64
65     public String toString() { return shortForm; }
66     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
67     public void toString(StringBuffer sb) {
68         if (synthetic) return;
69         boolean first = true;
70         if (alternatives.size()==0) {
71             sb.append(pad(15, shortForm) + "::= ");
72         } else for(Sequence s : this) {
73             sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : "  | "));
74             first = false;
75             sb.append(s.toString());
76             sb.append('\n');
77         }
78         sb.append('\n');
79     }
80
81     // SubUnion //////////////////////////////////////////////////////////////////////////////
82
83     /** FIXME this is kind of a hack */
84     public class Subset extends Union {
85         private final Set<Sequence> exclude;
86         public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
87         public Iterator<Sequence> iterator() {
88             final Iterator<Sequence> it = Union.this.iterator();
89             return new Iterator<Sequence>() {
90                 private Sequence next = it.hasNext() ? it.next() : null;
91                 public boolean hasNext() { return next != null; }
92                 public Sequence next() {
93                     Sequence ret = next;
94                     do {
95                         next = it.hasNext() ? it.next() : null;
96                     } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
97                     return ret;
98                 }
99                 public void remove() { throw new Error(); }
100             };
101         }
102     }
103
104 }