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