1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
10 /** an element which can produce one of several alternatives */
11 public class Union extends Element implements Iterable<Sequence> {
13 final String shortForm;
14 final boolean synthetic;
15 private final List<Sequence> alternatives = new ArrayList<Sequence>();
17 public Iterator<Sequence> iterator() { return alternatives.iterator(); }
18 public boolean contains(Sequence s) { return alternatives.contains(s); }
21 if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions");
23 for(Sequence s : this) {
24 Topology a = s.toAtom();
25 if (ret==null) ret = a;
26 else ret = ret.union(a);
28 if (ret==null) throw new RuntimeException("confusion on " + this);
32 /** adds an alternative */
33 public void add(Sequence s) {
35 if (/*!synthetic &&*/ shortForm!=null
36 //&& Character.isUpperCase(shortForm.charAt(0))
38 s.setName(toString());
42 * Since every cycle in a non-degenerate grammar contains at
43 * least one Union, every instance of this class must be able to
44 * display itself in both "long form" (list of the long forms of
45 * its alternatives) and "short form" (some abbreviation).
47 * @param shortForm the "short form" display; usually
48 * @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
50 public Union(String shortForm) { this(shortForm, false); }
51 public Union(String shortForm, boolean synthetic) {
52 this.shortForm = shortForm;
53 this.synthetic = synthetic;
56 public static Union epsilon = new Union("()");
57 static { epsilon.add(Sequence.empty); }
59 private Forest.Ref epsilonForm = null;
60 Forest epsilonForm() {
61 if (epsilonForm != null) return epsilonForm;
62 epsilonForm = new Forest.Ref();
63 for(Sequence s : this)
64 if (s.possiblyEpsilon(null))
65 epsilonForm.merge(s.epsilonForm());
69 // Display //////////////////////////////////////////////////////////////////////////////
71 public String toString() { return shortForm; }
72 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
73 public void toString(StringBuffer sb) {
74 if (synthetic) return;
76 if (alternatives.size()==0) {
77 sb.append(pad(15, shortForm) + "::= ");
78 } else for(Sequence s : this) {
79 sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : " | "));
81 sb.append(s.toString());
87 // SubUnion //////////////////////////////////////////////////////////////////////////////
89 /** FIXME this is kind of a hack */
90 public class Subset extends Union {
91 private final Set<Sequence> exclude;
92 public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
93 public Iterator<Sequence> iterator() {
94 final Iterator<Sequence> it = Union.this.iterator();
95 return new Iterator<Sequence>() {
96 private Sequence next = it.hasNext() ? it.next() : null;
97 public boolean hasNext() { return next != null; }
98 public Sequence next() {
101 next = it.hasNext() ? it.next() : null;
102 } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
105 public void remove() { throw new Error(); }