1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
10 /** juxtaposition; zero or more adjacent Elements; can specify a rewriting */
11 public abstract class Sequence extends Element implements Iterable<Element> {
13 // Static Constructors //////////////////////////////////////////////////////////////////////////////
15 public abstract Sequence and(Sequence s);
16 public abstract Sequence not(Sequence s);
18 private void needs(Sequence s) { s.needed.add(this); needs.add(s); }
19 private void hates(Sequence s) { s.hated.add(this); hates.add(s); }
21 /** the empty sequence (matches the empty string) */
22 public static final Sequence empty = new Sequence.Constant.Empty();
24 /** after matching the sequence, do not add anything to the output tree */
25 public static Sequence drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) { return new Constant.Drop(e, and, not, lame); }
27 /** after matching the sequence, insert a constant into the output tree */
28 public static Sequence constant(Element[] e, Object o, HashSet<Sequence> and, HashSet<Sequence> not) { return new Constant(e, o, and, not); }
30 /** after matching the sequence, place the result of the <tt>idx</tt>th match in the output tree */
31 public static Sequence singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { return new Singleton(e, idx, and, not); }
34 * after matching the sequence, create the specified output tree
35 * @param tag the tag for the output tree
36 * @param e the elements to match
37 * @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
39 public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
40 return new RewritingSequence(tag, e, drops, and, not); }
42 ////////////////////////////////////////////////////////////////////////////////
44 public Element noFollow = null;
45 public String name = null;
46 public void setName(String name) { this.name = name; }
47 public final Topology noFollow() { return noFollow==null ? null : noFollow.toAtom(); }
50 if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
51 return elements[0].toAtom();
54 protected final Element[] elements;
56 HashSet<Sequence> needed;
57 HashSet<Sequence> hated;
58 final HashSet<Sequence> needs;
59 final HashSet<Sequence> hates;
60 public boolean lame = false;
62 final Position firstp;
63 Position firstp() { return firstp; }
65 public Iterator<Element> iterator() { return new ArrayIterator<Element>(elements); }
66 protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
67 this.needs = and==null ? new HashSet<Sequence>() : and;
68 this.hates = not==null ? new HashSet<Sequence>() : not;
69 if (this.needs != null)
70 for(Sequence s : this.needs)
71 (s.needed==null?(s.needed=new HashSet<Sequence>()):s.needed).add(this);
72 if (this.hates != null)
73 for(Sequence s : this.hates)
74 (s.hated==null?(s.hated=new HashSet<Sequence>()):s.hated).add(this);
75 this.elements = elements;
76 this.firstp = new Position(0);
79 // DO NOT MESS WITH THE FOLLOWING LINE!!!
80 private Forest.Ref epsilonForm = null;
81 private boolean eps = false;
82 Forest epsilonForm() {
83 if (epsilonForm==null) {
84 epsilonForm = new Forest.Ref();
85 epsilonForm.merge(firstp().rewrite2(null));
90 protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args);
93 // Position //////////////////////////////////////////////////////////////////////////////
95 /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
96 public class Position implements IntegerMappable {
98 private Forest zero = null;
99 public Forest zero() {
100 if (zero != null) return zero;
101 if (pos > 0) throw new Error();
102 return zero = rewrite(null);
107 private final Position next;
108 final Forest[] holder;
110 private Position(int pos) {
112 this.next = pos==elements.length ? null : new Position(pos+1);
113 this.holder = new Forest[elements.length];
116 boolean isFirst() { return pos==0; }
118 /** the element immediately after this Position, or null if this is the last Position */
119 public Element element() { return pos>=elements.length ? null : elements[pos]; }
121 /** the element which produces the sequence to which this Position belongs */
122 public Sequence owner() { return Sequence.this; }
124 /** the next Position (the Position after <tt>this.element()</tt>) */
125 public Position next() { return next; }
127 /** true iff this Position is the last one in the sequence */
128 public boolean isLast() { return next()==null; }
130 // Position /////////////////////////////////////////////////////////////////////////////////
132 final <T> Forest<T> rewrite(Input.Location loc) {
133 if (this==firstp()) return epsilonForm();
134 return rewrite2(loc);
137 final <T> Forest<T> rewrite2(Input.Location loc) {
138 for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
139 for(int i=pos; i<elements.length; i++) {
140 if (holder[i]==null) holder[i] = elements[i].epsilonForm();
141 if (holder[i]==null) throw new Error("bad " + i);
143 Forest<T> ret = Sequence.this.postReduce(loc, holder);
144 for(int k=0; k<pos; k++) holder[k] = null; // to help GC
148 public String toString() {
149 StringBuffer ret = new StringBuffer();
151 for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
153 if (p==this) ret.append(" | ");
154 if (p.element()!=null) ret.append(p.element());
155 else ret.append(' ');
158 return ret.toString();
160 private final int idx = master_position_idx++;
161 public int toInt() { return idx; }
163 private static int master_position_idx = 0;
165 // toString //////////////////////////////////////////////////////////////////////////////
167 public String toString() { return toString(new StringBuffer(), false).toString(); }
168 StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
169 StringBuffer toString(StringBuffer sb, boolean spacing) {
170 for(int i=0; i<elements.length; i++) {
171 sb.append(elements[i].toString());
178 // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
180 static class Constant extends Sequence {
181 private final Object result;
182 public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
183 public Sequence and(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.needs(s); return ret; }
184 public Sequence not(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.hates(s); return ret; }
185 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
186 return (Forest<T>)Forest.leaf(loc, result);
188 static class Drop extends Constant {
189 public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
190 super(e, null, and, not);
194 static class Empty extends Sequence.Constant.Drop { public Empty() { super(new Element[] { }, null, null, false); } }
197 static class Singleton extends Sequence {
198 private final int idx;
199 public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
200 public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
201 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
202 public Sequence and(Sequence s) { Sequence ret = new Singleton(elements, idx, needs, hates); ret.needs(s); return ret; }
203 public Sequence not(Sequence s) { Sequence ret = new Singleton(elements, idx, needs, hates); ret.hates(s); return ret; }
206 public static class Unwrap extends Sequence {
207 private boolean[] drops;
208 public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = null; }
209 public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
210 public Sequence and(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.needs(s); return ret; }
211 public Sequence not(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.hates(s); return ret; }
212 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
213 for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
214 if (drops==null) return Forest.create(loc, null, args, true, false);
216 for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
217 Forest<T>[] args2 = new Forest[count];
219 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
220 return Forest.create(loc, null, args2, true, false);
224 static class RewritingSequence extends Sequence {
225 /*private*/public final Object tag;
226 private final boolean[] drops;
227 private int count = 0;
228 public Sequence and(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops, needs, hates); ret.needs(s); return ret; }
229 public Sequence not(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops, needs, hates); ret.hates(s); return ret; }
230 public RewritingSequence(Object tag, Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { this(tag, e, null, and, not); }
231 public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
234 this.drops = drops == null ? new boolean[e.length] : drops;
235 for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
237 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
238 Forest<T>[] args2 = new Forest[count];
240 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
241 //System.out.println("reduce \""+tag+"\"");
242 return Forest.create(loc, (T)tag, args2, false, false);
244 public StringBuffer toString(StringBuffer sb, boolean spacing) {
245 int len = sb.length();
246 super.toString(sb, spacing);
247 len = sb.length()-len;
248 if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');