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