1 // Copyright 2006 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 Cache
24 HashMap<Sequence,Boolean> canNeed = new HashMap<Sequence,Boolean>();
25 HashMap<Sequence,Boolean> canKill = new HashMap<Sequence,Boolean>();
27 final Position firstp;
31 // Static Constructors //////////////////////////////////////////////////////////////////////////////
33 /** create a sequence of one element */
34 public static Sequence create(Element e) { return create(new Element[] { e }, 0); }
36 /** create a sequence which drops the result of all but one of its element */
37 public static Sequence create(Element[] e, int which) { return new Singleton(e, which); }
39 /** create a sequence which always evaluates to a constant result */
40 public static Sequence create(Element[] e, Object result) { return new Constant(e, result); }
43 * create a sequence (general form)
44 * @param head the head of the output tree
45 * @param e the elements to match
46 * @param drop only elements of <tt>e</tt> whose corresponding <tt>boolean</tt> in <tt>drops</tt>
47 * is <i>false</i> will be included in the output tree
48 * @param foster if true, all children of the last child (ie
49 * grandchildren) are promoted to children of this
50 * node; this is very useful for matching repetitions
52 public static Sequence create(Object head, Element[] e, boolean[] drop, boolean foster) {
54 ? new Unwrap(e, head, drop)
55 : new RewritingSequence(head, e, drop);
58 /** return a new sequence identical to this one, but with a positive conjunct <tt>s</tt> */
59 public Sequence and(Sequence s) {
61 throw new RuntimeException("you may not use a sequence as a conjunct if it belongs to a Union");
64 s.needed_or_hated=true;
68 /** return a new sequence identical to this one, but with a negative conjunct <tt>s</tt> */
69 public Sequence andnot(Sequence s) {
71 throw new RuntimeException("you may not use a sequence as a conjunct if it belongs to a Union");
74 s.needed_or_hated=true;
78 /** return a new sequence identical to this one, but with a follow-set restricted to <tt>a</tt> */
79 public Sequence followedBy(Atom a) { Sequence ret = dup(); ret.follow = a; return ret; }
81 ////////////////////////////////////////////////////////////////////////////////
83 abstract Sequence _clone();
84 private Sequence dup() {
85 Sequence ret = _clone();
86 for(Sequence s : needs) { ret.needs.add(s); }
87 for(Sequence s : hates) { ret.hates.add(s); }
92 Iterable<Sequence> needs() { return needs; }
93 Iterable<Sequence> hates() { return hates; }
95 Position firstp() { return firstp; }
96 Position lastp() { return firstp().last(); }
98 public Iterator<Element> iterator() { return new ArrayIterator<Element>(elements); }
99 protected Sequence(Element[] elements) {
100 this.elements = elements;
101 for(int i=0; i<elements.length; i++)
102 if (elements[i]==null)
103 throw new RuntimeException("cannot have nulls in a sequence: " + this);
104 this.firstp = new Position(0, null);
107 // DO NOT MESS WITH THE FOLLOWING LINE!!!
108 private Forest.Many epsilonForm = null;
109 Forest epsilonForm(Input.Region loc) {
110 if (epsilonForm!=null) return epsilonForm;
111 epsilonForm = new Forest.Many();
112 epsilonForm.merge(firstp().rewrite(loc, false));
116 protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
119 // Position //////////////////////////////////////////////////////////////////////////////
121 /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
122 class Position implements IntegerMappable {
126 private Forest zero = null;
127 public Forest zero(Input.Region reg) {
128 if (zero != null) return zero;
129 if (pos > 0) throw new RuntimeException("Position.zero(): pos>0");
130 return zero = rewrite(reg);
134 private final Position next;
135 private final Position prev;
136 final Forest[] holder;
138 private Position(int pos, Position prev) {
140 this.next = pos==elements.length ? null : new Position(pos+1, this);
141 this.holder = new Forest[elements.length];
145 boolean isFirst() { return pos==0; }
147 /** the element immediately after this Position, or null if this is the last Position */
148 public Element element() { return pos>=elements.length ? null : elements[pos]; }
150 /** the element which produces the sequence to which this Position belongs */
151 public Sequence owner() { return Sequence.this; }
153 /** the next Position (the Position after <tt>this.element()</tt>) */
154 public Position next() { return next; }
156 /** true iff this Position is the last one in the sequence */
157 public boolean isLast() { return next()==null; }
158 public Position last() { return isLast() ? this : next().last(); }
159 public Position prev() { return prev; }
161 // Position /////////////////////////////////////////////////////////////////////////////////
163 final <T> Forest<T> rewrite(Input.Region loc) { return rewrite(loc, true); }
164 private final <T> Forest<T> rewrite(Input.Region loc, boolean epsilonCheck) {
165 if (epsilonCheck && this==firstp()) return epsilonForm(loc);
166 for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
167 for(int i=pos; i<elements.length; i++) {
168 if (holder[i]==null) holder[i] = elements[i].epsilonForm(loc);
169 if (holder[i]==null) throw new Error("bad " + i);
171 return Sequence.this.postReduce(loc, holder, this);
174 public String toString() {
175 StringBuffer ret = new StringBuffer();
177 for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
179 if (p==this) ret.append(" | ");
180 if (p.element()!=null) ret.append(p.element());
181 else ret.append(' ');
184 return ret.toString();
186 private final int idx = master_position_idx++;
187 public int toInt() { return idx; }
189 private static int master_position_idx = 0;
191 // toString //////////////////////////////////////////////////////////////////////////////
193 public String toString() { return toString(new StringBuffer(), false).toString(); }
194 StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
195 StringBuffer toString(StringBuffer sb, boolean spacing) {
196 for(int i=0; i<elements.length; i++) {
197 sb.append(elements[i]+"");
200 if (follow != null) {
204 for(Sequence s : needs) {
208 for(Sequence s : hates) {
216 // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
218 static class Constant extends Sequence {
219 private final Object result;
220 public Constant(Element[] e, Object result) {
222 if (result==null) throw new Error("constant sequences may not have result==null");
223 this.result = result;
225 Sequence _clone() { return new Constant(elements, result); }
226 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
227 return (Forest<T>)Forest.create(loc, result, null, false);
231 static class Singleton extends Sequence {
232 private final int idx;
233 public Singleton(Element e) { this(new Element[] { e }, 0); }
234 public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
235 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) { return args[idx]; }
236 Sequence _clone() { return new Singleton(elements,idx); }
239 static class Unwrap extends Sequence {
240 private boolean[] drops;
241 private final Object tag;
242 public Unwrap(Element[] e, Object tag) { this(e, tag, null); }
243 public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
244 Sequence _clone() { return new Unwrap(elements, tag, drops); }
245 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
246 for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
247 if (drops==null) return Forest.create(loc, (T)tag, args, true);
249 for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
250 Forest<T>[] args2 = new Forest[count];
252 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
253 return Forest.create(loc, (T)tag, args2, true);
257 static class RewritingSequence extends Sequence {
259 private final boolean[] drops;
260 private int count = 0;
261 Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
262 public RewritingSequence(Object tag, Element[] e) { this(tag, e, null); }
263 public RewritingSequence(Object tag, Element[] e, boolean[] drops) {
265 if (tag==null) throw new Error();
267 this.drops = drops == null ? new boolean[e.length] : drops;
268 for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
270 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
271 Forest<T>[] args2 = new Forest[count];
273 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
274 return Forest.create(loc, (T)tag, args2, false);
276 public StringBuffer toString(StringBuffer sb, boolean spacing) {
277 int len = sb.length();
279 sb.append("\""+StringUtil.escapify(tag.toString(),"\"\r\n")+"\":: ");
280 super.toString(sb, spacing);
281 len = sb.length()-len;
282 if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');