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 return elements[0].toAtom();
45 protected final Element[] elements;
47 final HashSet<Sequence> needs;
48 final HashSet<Sequence> hates;
51 final Position firstp;
52 Position firstp() { return firstp; }
54 public Iterator<Element> iterator() { return new ArrayIterator<Element>(elements); }
55 protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
56 this.needs = and==null ? new HashSet<Sequence>() : and;
57 this.hates = not==null ? new HashSet<Sequence>() : not;
58 //for(Sequence s : needs) s.lame = true;
59 //for(Sequence s : hates) s.lame = true;
60 this.elements = elements;
61 this.firstp = new Position(0);
64 void reachable(HashSet<Position> h) { firstp().reachable(h); }
66 Forest epsilonForm() { return firstp().rewrite(null); }
68 protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
71 // Position //////////////////////////////////////////////////////////////////////////////
73 /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
74 public class Position {
76 void reachable(HashSet<Position> h) {
77 if (h.contains(this)) return;
79 if (element() != null) element().reachable(h);
83 private final Position next;
84 final Forest[] holder;
86 private Position(int pos) {
88 this.next = pos==elements.length ? null : new Position(pos+1);
89 this.holder = new Forest[elements.length];
92 boolean isFirst() { return pos==0; }
93 boolean isRightNullable(Walk.Cache cache) {
94 if (isLast()) return true;
95 if (!element().possiblyEpsilon(cache)) return false;
96 return next().isRightNullable(cache);
99 /** the element immediately after this Position, or null if this is the last Position */
100 public Element element() { return pos>=elements.length ? null : elements[pos]; }
102 /** the element which produces the sequence to which this Position belongs */
103 public Sequence owner() { return Sequence.this; }
105 /** the next Position (the Position after <tt>this.element()</tt>) */
106 public Position next() { return next; }
108 /** true iff this Position is the last one in the sequence */
109 public boolean isLast() { return next()==null; }
111 // Reduction /////////////////////////////////////////////////////////////////////////////////
113 <T> Forest<T> rewrite(Token.Location loc) {
114 for(int i=pos; i<elements.length; i++) if (holder[i]==null) holder[i] = elements[i].epsilonForm();
115 Forest<T> ret = Sequence.this.postReduce(loc, holder);
116 for(int k=0; k<pos; k++) holder[k] = null; // to help GC
120 public String toString() {
121 StringBuffer ret = new StringBuffer();
123 for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
125 if (p==this) ret.append(" | ");
126 if (p.element()!=null) ret.append(p.element().possiblyEpsilon(null) ? "["+p.element()+"]" : p.element());
127 else ret.append(' ');
130 return ret.toString();
135 // toString //////////////////////////////////////////////////////////////////////////////
137 public String toString() { return toString(new StringBuffer(), false).toString(); }
138 StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
139 StringBuffer toString(StringBuffer sb, boolean spacing) {
140 for(int i=0; i<elements.length; i++) {
141 sb.append(elements[i].toString());
148 // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
150 static class Constant extends Sequence {
151 private final Object result;
152 public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
153 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
154 return (Forest<T>)Forest.leaf(loc, result, this);
156 static class Drop extends Constant {
157 public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
158 super(e, null, and, not);
162 static class Empty extends Sequence.Constant.Drop { public Empty() { super(new Element[] { }, null, null, false); } }
165 static class Singleton extends Sequence {
166 private final int idx;
167 public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
168 public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
169 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
172 static class Unwrap extends Sequence {
173 private boolean[] drops;
174 public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = null; }
175 public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
176 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
177 if (drops==null) return Forest.create(loc, null, args, this, true, false);
179 for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
180 Forest<T>[] args2 = new Forest[count];
182 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
183 return Forest.create(loc, null, args2, this, true, false);
187 static class RewritingSequence extends Sequence {
188 private final Object tag;
189 private final boolean[] drops;
190 private int count = 0;
191 public RewritingSequence(Object tag, Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { this(tag, e, null, and, not); }
192 public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
195 this.drops = drops == null ? new boolean[e.length] : drops;
196 for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
198 public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
199 Forest<T>[] args2 = new Forest[count];
201 for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
202 return Forest.create(loc, (T)tag, args2, this, false, false);
204 public StringBuffer toString(StringBuffer sb, boolean spacing) {
205 int len = sb.length();
206 super.toString(sb, spacing);
207 len = sb.length()-len;
208 if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');