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