4de0bb4969c58512982b7977fda60c9c6eaeac05
[sbp.git] / src / edu / berkeley / sbp / Union.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
9
10 /** an element which can produce one of several alternatives */
11 public class Union extends Element implements Iterable<Sequence> {
12
13     /** display form for the Union (ie not including the RHS) */
14     final String shortForm;
15
16     /** this is just a hint to use when printing out the grammar in visual form */
17     final boolean synthetic;
18
19     /** the actual alternatives */
20     private final List<Sequence> alternatives = new ArrayList<Sequence>();
21
22     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
23     public boolean contains(Sequence s) { return alternatives.contains(s); }
24
25     /** adds an alternative */
26     public void add(Sequence s) {
27         alternatives.add(s);
28         for(Sequence n : s.needs) add(n);
29         for(Sequence n : s.hates) add(n);
30     }
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 abbreviation).
37      *
38      *  @param shortForm the "short form" display; usually 
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 shortForm) { this(shortForm, false); }
42     public Union(String shortForm, boolean synthetic) {
43         this.shortForm = shortForm;
44         this.synthetic = synthetic;
45     }
46
47     public static Union epsilon = new Union("()");
48     static { epsilon.add(Sequence.empty); }
49
50     private Forest.Ref epsilonForm = null;
51     Forest epsilonForm() {
52         if (epsilonForm != null) return epsilonForm;
53         epsilonForm = new Forest.Ref();
54         for(Sequence s : this)
55             if (s.possiblyEpsilon(null))
56                 epsilonForm.merge(s.epsilonForm());
57         return epsilonForm;
58     }
59
60     // Display //////////////////////////////////////////////////////////////////////////////
61
62     public String toString() { return shortForm; }
63     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
64     public void toString(StringBuffer sb) {
65         if (synthetic) return;
66         boolean first = true;
67         if (alternatives.size()==0) {
68             sb.append(pad(15, shortForm) + "::= ");
69         } else for(Sequence s : this) {
70             sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : "  | "));
71             first = false;
72             sb.append(s.toString());
73             sb.append('\n');
74         }
75         sb.append('\n');
76     }
77
78     // SubUnion //////////////////////////////////////////////////////////////////////////////
79
80     /** FIXME this is kind of a hack */
81     public class Subset extends Union {
82         private final Set<Sequence> exclude;
83         public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
84         public Iterator<Sequence> iterator() {
85             final Iterator<Sequence> it = Union.this.iterator();
86             return new Iterator<Sequence>() {
87                 private Sequence next = it.hasNext() ? it.next() : null;
88                 public boolean hasNext() { return next != null; }
89                 public Sequence next() {
90                     Sequence ret = next;
91                     do {
92                         next = it.hasNext() ? it.next() : null;
93                     } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
94                     return ret;
95                 }
96                 public void remove() { throw new Error(); }
97             };
98         }
99     }
100
101 }