1 // Copyright 2006-2007 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, s, false); }
30 public Union(String name, Sequence s, boolean synthetic) { this(name, synthetic); add(s); }
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 name).
38 * @param shortForm the "short form" display; for display purposes only
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 name, boolean synthetic) {
43 this.synthetic = synthetic;
46 public boolean contains(Sequence s) {
48 return alternatives.contains(s);
51 /** iterator over this Union's Sequences */
52 public Iterator<Sequence> iterator() {
54 return alternatives.iterator();
57 /** adds an alternative */
58 public Union add(Sequence s) {
60 throw new RuntimeException("once Union.contains() or Union.iterator() has been invoked, "+
61 "you may not add any more Sequences to it\n "+
62 " union in question: " + this);
63 if (s.needed_or_hated)
64 throw new RuntimeException("you may not add a conjunct directly to a Union");
66 if (alternatives.contains(s)) return this;
71 /** adds a one-element sequence */
72 public void add(Element e) {
73 add(Sequence.create(e));
76 /** the Forest which results from matching this Union against the empty string at region <tt>region</tt> */
77 Forest epsilonForm(Input.Region region, Grammar cache) {
79 Forest.Many epsilonForm = new Forest.Many();
80 for(Sequence s : this)
81 if (cache.possiblyEpsilon(s))
82 epsilonForm.merge(s.epsilonForm(region, cache));
87 // Display //////////////////////////////////////////////////////////////////////////////
89 boolean isSynthetic() { return synthetic; }
90 String getName() { return name==null ? "(anon_union)" : name; }
92 public String toString() {
93 // technically this should be turned on, but we don't make a big deal
95 if (name != null) return name;
96 StringBuffer sb = new StringBuffer();
98 bodyToString(sb, "", " | ");
100 return sb.toString();
103 /** display this union in long/expanded form */
104 public StringBuffer toString(StringBuffer sb) {
105 // technically this should be turned on, but we don't make a big deal
107 if (synthetic) return sb;
108 boolean first = true;
109 String before = StringUtil.pad(15, getName()) + " = ";
110 if (alternatives.size()==0) {
115 "\n" + StringUtil.pad(15, "") + " | ");
121 private void bodyToString(StringBuffer sb, String before, String between) {
123 boolean first = true;
124 for(Sequence s : this) {
125 // FIXME: what to do here about printing out negated sequences?
126 sb.append(first ? before : between);
128 sb.append(s.toString());