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); }
31 * Since every cycle in a non-degenerate grammar contains at
32 * least one Union, every instance of this class must be able to
33 * display itself in both "long form" (list of the long forms of
34 * its alternatives) and "short form" (some name).
36 * @param shortForm the "short form" display; for display purposes only
37 * @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
39 public Union(String name, boolean synthetic) {
41 this.synthetic = synthetic;
44 public boolean contains(Sequence s) {
46 return alternatives.contains(s);
49 /** iterator over this Union's Sequences */
50 public Iterator<Sequence> iterator() {
52 return alternatives.iterator();
55 /** adds an alternative */
56 public void add(Sequence s) {
60 throw new RuntimeException("attempt to add a Sequence to a Union that has already been examined:\n "+this);
62 if (alternatives.contains(s)) return;
66 /** adds a one-element sequence */
67 public void add(Element e) {
68 add(Sequence.create(e));
72 // Epsilon Form //////////////////////////////////////////////////////////////////////////////
75 //private Forest.Many epsilonForm = null;
76 Forest epsilonForm(Input.Region loc) {
77 //if (epsilonForm != null) return epsilonForm;
78 Forest.Many epsilonForm = new Forest.Many();
79 for(Sequence s : this) {
81 if (new Walk.Cache().possiblyEpsilon(s))
82 epsilonForm.merge(s.epsilonForm(loc));
88 // Display //////////////////////////////////////////////////////////////////////////////
90 public String getName() {
91 if (name != null) return name;
92 return "(anon_union)";
94 public String toString() {
96 if (name != null) return name;
97 StringBuffer sb = new StringBuffer();
99 bodyToString(sb, "", " | ");
101 return sb.toString();
104 /** display this union in long/expanded form */
105 public StringBuffer toString(StringBuffer sb) {
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());