checkpoint
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
9
10 /** juxtaposition; zero or more adjacent Elements; can specify a rewriting */
11 public abstract class Sequence extends Element implements Iterable<Element> {
12
13     // Static Constructors //////////////////////////////////////////////////////////////////////////////
14
15     /** the empty sequence (matches the empty string) */
16     public static final Sequence empty = new Sequence.Constant.Empty();
17
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); }
20
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); }
23
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); }
26
27     /**
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
32      **/
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); }
35
36     ////////////////////////////////////////////////////////////////////////////////
37
38     protected final Element[] elements;
39
40     final HashSet<Sequence> needs;
41     final HashSet<Sequence> hates;
42           boolean           lame  = false;
43
44     final Position          firstp;
45     Position firstp() { return firstp; }
46
47     public Iterator<Element> iterator()    { return new ArrayIterator<Element>(elements); }
48     protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
49         this.needs = and==null ? new HashSet<Sequence>() : and;
50         this.hates = not==null ? new HashSet<Sequence>() : not;
51         //for(Sequence s : needs) s.lame = true;
52         //for(Sequence s : hates) s.lame = true;
53         this.elements = elements;
54         this.firstp = new Position(0);
55     }
56
57     void reachable(HashSet<Position> h) { firstp().reachable(h); }
58
59     Forest epsilonForm() { return firstp().rewrite(null); }
60
61     protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
62
63
64     // Position //////////////////////////////////////////////////////////////////////////////
65
66     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
67     public class Position {
68
69         void reachable(HashSet<Position> h) {
70             if (h.contains(this)) return;
71             h.add(this);
72             if (element() != null) element().reachable(h);
73         }
74
75                 final int      pos;
76         private final Position next;
77                 final Forest[] holder;
78         
79         private Position(int pos) {
80             this.pos      = pos;
81             this.next     = pos==elements.length ? null : new Position(pos+1);
82             this.holder   = new Forest[elements.length];
83         }
84
85         boolean isFirst() { return pos==0; }
86         boolean isRightNullable(Walk.Cache cache) {
87             if (isLast()) return true;
88             if (!element().possiblyEpsilon(cache)) return false;
89             return next().isRightNullable(cache);
90         }
91
92         /** the element immediately after this Position, or null if this is the last Position */
93         public Element  element() { return pos>=elements.length ? null : elements[pos]; }
94
95         /** the element which produces the sequence to which this Position belongs */
96         public Sequence owner() { return Sequence.this; }
97
98         /** the next Position (the Position after <tt>this.element()</tt>) */
99         public Position next() { return next; }
100
101         /** true iff this Position is the last one in the sequence */
102         public boolean isLast() { return next()==null; }
103
104         // Reduction /////////////////////////////////////////////////////////////////////////////////
105
106         <T> Forest<T> rewrite(Token.Location loc) {
107             for(int i=pos; i<elements.length; i++) if (holder[i]==null) holder[i] = elements[i].epsilonForm();
108             Forest<T> ret = Sequence.this.postReduce(loc, holder);
109             for(int k=0; k<pos; k++) holder[k] = null; // to help GC
110             return ret;
111         }
112
113         public String   toString() {
114             StringBuffer ret = new StringBuffer();
115             ret.append("<{");
116             for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
117                 ret.append(' ');
118                 if (p==this) ret.append(" | ");
119                 if (p.element()!=null) ret.append(p.element().possiblyEpsilon(null) ? "["+p.element()+"]" : p.element());
120                 else                   ret.append(' ');
121             }
122             ret.append("}>");
123             return ret.toString();
124         }
125     }
126
127
128     // toString //////////////////////////////////////////////////////////////////////////////
129
130     public String toString() { return toString(new StringBuffer(), false).toString(); }
131     StringBuffer toString(StringBuffer sb) { return toString(sb, true); }
132     StringBuffer toString(StringBuffer sb, boolean spacing) {
133         for(int i=0; i<elements.length; i++) {
134             sb.append(elements[i].toString());
135             sb.append(' ');
136         }
137         return sb;
138     }
139
140
141     // Specialized Subclasses //////////////////////////////////////////////////////////////////////////////
142
143     static class Constant extends Sequence {
144         private final Object result;
145         public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
146         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
147             return (Forest<T>)Forest.leaf(loc, result, this);
148         }
149         static class Drop extends Constant {
150             public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
151                 super(e, null, and, not);
152                 this.lame = lame;
153             }
154         }
155         static class Empty extends Sequence.Constant.Drop { public Empty() { super(new Element[] { }, null, null, false); } }
156     }
157
158     static class Singleton extends Sequence {
159         private final int idx;
160         public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
161         public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
162         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
163     }
164
165     static class Unwrap extends Sequence {
166         private boolean[] drops;
167         public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not)                  { super(e, and, not); this.drops = null; }
168         public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
169         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
170             if (drops==null) return Forest.create(loc, null, args, this, true, false);
171             int count = 0;
172             for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
173             Forest<T>[] args2 = new Forest[count];
174             int j = 0;
175             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
176             return Forest.create(loc, null, args2, this, true, false);            
177         }
178     }
179
180     static class RewritingSequence extends Sequence {
181         private final Object tag;
182         private final boolean[] drops;
183         private int count = 0;
184         public RewritingSequence(Object tag, Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { this(tag, e, null, and, not); }
185         public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
186             super(e, and, not);
187             this.tag = tag;
188             this.drops = drops == null ? new boolean[e.length] : drops;
189             for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
190         }
191         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
192             Forest<T>[] args2 = new Forest[count];
193             int j = 0;
194             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
195             return Forest.create(loc, (T)tag, args2, this, false, false);
196         }
197         public StringBuffer toString(StringBuffer sb, boolean spacing) {
198             int len = sb.length();
199             super.toString(sb, spacing);
200             len = sb.length()-len;
201             if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');
202             sb.append(" => ");
203             sb.append(tag);
204             return sb;
205         }
206     }
207 }