94c1fc9653503356405b272cace0c3d6cd4820cd
[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 void add(Sequence s) {
58         /*
59           FIXME
60         if (viewed)
61             throw new RuntimeException("attempt to add a Sequence to a Union that has already been examined:\n  "+this);
62         */
63         if (alternatives.contains(s)) return;
64         alternatives.add(s);
65     }
66
67     /** adds a one-element sequence */
68     public void add(Element e) {
69         add(Sequence.create(e));
70     }
71
72
73     // Epsilon Form //////////////////////////////////////////////////////////////////////////////
74
75     // FIXME
76     //private Forest.Many epsilonForm = null;
77     Forest epsilonForm(Input.Region loc) {
78         //if (epsilonForm != null) return epsilonForm;
79         Forest.Many epsilonForm = new Forest.Many();
80         for(Sequence s : this) {
81             // FIXME FIXME FIXME
82             if (new Walk.Cache().possiblyEpsilon(s))
83                 epsilonForm.merge(s.epsilonForm(loc));
84         }
85         return epsilonForm;
86     }
87
88
89     // Display //////////////////////////////////////////////////////////////////////////////
90
91     boolean isSynthetic() { return synthetic; }
92     String getName()      { return name==null ? "(anon_union)" : name; }
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 }