d25260450ef90d7ae0ad6466d69edc00026ff283
[sbp.git] / src / edu / berkeley / sbp / Sequence.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 /** juxtaposition; zero or more adjacent Elements; can specify a rewriting */
11 public abstract class Sequence extends Element implements Iterable<Element> {
12
13     // Static Constructors //////////////////////////////////////////////////////////////////////////////
14
15     /** the empty sequence (matches the empty string) */
16     public static final Sequence empty = new Sequence.Constant.Empty();
17
18     /** after matching the sequence, do not add anything to the output tree */
19     public static Sequence drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) { return new Constant.Drop(e, and, not, lame); }
20
21     /** after matching the sequence, insert a constant into the output tree */
22     public static Sequence constant(Element[] e, Object o, HashSet<Sequence> and, HashSet<Sequence> not) { return new Constant(e, o, and, not); }
23
24     /** after matching the sequence, place the result of the <tt>idx</tt>th match in the output tree */
25     public static Sequence singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { return new Singleton(e, idx, and, not); }
26
27     /**
28      *  after matching the sequence, create the specified output tree
29      *  @param tag   the tag for the output tree
30      *  @param e     the elements to match
31      *  @param drops only elements of <tt>e</tt> whose corresponding <tt>boolean</tt> in <tt>drops</tt> is <i>false</i> will be included in the output tree
32      **/
33     public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
34         return new RewritingSequence(tag, e, drops, and, not); }
35
36     ////////////////////////////////////////////////////////////////////////////////
37
38     public Topology noFollow() { return null; }
39
40     Topology toAtom() {
41         if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
42         return elements[0].toAtom();
43     }
44
45     protected final Element[] elements;
46
47     final HashSet<Sequence> needs;
48     final HashSet<Sequence> hates;
49           boolean           lame  = false;
50
51     final Position          firstp;
52     Position firstp() { return firstp; }
53
54     public Iterator<Element> iterator()    { return new ArrayIterator<Element>(elements); }
55     protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
56         this.needs = and==null ? new HashSet<Sequence>() : and;
57         this.hates = not==null ? new HashSet<Sequence>() : not;
58         //for(Sequence s : needs) s.lame = true;
59         //for(Sequence s : hates) s.lame = true;
60         this.elements = elements;
61         this.firstp = new Position(0);
62     }
63
64     void reachable(HashSet<Position> h) { firstp().reachable(h); }
65
66     Forest epsilonForm() { return firstp().rewrite(null); }
67
68     protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
69
70
71     // Position //////////////////////////////////////////////////////////////////////////////
72
73     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
74     public class Position {
75
76         void reachable(HashSet<Position> h) {
77             if (h.contains(this)) return;
78             h.add(this);
79             if (element() != null) element().reachable(h);
80         }
81
82                 final int      pos;
83         private final Position next;
84                 final Forest[] holder;
85         
86         private Position(int pos) {
87             this.pos      = pos;
88             this.next     = pos==elements.length ? null : new Position(pos+1);
89             this.holder   = new Forest[elements.length];
90         }
91
92         boolean isFirst() { return pos==0; }
93         boolean isRightNullable(Walk.Cache cache) {
94             if (isLast()) return true;
95             if (!element().possiblyEpsilon(cache)) return false;
96             return next().isRightNullable(cache);
97         }
98
99         /** the element immediately after this Position, or null if this is the last Position */
100         public Element  element() { return pos>=elements.length ? null : elements[pos]; }
101
102         /** the element which produces the sequence to which this Position belongs */
103         public Sequence owner() { return Sequence.this; }
104
105         /** the next Position (the Position after <tt>this.element()</tt>) */
106         public Position next() { return next; }
107
108         /** true iff this Position is the last one in the sequence */
109         public boolean isLast() { return next()==null; }
110
111         // Reduction /////////////////////////////////////////////////////////////////////////////////
112
113         <T> Forest<T> rewrite(Token.Location loc) {
114             for(int i=pos; i<elements.length; i++) if (holder[i]==null) holder[i] = elements[i].epsilonForm();
115             Forest<T> ret = Sequence.this.postReduce(loc, holder);
116             for(int k=0; k<pos; k++) holder[k] = null; // to help GC
117             return ret;
118         }
119
120         public String   toString() {
121             StringBuffer ret = new StringBuffer();
122             ret.append("<{");
123             for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
124                 ret.append(' ');
125                 if (p==this) ret.append(" | ");
126                 if (p.element()!=null) ret.append(p.element().possiblyEpsilon(null) ? "["+p.element()+"]" : p.element());
127                 else                   ret.append(' ');
128             }
129             ret.append("}>");
130             return ret.toString();
131         }
132     }
133
134
135     // toString //////////////////////////////////////////////////////////////////////////////
136
137     public String toString() { return toString(new StringBuffer(), false).toString(); }
138     StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
139     StringBuffer toString(StringBuffer sb, boolean spacing) {
140         for(int i=0; i<elements.length; i++) {
141             sb.append(elements[i].toString());
142             sb.append(' ');
143         }
144         return sb;
145     }
146
147
148     // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
149
150     static class Constant extends Sequence {
151         private final Object result;
152         public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
153         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
154             return (Forest<T>)Forest.leaf(loc, result, this);
155         }
156         static class Drop extends Constant {
157             public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
158                 super(e, null, and, not);
159                 this.lame = lame;
160             }
161         }
162         static class Empty extends Sequence.Constant.Drop { public Empty() { super(new Element[] { }, null, null, false); } }
163     }
164
165     static class Singleton extends Sequence {
166         private final int idx;
167         public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
168         public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
169         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
170     }
171
172     static class Unwrap extends Sequence {
173         private boolean[] drops;
174         public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not)                  { super(e, and, not); this.drops = null; }
175         public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
176         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
177             if (drops==null) return Forest.create(loc, null, args, this, true, false);
178             int count = 0;
179             for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
180             Forest<T>[] args2 = new Forest[count];
181             int j = 0;
182             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
183             return Forest.create(loc, null, args2, this, true, false);            
184         }
185     }
186
187     static class RewritingSequence extends Sequence {
188         private final Object tag;
189         private final boolean[] drops;
190         private int count = 0;
191         public RewritingSequence(Object tag, Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { this(tag, e, null, and, not); }
192         public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
193             super(e, and, not);
194             this.tag = tag;
195             this.drops = drops == null ? new boolean[e.length] : drops;
196             for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
197         }
198         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
199             Forest<T>[] args2 = new Forest[count];
200             int j = 0;
201             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
202             return Forest.create(loc, (T)tag, args2, this, false, false);
203         }
204         public StringBuffer toString(StringBuffer sb, boolean spacing) {
205             int len = sb.length();
206             super.toString(sb, spacing);
207             len = sb.length()-len;
208             if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');
209             sb.append(" => ");
210             sb.append(tag);
211             return sb;
212         }
213     }
214 }