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