79854c64b6b106ad2d4e972781a575ee552192ba
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
1 // Copyright 2006-2007 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 /** <font color=green>juxtaposition; zero or more adjacent Elements; can specify a rewriting</font> */
13 public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
14
15     protected final Element[] elements;
16
17     boolean needed_or_hated = false;
18     boolean in_a_union = false;
19
20     final HashSet<Sequence> needs  = new HashSet<Sequence>();
21     final HashSet<Sequence> hates  = new HashSet<Sequence>();
22
23     // FIXME: these are ugly -- migrate into Grammar
24     HashMap<Sequence,Boolean> canNeed = new HashMap<Sequence,Boolean>();
25     HashMap<Sequence,Boolean> canKill = new HashMap<Sequence,Boolean>();
26
27     final Position firstp;
28
29     Atom follow = null;
30
31     private static int global_sernum = 0;
32     private int sernum = global_sernum++;
33     int[] needs_int() {
34         int[] ret = new int[needs.size()];
35         int i = 0;
36         for(Sequence s : needs) ret[i++] = s.sernum;
37         return ret;
38     }
39     int[] hates_int() {
40         int[] ret = new int[hates.size()];
41         int i = 0;
42         for(Sequence s : hates) ret[i++] = s.sernum;
43         return ret;
44     }
45
46
47     // Static Constructors //////////////////////////////////////////////////////////////////////////////
48
49     /** create a sequence of one element */
50     public static Sequence create(Element e) { return create(new Element[] { e }, 0); }
51
52     /** create a sequence which drops the result of all but one of its element */
53     public static Sequence create(Element[] e, int which) {
54         return new Singleton(e, which); }
55
56     /** create a sequence which always evaluates to a constant result  */
57     public static Sequence create(Object result, Element[] e) {
58         return new RewritingSequence(result, e, trues(e.length)); }
59
60     private static boolean[] trues(int length) {
61         boolean[] ret = new boolean[length];
62         for(int i=0; i<ret.length; i++) ret[i] = true;
63         return ret;
64     }
65
66     /**
67      *  create a sequence (general form)
68      *  @param head   the head of the output tree
69      *  @param e      the elements to match
70      *  @param drop   only elements of <tt>e</tt> whose corresponding <tt>boolean</tt> in <tt>drops</tt>
71      *                is <i>false</i> will be included in the output tree
72      *  @param lifts  which (if any) child trees to lift
73      **/
74     public static Sequence create(Object head, Element[] e, boolean[] drop) {
75         return create(head, e, drop, new boolean[e.length]); }
76     public static Sequence create(Object head, Element[] e, boolean[] drop, boolean[] lifts) {
77         if (lifts==null) lifts = new boolean[e.length];
78         return new RewritingSequence(head, e, drop, lifts);
79     }
80
81     /** return a new sequence identical to this one, but with a positive conjunct <tt>s</tt> */
82     public Sequence and(Sequence s) {
83         if (s.in_a_union)
84             throw new RuntimeException("you may not use a sequence as a conjunct if it belongs to a Union");
85         Sequence ret = dup();
86         ret.needs.add(s);
87         s.needed_or_hated=true;
88         return ret;
89     }
90
91     /** return a new sequence identical to this one, but with a negative conjunct <tt>s</tt> */
92     public Sequence andnot(Sequence s) {
93         if (s.in_a_union)
94             throw new RuntimeException("you may not use a sequence as a conjunct if it belongs to a Union");
95         Sequence ret = dup();
96         ret.hates.add(s);
97         s.needed_or_hated=true;
98         return ret;
99     }
100
101     /** return a new sequence identical to this one, but with a follow-set restricted to <tt>a</tt> */
102     public Sequence followedBy(Atom a) { Sequence ret = dup(); ret.follow = a; return ret; }
103
104     ////////////////////////////////////////////////////////////////////////////////
105
106     abstract Sequence _clone();
107     private Sequence dup() {
108         Sequence ret = _clone();
109         for(Sequence s : needs) { ret.needs.add(s); }
110         for(Sequence s : hates) { ret.hates.add(s); }
111         ret.follow = follow;
112         return ret;
113     }
114
115     Iterable<Sequence> needs() { return needs; }
116     Iterable<Sequence> hates() { return hates; }
117
118     Position firstp() { return firstp; }
119     Position lastp() { return firstp().last(); }
120
121     public Iterator<Element> iterator()    { return new ArrayIterator<Element>(elements); }
122     protected Sequence(Element[] elements) {
123         this.elements = elements;
124         for(int i=0; i<elements.length; i++)
125             if (elements[i]==null)
126                 throw new RuntimeException("cannot have nulls in a sequence: " + this);
127         this.firstp = new Position(0, null);
128     }
129
130     abstract Forest epsilonForm(Input.Region loc, Grammar cache);
131
132     protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
133
134
135     // Position //////////////////////////////////////////////////////////////////////////////
136
137     static abstract class Pos implements IntegerMappable, Comparable<Pos>, Serializable {
138         final Forest[] holder;
139         Pos(int len) { this.holder = new Forest[len]; }
140
141         public abstract int   provides();
142         public abstract int[] needs();
143         public abstract int[] hates();
144         public abstract boolean            owner_needed_or_hated();
145
146         public abstract int numPops();
147         public abstract <T> Forest<T> rewrite(Input.Region loc, Grammar cache);
148     }
149
150     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
151     class Position extends Pos implements IntegerMappable {
152         /*
153         public Pos getPos() {
154             return new DumbPos(elements.length, provides(), needs(), hates(), owner_needed_or_hated(), numPops(), 
155                 public int   provides();
156                 public int[] needs();
157                 public int[] hates();
158                 public boolean            owner_needed_or_hated();
159                 public int numPops();
160                 public <T> Forest<T> rewrite(Input.Region loc, Grammar cache)
161             };
162         }
163         */
164         public int ord = -1;
165         public int ord()     { return ord; }
166         public int numPops() { return pos; }
167
168                 final int      pos;
169         private final Position next;
170         private final Position prev;
171
172         public int     provides() { return owner().sernum; }
173         public int[]   needs() { return owner().needs_int(); }
174         public int[]   hates() { return owner().hates_int(); }
175         public boolean owner_needed_or_hated() { return owner().needed_or_hated; }
176         
177         private Position(int pos, Position prev) {
178             super(elements.length);
179             this.pos      = pos;
180             this.next     = pos==elements.length ? null : new Position(pos+1, this);
181             this.prev     = prev;
182         }
183
184         public int compareTo(Pos p) {
185             return ord - ((Position)p).ord;
186         }
187
188         boolean isFirst() { return pos==0; }
189         public int pos() { return pos; }
190
191         /** the element immediately after this Position, or null if this is the last Position */
192         public Element  element() { return pos>=elements.length ? null : elements[pos]; }
193
194         /** the element which produces the sequence to which this Position belongs */
195         public Sequence owner() { return Sequence.this; }
196
197         /** the next Position (the Position after <tt>this.element()</tt>) */
198         public Position next() { return next; }
199
200         /** true iff this Position is the last one in the sequence */
201         public boolean isLast() { return next()==null; }
202         public Position last() { return isLast() ? this : next().last(); }
203         public Position prev() { return prev; }
204
205         // Position /////////////////////////////////////////////////////////////////////////////////
206
207         public final <T> Forest<T> rewrite(Input.Region loc, Grammar cache) {
208             if (this==firstp()) epsilonForm(loc, cache);
209             for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
210             for(int i=pos; i<elements.length; i++) {
211                 if (holder[i]==null) holder[i] = ((Union)elements[i]).epsilonForm(loc, cache);
212                 if (holder[i]==null) throw new Error("bad");
213             }
214             return Sequence.this.postReduce(loc, holder, this);
215         }
216
217         public String   toString() {
218             StringBuffer ret = new StringBuffer();
219             ret.append("<{");
220             for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
221                 ret.append(' ');
222                 if (p==this) ret.append(" | ");
223                 if (p.element()!=null) ret.append(p.element());
224                 else                   ret.append(' ');
225             }
226             ret.append("}>");
227             return ret.toString();
228         }
229         private final int idx = master_position_idx++;
230         public int toInt() { return idx; }
231     }
232     private static int master_position_idx = 0;
233
234
235     // toString //////////////////////////////////////////////////////////////////////////////
236
237     public String toString() { return toString(new StringBuffer(), false).toString(); }
238     StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
239     StringBuffer toString(StringBuffer sb, boolean spacing) {
240         for(int i=0; i<elements.length; i++) {
241             sb.append(elements[i]+"");
242             sb.append(' ');
243         }
244         if (follow != null) {
245             sb.append("-> ");
246             sb.append(follow);
247         }
248         for(Sequence s : needs) {
249             sb.append("& ");
250             sb.append(s);
251         }
252         for(Sequence s : hates) {
253             sb.append("&~ ");
254             sb.append(s);
255         }
256         return sb;
257     }
258
259
260     // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
261
262     static class Singleton extends Sequence {
263         private final int idx;
264         public Singleton(Element e) { this(new Element[] { e }, 0); }
265         public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
266         public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) { return args[idx]; }
267         Sequence _clone() { return new Singleton(elements,idx); }
268         Forest epsilonForm(Input.Region loc, Grammar cache) {
269             return ((Union)elements[idx]).epsilonForm(loc, cache);
270         }
271     }
272
273     static class RewritingSequence extends Sequence {
274         private final Object    tag;
275         private final boolean[] drops;
276         private final boolean[] lifts;
277         Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
278         public RewritingSequence(Object tag, Element[] e) { this(tag, e, null); }
279         public RewritingSequence(Object tag, Element[] e, boolean[] drops) { this(tag, e, drops, new boolean[e.length]); }
280         public RewritingSequence(Object tag, Element[] e, boolean[] drops, boolean[] lifts) {
281             super(e);
282             if (tag==null) throw new Error();
283             this.tag = tag;
284             this.drops = drops == null ? new boolean[e.length] : drops;
285             int count = 0;
286             for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
287             this.lifts = new boolean[count];
288             int j = 0;
289             for(int i=0; i<this.drops.length; i++)
290                 if (!this.drops[i])
291                     this.lifts[j++] = lifts[i];
292         }
293         public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
294             Forest<T>[] args2 = new Forest[lifts.length];
295             int j = 0;
296             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
297             return Forest.create(loc, (T)tag, args2, lifts);
298         }
299         public StringBuffer toString(StringBuffer sb, boolean spacing) {
300             int len = sb.length();
301             if (tag != null)
302                 sb.append("\""+StringUtil.escapify(tag.toString(),"\"\r\n")+"\":: ");
303             super.toString(sb, spacing);
304             len = sb.length()-len;
305             if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');
306             return sb;
307         }
308         Forest epsilonForm(Input.Region loc, Grammar cache) {
309             return Forest.create(loc, tag, new Forest[0], lifts);
310         }
311     }
312 }