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     private Forest.Ref epsilonForm = null;
40     Forest epsilonForm() {
41         if (epsilonForm != null) return epsilonForm;
42         epsilonForm = new Forest.Ref();
43         for(Sequence s : this)
44             if (s.possiblyEpsilon(null))
45                 epsilonForm.merge(s.epsilonForm());
46         return epsilonForm;
47     }
48
49     // Display //////////////////////////////////////////////////////////////////////////////
50
51     public String toString() { return shortForm; }
52     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
53     public void toString(StringBuffer sb) {
54         if (synthetic) return;
55         boolean first = true;
56         if (alternatives.size()==0) {
57             sb.append(pad(15, shortForm) + "::= ");
58         } else for(Sequence s : this) {
59             sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : "  | "));
60             first = false;
61             sb.append(s.toString());
62             sb.append('\n');
63         }
64         sb.append('\n');
65     }
66
67     // SubUnion //////////////////////////////////////////////////////////////////////////////
68
69     /** FIXME this is kind of a hack */
70     public class Subset extends Union {
71         private final Set<Sequence> exclude;
72         public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
73         public Iterator<Sequence> iterator() {
74             final Iterator<Sequence> it = Union.this.iterator();
75             return new Iterator<Sequence>() {
76                 private Sequence next = it.hasNext() ? it.next() : null;
77                 public boolean hasNext() { return next != null; }
78                 public Sequence next() {
79                     Sequence ret = next;
80                     do {
81                         next = it.hasNext() ? it.next() : null;
82                     } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
83                     return ret;
84                 }
85                 public void remove() { throw new Error(); }
86             };
87         }
88     }
89
90 }