cleanups, reorg, and commenting
[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     public Union(String name, Sequence s) { this(name, s, false); }
30     public Union(String name, Sequence s, boolean synthetic) { this(name, synthetic); add(s); }
31
32     /**
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).
37      *
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
40      */
41     public Union(String name, boolean synthetic) {
42         this.name = name;
43         this.synthetic = synthetic;
44     }
45
46     public boolean contains(Sequence s) {
47         viewed = true;
48         return alternatives.contains(s);
49     }
50
51     /** iterator over this Union's Sequences */
52     public Iterator<Sequence> iterator() {
53         viewed = true;
54         return alternatives.iterator();
55     }
56
57     /** adds an alternative */
58     public Union add(Sequence s) {
59         if (viewed)
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");
65         s.in_a_union = true;
66         if (alternatives.contains(s)) return this;
67         alternatives.add(s);
68         return this;
69     }
70
71     /** adds a one-element sequence */
72     public void add(Element e) {
73         add(Sequence.create(e));
74     }
75
76     /** the Forest which results from matching this Union against the empty string at region <tt>region</tt> */
77     Forest epsilonForm(Input.Region region, Cache cache) {
78         viewed = true;
79         Forest.Many epsilonForm = new Forest.Many();
80         for(Sequence s : this)
81             if (cache.possiblyEpsilon(s))
82                 epsilonForm.merge(s.epsilonForm(region, cache));
83         return epsilonForm;
84     }
85
86
87     // Display //////////////////////////////////////////////////////////////////////////////
88
89     boolean isSynthetic() { return synthetic; }
90     String getName()      { return name==null ? "(anon_union)" : name; }
91
92     public String toString() {
93         // technically this should be turned on, but we don't make a big deal
94         //viewed = true;
95         if (name != null) return name;
96         StringBuffer sb = new StringBuffer();
97         sb.append("(");
98         bodyToString(sb, "", " | ");
99         sb.append(")");
100         return sb.toString();
101     }
102
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
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 }