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