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 /** display form for the Union (ie not including the RHS) */
14 final String shortForm;
16 /** this is just a hint to use when printing out the grammar in visual form */
17 final boolean synthetic;
19 /** the actual alternatives */
20 private final List<Sequence> alternatives = new ArrayList<Sequence>();
22 public Iterator<Sequence> iterator() { return alternatives.iterator(); }
23 public boolean contains(Sequence s) { return alternatives.contains(s); }
25 /** adds an alternative */
26 public void add(Sequence s) {
28 for(Sequence n : s.needs) add(n);
29 for(Sequence n : s.hates) add(n);
33 * Since every cycle in a non-degenerate grammar contains at
34 * least one Union, every instance of this class must be able to
35 * display itself in both "long form" (list of the long forms of
36 * its alternatives) and "short form" (some abbreviation).
38 * @param shortForm the "short form" display; usually
39 * @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
41 public Union(String shortForm) { this(shortForm, false); }
42 public Union(String shortForm, boolean synthetic) {
43 this.shortForm = shortForm;
44 this.synthetic = synthetic;
47 public static Union epsilon = new Union("()");
48 static { epsilon.add(Sequence.empty); }
50 private Forest.Ref epsilonForm = null;
51 Forest epsilonForm() {
52 if (epsilonForm != null) return epsilonForm;
53 epsilonForm = new Forest.Ref();
54 for(Sequence s : this)
55 if (s.possiblyEpsilon(null))
56 epsilonForm.merge(s.epsilonForm());
60 // Display //////////////////////////////////////////////////////////////////////////////
62 public String toString() { return shortForm; }
63 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
64 public void toString(StringBuffer sb) {
65 if (synthetic) return;
67 if (alternatives.size()==0) {
68 sb.append(pad(15, shortForm) + "::= ");
69 } else for(Sequence s : this) {
70 sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : " | "));
72 sb.append(s.toString());
78 // SubUnion //////////////////////////////////////////////////////////////////////////////
80 /** FIXME this is kind of a hack */
81 public class Subset extends Union {
82 private final Set<Sequence> exclude;
83 public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
84 public Iterator<Sequence> iterator() {
85 final Iterator<Sequence> it = Union.this.iterator();
86 return new Iterator<Sequence>() {
87 private Sequence next = it.hasNext() ? it.next() : null;
88 public boolean hasNext() { return next != null; }
89 public Sequence next() {
92 next = it.hasNext() ? it.next() : null;
93 } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
96 public void remove() { throw new Error(); }