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