1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.*;
9 import java.lang.reflect.*;
10 import java.lang.ref.*;
12 /** <font color=green>juxtaposition; zero or more adjacent Elements; can specify a rewriting</font> */
13 public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
15 protected final Element[] elements;
17 boolean needed_or_hated = false;
18 boolean in_a_union = false;
20 final HashSet<Sequence> needs = new HashSet<Sequence>();
21 final HashSet<Sequence> hates = new HashSet<Sequence>();
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>();
27 final Position firstp;
31 private static int global_sernum = 0;
32 private int sernum = global_sernum++;
34 int[] ret = new int[needs.size()];
36 for(Sequence s : needs) ret[i++] = s.sernum;
40 int[] ret = new int[hates.size()];
42 for(Sequence s : hates) ret[i++] = s.sernum;
47 // Static Constructors //////////////////////////////////////////////////////////////////////////////
49 /** create a sequence of one element */
50 public static Sequence create(Element e) { return create(new Element[] { e }, 0); }
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); }
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)); }
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;
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
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);
81 /** return a new sequence identical to this one, but with a positive conjunct <tt>s</tt> */
82 public Sequence and(Sequence s) {
84 throw new RuntimeException("you may not use a sequence as a conjunct if it belongs to a Union");
87 s.needed_or_hated=true;
91 /** return a new sequence identical to this one, but with a negative conjunct <tt>s</tt> */
92 public Sequence andnot(Sequence s) {
94 throw new RuntimeException("you may not use a sequence as a conjunct if it belongs to a Union");
97 s.needed_or_hated=true;
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; }
104 ////////////////////////////////////////////////////////////////////////////////
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); }
115 Iterable<Sequence> needs() { return needs; }
116 Iterable<Sequence> hates() { return hates; }
118 Pos firstp() { return firstp; }
119 Pos lastp() { return firstp().last(); }
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(this, 0, null);
130 abstract Forest epsilonForm(Input.Region loc);
132 protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
135 // Position //////////////////////////////////////////////////////////////////////////////
137 static abstract class Pos implements IntegerMappable, Comparable<Pos>, Serializable {
140 private transient Sequence owner;
142 public int ord() { return ord; }
144 final Forest[] holder;
146 Pos(int len, Sequence owner) {
148 this.holder = new Forest[len];
151 public abstract int provides();
152 public abstract int[] needs();
153 public abstract int[] hates();
154 public abstract boolean owner_needed_or_hated();
156 public abstract boolean isFirst();
157 public abstract boolean isLast();
158 public abstract Pos last();
159 public abstract Pos prev();
160 public abstract Pos next();
162 /** the element which produces the sequence to which this Position belongs */
163 public Sequence owner() { return owner; }
165 abstract Element element();
167 public abstract int numPops();
168 public abstract <T> Forest<T> rewrite(Input.Region loc);
171 /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
172 private static class Position extends Pos implements IntegerMappable {
174 public Pos getPos() {
175 return new DumbPos(elements.length, provides(), needs(), hates(), owner_needed_or_hated(), numPops(),
176 public int provides();
177 public int[] needs();
178 public int[] hates();
179 public boolean owner_needed_or_hated();
180 public int numPops();
181 public <T> Forest<T> rewrite(Input.Region loc)
185 public int numPops() { return pos; }
188 private final Position next;
189 private final Position prev;
191 public int provides() { return owner().sernum; }
192 public int[] needs() { return owner().needs_int(); }
193 public int[] hates() { return owner().hates_int(); }
194 public boolean owner_needed_or_hated() { return owner().needed_or_hated; }
196 private Position(Sequence owner, int pos, Position prev) {
197 super(owner.elements.length,owner);
199 this.next = pos==owner.elements.length ? null : new Position(owner, pos+1, this);
203 public int compareTo(Pos p) {
204 return ord - ((Position)p).ord;
207 public boolean isFirst() { return pos==0; }
208 public int pos() { return pos; }
210 /** the element immediately after this Position, or null if this is the last Position */
211 public Element element() { return pos>=owner().elements.length ? null : owner().elements[pos]; }
213 /** the next Position (the Position after <tt>this.element()</tt>) */
214 public Position next() { return next; }
216 /** true iff this Position is the last one in the sequence */
217 public boolean isLast() { return next()==null; }
218 public Position last() { return isLast() ? this : next().last(); }
219 public Position prev() { return prev; }
221 // Position /////////////////////////////////////////////////////////////////////////////////
223 public final <T> Forest<T> rewrite(Input.Region loc) {
224 if (isFirst()) owner().epsilonForm(loc);
225 for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
226 for(int i=pos; i<owner().elements.length; i++) {
227 if (holder[i]==null) holder[i] = ((Union)owner().elements[i]).epsilonForm(loc);
228 if (holder[i]==null) throw new Error("bad");
230 return owner().postReduce(loc, holder, this);
233 public String toString() {
234 StringBuffer ret = new StringBuffer();
236 for(Position p = (Position)owner().firstp(); p != null; p = p.next()) {
238 if (p==this) ret.append(" | ");
239 if (p.element()!=null) ret.append(p.element());
240 else ret.append(' ');
243 return ret.toString();
245 private final int idx = master_position_idx++;
246 public int toInt() { return idx; }
248 private static int master_position_idx = 0;
251 // toString //////////////////////////////////////////////////////////////////////////////
253 public String toString() { return toString(new StringBuffer(), false).toString(); }
254 StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
255 StringBuffer toString(StringBuffer sb, boolean spacing) {
256 for(int i=0; i<elements.length; i++) {
257 sb.append(elements[i]+"");
260 if (follow != null) {
264 for(Sequence s : needs) {
268 for(Sequence s : hates) {
275 // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
277 static class Singleton extends Sequence {
278 private final int idx;
279 public Singleton(Element e) { this(new Element[] { e }, 0); }
280 public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
281 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) { return args[idx]; }
282 Sequence _clone() { return new Singleton(elements,idx); }
283 Forest epsilonForm(Input.Region loc) {
284 return ((Union)elements[idx]).epsilonForm(loc);
288 static class RewritingSequence extends Sequence {
289 private final Object tag;
290 private final boolean[] drops;
291 private final boolean[] lifts;
292 Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
293 public RewritingSequence(Object tag, Element[] e) { this(tag, e, null); }
294 public RewritingSequence(Object tag, Element[] e, boolean[] drops) { this(tag, e, drops, new boolean[e.length]); }
295 public RewritingSequence(Object tag, Element[] e, boolean[] drops, boolean[] lifts) {
297 if (tag==null) throw new Error();
299 this.drops = drops == null ? new boolean[e.length] : drops;
301 for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
302 this.lifts = new boolean[count];
304 for(int i=0; i<this.drops.length; i++)
306 this.lifts[j++] = lifts[i];
308 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
309 Forest<T>[] args2 = new Forest[lifts.length];
311 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
312 return Forest.create(loc, (T)tag, args2, lifts);
314 public StringBuffer toString(StringBuffer sb, boolean spacing) {
315 int len = sb.length();
317 sb.append("\""+StringUtil.escapify(tag.toString(),"\"\r\n")+"\":: ");
318 super.toString(sb, spacing);
319 len = sb.length()-len;
320 if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');
323 Forest epsilonForm(Input.Region loc) {
324 return Forest.create(loc, tag, new Forest[0], lifts);