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