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 abstract Sequence _clone();
17 Sequence ret = _clone();
18 for(Sequence s : needs) { ret.needs.add(s); s.needed.add(ret); }
19 for(Sequence s : hates) { ret.hates.add(s); s.hated.add(ret); }
20 ret.noFollow = noFollow;
24 /** the empty sequence (matches the empty string) */
25 public static final Sequence empty = new Sequence.Constant.Empty();
27 /** after matching the sequence, do not add anything to the output tree */
28 public static Sequence drop(Element[] e, boolean lame) { return new Constant.Drop(e, lame); }
30 /** after matching the sequence, insert a constant into the output tree */
31 public static Sequence constant(Element[] e, Object o) { return new Constant(e, o); }
33 /** after matching the sequence, place the result of the <tt>idx</tt>th match in the output tree */
34 public static Sequence singleton(Element[] e, int idx) { return new Singleton(e, idx); }
37 * after matching the sequence, create the specified output tree
38 * @param tag the tag for the output tree
39 * @param e the elements to match
40 * @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
42 public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops) { return new RewritingSequence(tag, e, drops); }
44 ////////////////////////////////////////////////////////////////////////////////
46 public Element noFollow = null;
47 public final Topology noFollow() { return noFollow==null ? null : Atom.toAtom(noFollow); }
50 if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
51 return Atom.toAtom(elements[0]);
54 public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed.add(ret); return ret; }
55 public Sequence not(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); return ret; }
57 protected final Element[] elements;
59 final HashSet<Sequence> needed = new HashSet<Sequence>();
60 final HashSet<Sequence> hated = new HashSet<Sequence>();
61 final HashSet<Sequence> needs = new HashSet<Sequence>();
62 final HashSet<Sequence> hates = new HashSet<Sequence>();
63 public boolean lame = false;
65 final Position firstp;
66 Position firstp() { return firstp; }
68 public Iterator<Element> iterator() { return new ArrayIterator<Element>(elements); }
69 protected Sequence(Element[] elements) {
70 this.elements = elements;
71 this.firstp = new Position(0);
74 // DO NOT MESS WITH THE FOLLOWING LINE!!!
75 private Forest.Ref epsilonForm = null;
76 Forest epsilonForm() {
77 if (epsilonForm!=null) return epsilonForm;
78 epsilonForm = new Forest.Ref();
79 epsilonForm.merge(firstp().rewrite(null, false));
83 protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p);
86 // Position //////////////////////////////////////////////////////////////////////////////
88 /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
89 public class Position implements IntegerMappable {
91 private Forest zero = null;
92 public Forest zero() {
93 if (zero != null) return zero;
94 if (pos > 0) throw new Error();
95 return zero = rewrite(null);
100 private final Position next;
101 final Forest[] holder;
103 private Position(int pos) {
105 this.next = pos==elements.length ? null : new Position(pos+1);
106 this.holder = new Forest[elements.length];
109 boolean isFirst() { return pos==0; }
111 /** the element immediately after this Position, or null if this is the last Position */
112 public Element element() { return pos>=elements.length ? null : elements[pos]; }
114 /** the element which produces the sequence to which this Position belongs */
115 public Sequence owner() { return Sequence.this; }
117 /** the next Position (the Position after <tt>this.element()</tt>) */
118 public Position next() { return next; }
120 /** true iff this Position is the last one in the sequence */
121 public boolean isLast() { return next()==null; }
123 // Position /////////////////////////////////////////////////////////////////////////////////
125 final <T> Forest<T> rewrite(Input.Location loc) { return rewrite(loc, true); }
126 private final <T> Forest<T> rewrite(Input.Location loc, boolean epsilonCheck) {
127 if (epsilonCheck && this==firstp()) return epsilonForm();
128 for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
129 for(int i=pos; i<elements.length; i++) {
130 if (holder[i]==null) holder[i] = elements[i].epsilonForm();
131 if (holder[i]==null) throw new Error("bad " + i);
133 Forest<T> ret = Sequence.this.postReduce(loc, holder, this);
134 for(int k=0; k<pos; k++) holder[k] = null; // to help GC
138 public String toString() {
139 StringBuffer ret = new StringBuffer();
141 for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
143 if (p==this) ret.append(" | ");
144 if (p.element()!=null) ret.append(p.element());
145 else ret.append(' ');
148 return ret.toString();
150 private final int idx = master_position_idx++;
151 public int toInt() { return idx; }
153 private static int master_position_idx = 0;
155 // toString //////////////////////////////////////////////////////////////////////////////
157 public String toString() { return toString(new StringBuffer(), false).toString(); }
158 StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
159 StringBuffer toString(StringBuffer sb, boolean spacing) {
160 for(int i=0; i<elements.length; i++) {
161 sb.append(elements[i].toString());
168 // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
170 static class Constant extends Sequence {
171 private final Object result;
172 public Constant(Element[] e, Object result) { super(e); this.result = result; }
173 Sequence _clone() { return new Constant(elements, result); }
174 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
175 return (Forest<T>)Forest.leaf(loc, result, p);
177 static class Drop extends Constant {
178 Sequence _clone() { return new Drop(elements, lame); }
179 public Drop(Element[] e, boolean lame) {
184 static class Empty extends Sequence.Constant.Drop {
185 Sequence _clone() { return new Empty(); }
186 public Empty() { super(new Element[] { }, false); } }
189 static class Singleton extends Sequence {
190 private final int idx;
191 public Singleton(Element e) { this(new Element[] { e }, 0); }
192 public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
193 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) { return (Forest<T>)Forest.singleton(loc, args[idx], p); }
194 Sequence _clone() { return new Singleton(elements,idx); }
197 public static class Unwrap extends Sequence {
198 private boolean[] drops;
199 public Unwrap(Element[] e) { super(e); this.drops = null; }
200 public Unwrap(Element[] e, boolean[] drops) { super(e); this.drops = drops; }
201 Sequence _clone() { return new Unwrap(elements, drops); }
202 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
203 for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
204 if (drops==null) return Forest.create(loc, null, args, true, false, p);
206 for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
207 Forest<T>[] args2 = new Forest[count];
209 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
210 return Forest.create(loc, null, args2, true, false, p);
214 static class RewritingSequence extends Sequence {
215 /*private*/public final Object tag;
216 private final boolean[] drops;
217 private int count = 0;
218 Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
219 public RewritingSequence(Object tag, Element[] e) { this(tag, e, null); }
220 public RewritingSequence(Object tag, Element[] e, boolean[] drops) {
223 this.drops = drops == null ? new boolean[e.length] : drops;
224 for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
226 public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
227 Forest<T>[] args2 = new Forest[count];
229 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
230 //System.out.println("reduce \""+tag+"\"");
231 return Forest.create(loc, (T)tag, args2, false, false, p);
233 public StringBuffer toString(StringBuffer sb, boolean spacing) {
234 int len = sb.length();
235 super.toString(sb, spacing);
236 len = sb.length()-len;
237 if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');