1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.*;
9 import java.lang.reflect.*;
10 import java.lang.ref.*;
13 * <font color=green>an element which can produce one of several alternatives</font>.
16 * Unlike the other Elements, Union is not immutable once
17 * constructed. To simulate this desirable feature, it is immutable
18 * <i>once examined</i> by taking its iterator or calling contains().
20 public class Union extends Element implements Iterable<Sequence> {
22 private final String name;
23 private final boolean synthetic;
24 private boolean viewed = false;
26 private final List<Sequence> alternatives = new ArrayList<Sequence>();
28 public Union(String name) { this(name, false); }
29 public Union(String name, Sequence s) { this(name, false); add(s); }
32 * Since every cycle in a non-degenerate grammar contains at
33 * least one Union, every instance of this class must be able to
34 * display itself in both "long form" (list of the long forms of
35 * its alternatives) and "short form" (some name).
37 * @param shortForm the "short form" display; for display purposes only
38 * @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
40 public Union(String name, boolean synthetic) {
42 this.synthetic = synthetic;
45 public boolean contains(Sequence s) {
47 return alternatives.contains(s);
50 /** iterator over this Union's Sequences */
51 public Iterator<Sequence> iterator() {
53 return alternatives.iterator();
56 /** adds an alternative */
57 public Union add(Sequence s) {
59 throw new RuntimeException("once Union.contains() or Union.iterator() has been invoked, "+
60 "you may not add any more Sequences to it\n "+
61 " union in question: " + this);
62 if (s.needed_or_hated)
63 throw new RuntimeException("you may not add a conjunct directly to a Union");
65 if (alternatives.contains(s)) return this;
70 /** adds a one-element sequence */
71 public void add(Element e) {
72 add(Sequence.create(e));
76 // Epsilon Form //////////////////////////////////////////////////////////////////////////////
78 private Forest.Many epsilonForm = null;
79 Forest epsilonForm(Input.Region loc) {
80 // FIXME: this is pretty ugly...
82 if (epsilonForm != null) return epsilonForm;
83 Forest.Many epsilonForm = new Forest.Many();
84 Cache cache = new Cache(null, null);
85 for(Sequence s : this)
86 if (cache.possiblyEpsilon(s))
87 epsilonForm.merge(s.epsilonForm(loc));
92 // Display //////////////////////////////////////////////////////////////////////////////
94 boolean isSynthetic() { return synthetic; }
95 String getName() { return name==null ? "(anon_union)" : name; }
97 public String toString() {
98 // technically this should be turned on, but we don't make a big deal
100 if (name != null) return name;
101 StringBuffer sb = new StringBuffer();
103 bodyToString(sb, "", " | ");
105 return sb.toString();
108 /** display this union in long/expanded form */
109 public StringBuffer toString(StringBuffer sb) {
110 // technically this should be turned on, but we don't make a big deal
112 if (synthetic) return sb;
113 boolean first = true;
114 String before = StringUtil.pad(15, getName()) + " = ";
115 if (alternatives.size()==0) {
120 "\n" + StringUtil.pad(15, "") + " | ");
126 private void bodyToString(StringBuffer sb, String before, String between) {
128 boolean first = true;
129 for(Sequence s : this) {
130 // FIXME: what to do here about printing out negated sequences?
131 sb.append(first ? before : between);
133 sb.append(s.toString());