fix major bug: create YieldSet2 which does not consider needs/hates
[sbp.git] / src / edu / berkeley / sbp / Union.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.*;
7 import java.io.*;
8 import java.util.*;
9 import java.lang.reflect.*;
10 import java.lang.ref.*;
11
12 /**
13  *  <font color=green>an element which can produce one of several alternatives</font>.
14  *  <p>
15  *
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().
19  */
20 public class Union extends Element implements Iterable<Sequence> {
21
22     /*private*/ final String name;
23     /*private*/ final boolean synthetic;
24     private boolean viewed = false;
25
26     private final List<Sequence> alternatives = new ArrayList<Sequence>();
27
28     public Union(String name) { this(name, false); }
29
30     /**
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).
35      *
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
38      */
39     public Union(String name, boolean synthetic) {
40         this.name = name;
41         this.synthetic = synthetic;
42     }
43
44     public boolean contains(Sequence s) {
45         viewed = true;
46         return alternatives.contains(s);
47     }
48
49     /** iterator over this Union's Sequences */
50     public Iterator<Sequence> iterator() {
51         viewed = true;
52         return alternatives.iterator();
53     }
54
55     /** adds an alternative */
56     public void add(Sequence s) {
57         /*
58           FIXME
59         if (viewed)
60             throw new RuntimeException("attempt to add a Sequence to a Union that has already been examined:\n  "+this);
61         */
62         if (alternatives.contains(s)) return;
63         alternatives.add(s);
64     }
65
66     /** adds a one-element sequence */
67     public void add(Element e) {
68         add(Sequence.create(e));
69     }
70
71
72     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
73
74     // FIXME
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) {
80             // FIXME FIXME FIXME
81             if (new Walk.Cache().possiblyEpsilon(s))
82                 epsilonForm.merge(s.epsilonForm(loc));
83         }
84         return epsilonForm;
85     }
86
87
88     // Display //////////////////////////////////////////////////////////////////////////////
89
90     public String getName() {
91         if (name != null) return name;
92         return "(anon_union)";
93     }
94     public String toString() {
95         viewed = true;
96         if (name != null) return name;
97         StringBuffer sb = new StringBuffer();
98         sb.append("(");
99         bodyToString(sb, "", " | ");
100         sb.append(")");
101         return sb.toString();
102     }
103
104     /** display this union in long/expanded form */
105     public StringBuffer toString(StringBuffer sb) {
106         viewed = true;
107         if (synthetic) return sb;
108         boolean first = true;
109         String before = StringUtil.pad(15, getName()) + " = ";
110         if (alternatives.size()==0) {
111             sb.append(before);
112         } else {
113             bodyToString(sb,
114                          before,
115                          "\n" + StringUtil.pad(15, "")        + " | ");
116             sb.append('\n');
117         }
118         return sb;
119     }
120     
121     private void bodyToString(StringBuffer sb, String before, String between) {
122         viewed = true;
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);
127             first = false;
128             sb.append(s.toString());
129         }
130     }
131
132 }