checkpoint
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.util.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
10 public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>> {
11
12     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
14         try {
15             Iterator<Tree<T>> it = expand(true).iterator();
16             if (!it.hasNext()) throw new ParseFailed();
17             return it.next();
18         } catch (InnerAmbiguous ia) { throw new Ambiguous(ia.f); }
19     }
20
21     /** expand this forest into a set of trees */
22     public HashSet<Tree<T>> expand(boolean toss) {
23         final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
24         visit(new TreeMaker2<T>(toss, ret), null, null);
25         if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
26         return ret;
27     }
28
29     private static class InnerAmbiguous extends RuntimeException {
30         public final Forest<?> f;
31         public InnerAmbiguous(Forest<?> f) { this.f = f; }
32     }
33
34     public static interface TreeConsumer<T> {
35         public void addTree(Tree<T> t);
36     }
37     public static class HashSetTreeConsumer<T> extends HashSet<Tree<T>> implements TreeConsumer<T> {
38         public void addTree(Tree<T> t) { super.add(t); }
39     }
40
41     static        <T> Forest<T> singleton(Input.Location loc)                       { return create(loc, null, new Forest[] { }, false, true); }
42     static        <T> Forest<T> singleton(Input.Location loc, Forest<T> body)       { return create(loc, null, new Forest[] { body },  false, true); }
43     static        <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
44     public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
45         return new MyBody<T>(loc, tag, tokens, unwrap, singleton);
46     }
47
48     // Body //////////////////////////////////////////////////////////////////////////////
49
50     protected static interface Body<T> {
51         void expand(int i, TreeMaker<T> h);
52     }
53
54     protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
55
56         public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
57             ivbc.invoke(this, b, c);
58         }
59
60         private final Input.Location    location;
61         private final T                 tag;
62         private final Forest<T>[]       tokens;
63         private final boolean           unwrap;
64         private final boolean           singleton;
65
66         private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
67             this.location = loc;
68             this.tag = tag;
69             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
70             if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
71             if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
72             this.unwrap = unwrap;
73             this.singleton = singleton;
74         }
75
76         public void expand(final int i, final TreeMaker<T> h) {
77             if (singleton) {
78                 tokens[0].visit(h, null, i);
79                 return;
80             }
81             if (i==0) h.start(tag, location);
82
83             if (i==tokens.length) {
84                 h.finish(tag, location);
85
86             } else if (unwrap && i==tokens.length-1) {
87                 if (tokens[i] != null)
88                     tokens[i].visit(h, null, 0);
89
90             } else {
91                 tokens[i].visit(new TreeMaker<T>(h.toss) {
92                     public void start(T head, Input.Location loc) { }
93                     public void addTree(Tree<T> t) { toks.add(t); }
94                     public void finish(T head, Input.Location loc) {
95                         int old = h.toks.size();
96                         h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));
97                         expand(i+1, h);
98                         while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
99                     }
100                 }, null, null);
101             }
102         }
103
104         protected String  headToString()         { return null; }
105         protected String  headToJava()           { return null; }
106         protected String  left()                 { return "{"; }
107         protected String  right()                { return "}"; }
108         protected boolean ignoreSingleton()      { return false; }
109     }
110
111
112     // Ref //////////////////////////////////////////////////////////////////////////////
113
114     /**
115      *  This class represents a partially complete collection of
116      *  forests to be viewed as a forest at some later date; once
117      *  viewed, it becomes immutable
118      */
119     static class Ref<T> extends Forest<T> {
120         private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
121         public Ref() { }
122         public void merge(Forest p) { if (p!=this) hp.add(p, true); }
123         public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
124             if (hp==null) return;
125             for(Forest<T> f : hp)
126                 f.visit(ivbc, b, c);
127         }
128         public Forest resolve() { return this; }
129     }
130
131     public abstract <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
132     private static class TreeMaker2<T> extends TreeMaker<T> {
133         private TreeConsumer<T> tc;
134         public TreeMaker2(boolean toss, TreeConsumer<T> tc) { super(toss); this.tc = tc; }
135         public void finish(T head, Input.Location loc) { tc.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));; }
136         public void start(T head, Input.Location loc) { }
137         public void addTree(Tree<T> t) { toks.add(t); }
138     }
139     private static abstract class TreeMaker<T> implements Invokable<Forest.Body<T>,Boolean,Integer>, TreeConsumer<T> {
140         public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
141         private boolean toss;
142         protected T head;
143         public TreeMaker(boolean toss) { this.toss = toss; }
144         public abstract void start(T head, Input.Location loc);
145         public abstract void finish(T head, Input.Location loc);
146         public abstract void addTree(Tree<T> t);
147         public void invoke(Forest.Body<T> bod, Boolean o, Integer i) {
148             if (i==null) {
149                 ArrayList<Tree<T>> toks = this.toks;
150                 this.toks = new ArrayList<Tree<T>>();
151                 bod.expand(0, this);
152                 this.toks = toks;
153             } else {
154                 bod.expand(i, this);
155             }
156         }
157     }
158
159     // Statics //////////////////////////////////////////////////////////////////////////////
160
161     private static Tree[] tree_hint = new Tree[0];
162     private static final Forest[] emptyForestArray = new Forest[0];
163
164     protected String  headToString()    { return null; }
165     protected String  headToJava()      { return null; }
166     protected String  left()            { return "<?"; }
167     protected String  right()           { return "?>"; }
168     protected boolean ignoreSingleton() { return true; }
169 }