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 public boolean needed_or_hated = false;
19 public HashMap<Sequence,Boolean> canNeed = new HashMap<Sequence,Boolean>();
20 public HashMap<Sequence,Boolean> canKill = new HashMap<Sequence,Boolean>();
22 final HashSet<Sequence> hated = new HashSet<Sequence>();
24 final HashSet<Sequence> needs = new HashSet<Sequence>();
25 final HashSet<Sequence> hates = new HashSet<Sequence>();
27 final Position firstp;
31 // Static Constructors //////////////////////////////////////////////////////////////////////////////
33 abstract Sequence _clone();
35 Sequence ret = _clone();
36 for(Sequence s : needs) { ret.needs.add(s); }
37 for(Sequence s : hates) { ret.hates.add(s); s.hated.add(ret); }
42 /** create an empty sequence (matches the empty string) */
43 public static Sequence create() { return new Sequence.Constant.Empty(); }
45 /** create a sequence of one element */
46 public static Sequence create(Element e) { return create(new Element[] { e }, 0); }
48 /** create a sequence which drops the result of all but one of its element */
49 public static Sequence create(Element[] e, int which) { return new Singleton(e, which); }
51 /** create a sequence which always evaluates to a constant result */
52 public static Sequence create(Element[] e, Object result) { return new Constant(e, result); }
55 * create a sequence (general form)
56 * @param head the head of the output tree
57 * @param e the elements to match
58 * @param drop only elements of <tt>e</tt> whose corresponding <tt>boolean</tt> in <tt>drops</tt>
59 * is <i>false</i> will be included in the output tree
60 * @param foster if true, all children of the last child (ie
61 * grandchildren) are promoted to children of this
62 * node; this is very useful for matching repetitions
64 public static Sequence create(Object head, Element[] e, boolean[] drop, boolean foster) {
66 ? new Unwrap(e, head, drop)
67 : new RewritingSequence(head, e, drop);
70 ////////////////////////////////////////////////////////////////////////////////
72 /** return a new sequence identical to this one, but with a positive conjunct <tt>s</tt> */
73 public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed_or_hated=true; return ret; }
75 /** return a new sequence identical to this one, but with a negative conjunct <tt>s</tt> */
76 public Sequence andnot(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); s.needed_or_hated=true; return ret; }
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 boolean hatesAny(Iterable<Sequence> it) {
82 if (hates.isEmpty()) return false;
83 for(Sequence s : it) if (hates.contains(s)) return true;
87 Iterable<Sequence> needs() { return needs; }
88 Iterable<Sequence> hates() { return hates; }
90 Position firstp() { return firstp; }
92 public Iterator<Element> iterator() { return new ArrayIterator<Element>(elements); }
93 protected Sequence(Element[] elements) {
94 this.elements = elements;
95 this.firstp = new Position(0);
98 // DO NOT MESS WITH THE FOLLOWING LINE!!!
99 private Forest.Many epsilonForm = null;
100 Forest epsilonForm(Input.Region loc) {
101 if (epsilonForm!=null) return epsilonForm;
102 epsilonForm = new Forest.Many();
103 epsilonForm.merge(firstp().rewrite(loc, false));
107 protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
110 // Position //////////////////////////////////////////////////////////////////////////////
112 /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
113 class Position implements IntegerMappable {
116 public int compareTo(Position p, Walk.Cache cache) {
117 Position position = this;
118 Position rposition = p;
120 if (Reduction.canKill(cache, position, rposition) &&
121 Reduction.canKill(cache, rposition, position)) throw new Error();
122 if (Reduction.canKill(cache, position, rposition)) ret = 1;
123 else if (Reduction.canKill(cache, rposition, position)) ret = -1;
124 if (Reduction.canNeed(cache, position, rposition)) ret = 1;
125 else if (Reduction.canNeed(cache, rposition, position)) ret = -1;
129 private Forest zero = null;
130 public Forest zero(Input.Region reg) {
131 if (zero != null) return zero;
132 if (pos > 0) throw new Error();
133 return zero = rewrite(reg);
138 private final Position next;
139 final Forest[] holder;
141 private Position(int pos) {
143 this.next = pos==elements.length ? null : new Position(pos+1);
144 this.holder = new Forest[elements.length];
147 boolean isFirst() { return pos==0; }
149 /** the element immediately after this Position, or null if this is the last Position */
150 public Element element() { return pos>=elements.length ? null : elements[pos]; }
152 /** the element which produces the sequence to which this Position belongs */
153 public Sequence owner() { return Sequence.this; }
155 /** the next Position (the Position after <tt>this.element()</tt>) */
156 public Position next() { return next; }
158 /** true iff this Position is the last one in the sequence */
159 public boolean isLast() { return next()==null; }
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 Forest<T> ret = Sequence.this.postReduce(loc, holder, this);
172 //for(int k=0; k<pos; k++) holder[k] = null; // to help GC
176 public String toString() {
177 StringBuffer ret = new StringBuffer();
179 for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
181 if (p==this) ret.append(" | ");
182 if (p.element()!=null) ret.append(p.element());
183 else ret.append(' ');
186 return ret.toString();
188 private final int idx = master_position_idx++;
189 public int toInt() { return idx; }
191 private static int master_position_idx = 0;
193 // toString //////////////////////////////////////////////////////////////////////////////
195 public String toString() { return toString(new StringBuffer(), false).toString(); }
196 StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
197 StringBuffer toString(StringBuffer sb, boolean spacing) {
198 for(int i=0; i<elements.length; i++) {
199 sb.append(elements[i].toString());
202 if (follow != null) {
206 for(Sequence s : needs) {
210 for(Sequence s : hates) {
218 // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
220 static class Constant extends Sequence {
221 private final Object result;
222 public Constant(Element[] e, Object result) { super(e); this.result = result; }
223 Sequence _clone() { return new Constant(elements, result); }
224 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
225 return (Forest<T>)Forest.create(loc, result, null, false);
227 static class Drop extends Constant {
228 Sequence _clone() { return new Drop(elements); }
229 public Drop(Element[] e) { super(e, null); }
231 static class Empty extends Sequence.Constant.Drop {
232 Sequence _clone() { return new Empty(); }
233 public Empty() { super(new Element[] { }); } }
236 static class Singleton extends Sequence {
237 private final int idx;
238 public Singleton(Element e) { this(new Element[] { e }, 0); }
239 public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
240 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) { return args[idx]; }
241 Sequence _clone() { return new Singleton(elements,idx); }
244 static class Unwrap extends Sequence {
245 private boolean[] drops;
246 private final Object tag;
247 public Unwrap(Element[] e, Object tag) { super(e); this.drops = null; this.tag = tag; }
248 public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
249 Sequence _clone() { return new Unwrap(elements, tag, drops); }
250 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
251 for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
252 if (drops==null) return Forest.create(loc, (T)tag, args, true);
254 for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
255 Forest<T>[] args2 = new Forest[count];
257 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
258 return Forest.create(loc, (T)tag, args2, true);
264 static class RegionRewritingSequence extends RewritingSequence {
265 private Functor<Input.Region, Object> tagf;
266 public RegionRewritingSequence(Functor<Input.Region,Object> tagfunctor, Element[] e, boolean[] drops) {
267 super(null, e, drops);
268 this.tagf = tagfunctor;
270 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
271 this.tag = tagf.invoke(loc);
272 Forest<T> ret = super.postReduce(loc, args, p);
278 static class RewritingSequence extends Sequence {
279 /*private*/public /*final*/ Object tag;
280 private final boolean[] drops;
281 private int count = 0;
282 Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
283 public RewritingSequence(Object tag, Element[] e) { this(tag, e, null); }
284 public RewritingSequence(Object tag, Element[] e, boolean[] drops) {
287 this.drops = drops == null ? new boolean[e.length] : drops;
288 for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
290 public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
291 Forest<T>[] args2 = new Forest[count];
293 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
294 //System.out.println("reduce \""+tag+"\"");
295 return Forest.create(loc, (T)tag, args2, false);
297 public StringBuffer toString(StringBuffer sb, boolean spacing) {
298 int len = sb.length();
300 sb.append("\""+StringUtil.escapify(tag.toString(),"\"\r\n")+"\":: ");
301 super.toString(sb, spacing);
302 len = sb.length()-len;
303 if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');