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 /** the empty sequence (matches the empty string) */
16 public static final Sequence empty = new Sequence.Constant.Empty();
18 /** after matching the sequence, do not add anything to the output tree */
19 public static Sequence drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) { return new Constant.Drop(e, and, not, lame); }
21 /** after matching the sequence, insert a constant into the output tree */
22 public static Sequence constant(Element[] e, Object o, HashSet<Sequence> and, HashSet<Sequence> not) { return new Constant(e, o, and, not); }
24 /** after matching the sequence, place the result of the <tt>idx</tt>th match in the output tree */
25 public static Sequence singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { return new Singleton(e, idx, and, not); }
28 * after matching the sequence, create the specified output tree
29 * @param tag the tag for the output tree
30 * @param e the elements to match
31 * @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
33 public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
34 return new RewritingSequence(tag, e, drops, and, not); }
36 ////////////////////////////////////////////////////////////////////////////////
38 public Topology noFollow() { return null; }
41 if (elements.length>1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
42 if (elements.length==0) return null;
43 return elements[0].toAtom();
46 protected final Element[] elements;
48 final HashSet<Sequence> needs;
49 final HashSet<Sequence> hates;
52 final Position firstp;
53 Position firstp() { return firstp; }
55 public Iterator<Element> iterator() { return new ArrayIterator<Element>(elements); }
56 protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
57 this.needs = and==null ? new HashSet<Sequence>() : and;
58 this.hates = not==null ? new HashSet<Sequence>() : not;
59 //for(Sequence s : needs) s.lame = true;
60 //for(Sequence s : hates) s.lame = true;
61 this.elements = elements;
62 this.firstp = new Position(0);
65 void reachable(HashSet<Position> h) { firstp().reachable(h); }
67 Forest epsilonForm() { return firstp().rewrite(null); }
69 protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
72 // Position //////////////////////////////////////////////////////////////////////////////
74 /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
75 public class Position {
77 void reachable(HashSet<Position> h) {
78 if (h.contains(this)) return;
80 if (element() != null) element().reachable(h);
84 private final Position next;
85 final Forest[] holder;
87 private Position(int pos) {
89 this.next = pos==elements.length ? null : new Position(pos+1);
90 this.holder = new Forest[elements.length];
93 boolean isFirst() { return pos==0; }
94 boolean isRightNullable(Walk.Cache cache) {
95 if (isLast()) return true;
96 if (!element().possiblyEpsilon(cache)) return false;
97 return next().isRightNullable(cache);
100 /** the element immediately after this Position, or null if this is the last Position */
101 public Element element() { return pos>=elements.length ? null : elements[pos]; }
103 /** the element which produces the sequence to which this Position belongs */
104 public Sequence owner() { return Sequence.this; }
106 /** the next Position (the Position after <tt>this.element()</tt>) */
107 public Position next() { return next; }
109 /** true iff this Position is the last one in the sequence */
110 public boolean isLast() { return next()==null; }
112 // Reduction /////////////////////////////////////////////////////////////////////////////////
114 <T> Forest<T> rewrite(Token.Location loc) {
115 for(int i=pos; i<elements.length; i++) if (holder[i]==null) holder[i] = elements[i].epsilonForm();
116 Forest<T> ret = Sequence.this.postReduce(loc, holder);
117 for(int k=0; k<pos; k++) holder[k] = null; // to help GC
121 public String toString() {
122 StringBuffer ret = new StringBuffer();
124 for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
126 if (p==this) ret.append(" | ");
127 if (p.element()!=null) ret.append(p.element().possiblyEpsilon(null) ? "["+p.element()+"]" : p.element());
128 else ret.append(' ');
131 return ret.toString();
136 // toString //////////////////////////////////////////////////////////////////////////////
138 public String toString() { return toString(new StringBuffer(), false).toString(); }
139 StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
140 StringBuffer toString(StringBuffer sb, boolean spacing) {
141 for(int i=0; i<elements.length; i++) {
142 sb.append(elements[i].toString());
149 // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
151 static class Constant extends Sequence {
152 private final Object result;
153 public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
154 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
155 return (Forest<T>)Forest.leaf(loc, result, this);
157 static class Drop extends Constant {
158 public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
159 super(e, null, and, not);
163 static class Empty extends Sequence.Constant.Drop { public Empty() { super(new Element[] { }, null, null, false); } }
166 static class Singleton extends Sequence {
167 private final int idx;
168 public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
169 public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
170 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
173 static class Unwrap extends Sequence {
174 private boolean[] drops;
175 public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = null; }
176 public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
177 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
178 if (drops==null) return Forest.create(loc, null, args, this, true, false);
180 for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
181 Forest<T>[] args2 = new Forest[count];
183 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
184 return Forest.create(loc, null, args2, this, true, false);
188 static class RewritingSequence extends Sequence {
189 private final Object tag;
190 private final boolean[] drops;
191 private int count = 0;
192 public RewritingSequence(Object tag, Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { this(tag, e, null, and, not); }
193 public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
196 this.drops = drops == null ? new boolean[e.length] : drops;
197 for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
199 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
200 Forest<T>[] args2 = new Forest[count];
202 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
203 return Forest.create(loc, (T)tag, args2, this, false, false);
205 public StringBuffer toString(StringBuffer sb, boolean spacing) {
206 int len = sb.length();
207 super.toString(sb, spacing);
208 len = sb.length()-len;
209 if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');