add maxLength argument to Input.showRegion()
[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, false); add(s); }
30
31     /**
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).
36      *
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
39      */
40     public Union(String name, boolean synthetic) {
41         this.name = name;
42         this.synthetic = synthetic;
43     }
44
45     public boolean contains(Sequence s) {
46         viewed = true;
47         return alternatives.contains(s);
48     }
49
50     /** iterator over this Union's Sequences */
51     public Iterator<Sequence> iterator() {
52         viewed = true;
53         return alternatives.iterator();
54     }
55
56     /** adds an alternative */
57     public Union add(Sequence s) {
58         if (viewed)
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");
64         s.in_a_union = true;
65         if (alternatives.contains(s)) return this;
66         alternatives.add(s);
67         return this;
68     }
69
70     /** adds a one-element sequence */
71     public void add(Element e) {
72         add(Sequence.create(e));
73     }
74
75
76     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
77
78     private Forest.Many epsilonForm = null;
79     Forest epsilonForm(Input.Region loc) {
80         // FIXME: this is pretty ugly...
81         viewed = true;
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));
88         return epsilonForm;
89     }
90
91
92     // Display //////////////////////////////////////////////////////////////////////////////
93
94     boolean isSynthetic() { return synthetic; }
95     String getName()      { return name==null ? "(anon_union)" : name; }
96
97     public String toString() {
98         // technically this should be turned on, but we don't make a big deal
99         //viewed = true;
100         if (name != null) return name;
101         StringBuffer sb = new StringBuffer();
102         sb.append("(");
103         bodyToString(sb, "", " | ");
104         sb.append(")");
105         return sb.toString();
106     }
107
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
111         //viewed = true;
112         if (synthetic) return sb;
113         boolean first = true;
114         String before = StringUtil.pad(15, getName()) + " = ";
115         if (alternatives.size()==0) {
116             sb.append(before);
117         } else {
118             bodyToString(sb,
119                          before,
120                          "\n" + StringUtil.pad(15, "")        + " | ");
121             sb.append('\n');
122         }
123         return sb;
124     }
125     
126     private void bodyToString(StringBuffer sb, String before, String between) {
127         viewed = true;
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);
132             first = false;
133             sb.append(s.toString());
134         }
135     }
136
137 }