checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / ReflectiveGrammar.java
1 package edu.berkeley.sbp.misc;
2 import java.io.*;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.tib.*;
8 import edu.berkeley.sbp.chr.*;
9 import edu.berkeley.sbp.util.*;
10
11 public class ReflectiveGrammar /*extends MetaGrammar*/ {
12     /*
13     final Class baseClass;
14     public ReflectiveGrammar(Class baseClass) { this.baseClass = baseClass; }
15
16     public Object convertLabel(String label) { return new ClassMark(label); }
17     public Object repeatTag() { return arrayMark; }
18
19     private static class ArrayMark { }
20     private static final ArrayMark arrayMark = new ArrayMark();
21
22     private static class ClassMark {
23         public final String clazz;
24         public ClassMark(String clazz) { this.clazz = clazz; }
25         public String toString() { return clazz+"$"; }
26     }
27
28     public String stringify(Tree<Object> t) throws Exception {
29         StringBuffer ret = new StringBuffer();
30         for(int i=0; i<t.numChildren(); i++) {
31             Tree<Object> child = t.child(i);
32             Object head = child.numChildren() > 0 ? buildHead(child, String.class) : child.head();
33             if (head!=null) ret.append(head);
34         }
35         return ret.toString();
36     }
37
38     public Object build(Tree<Object> t) throws Exception { return buildHead(t, null); }
39     public Object buildHead(Tree<Object> t, Class c) throws Exception {
40         //System.out.println("buildHead " + (c==null?null:c.getName()) + " " + t);
41         Object h = t.head();
42
43         if (h != null && h instanceof ClassMark) {
44             String clazz = ReflectiveWalker.mangle(((ClassMark)h).clazz);
45             Class c2 = (c!=null && c.getName().endsWith("$"+clazz)) ? c : Reflection.forNameOrNull(baseClass.getName()+"$"+clazz);
46             if (c2!=null) return buildBody(t, c2);
47             for(Method m : baseClass.getMethods()) {
48                 if (!m.getName().equals(clazz)) continue;
49                 Object[] o = new Object[m.getParameterTypes().length];
50                 for(int i=0; i<o.length; i++)
51                     o[i] = i>=t.numChildren() ? null : buildHead(t.child(i), m.getParameterTypes()[i]);
52                 return m.invoke(null, o);
53             }
54             throw new RuntimeException("couldn't figure out what to invoke for ClassMark " + clazz);
55         } else if (h==arrayMark && c==null) {
56             c = Object[].class;            
57         }
58         if (c.isArray()) {
59             Object[] ret = new Object[t.numChildren()];
60             for(int i=0; i<ret.length; i++)
61                 ret[i] = buildHead(t.child(i), c.getComponentType());
62             return Reflection.lub(ret);
63         }
64         if (c==String.class) return stringify(t);
65         if (c==int.class)    { String s = stringify(t); if (s==null||"".equals(s)) return new Integer(0); return new Integer(s); }
66         if (h==null)         return buildBody(t, c);
67
68         if (t.numChildren() > 0)
69             throw new RuntimeException("can't buildHead() on a tree with children when the head is of type " +
70                                        h.getClass().getName() + "(c=="+(c==null?"null":c.getName())+")");
71         return h;
72     }
73
74     public Object buildBody(Tree<Object> t, Class c) throws Exception {
75         //System.out.println("buildBody " + (c==null?null:c.getName()) + " " + t);
76         c = resolveClass(t, c);
77         if (c==null) return buildHead(t, null);
78         Object o = c.newInstance();
79         Field[] f = c.getFields();
80         OUTER: for(int i=0; i<t.numChildren(); i++) {
81             Object label = t.label(i);
82             Field field = null;
83             if (label!=null) field = Reflection.getField(c, label+"");
84             if (field==null && label != null)
85                 for(Method m : c.getMethods())
86                     if (m.getName().equals(label)) {
87                         m.invoke(o, new Object[] { buildHead(t.child(i), m.getParameterTypes()[0]) });
88                         continue OUTER;
89                     }
90             if (field==null && label==null) {
91                 field = Reflection.getField(c, 0);
92             }
93             if (field==null) {
94                 System.err.println("warning: skipping field " + label + " ("+i+") on class " + c.getName());
95                 System.err.println("   tree: " + t);
96             }
97             else {
98                 Object tgt = Reflection.rebuild(buildHead(t.child(i), field.getType()), field.getType());
99                 //if (tgt instanceof Object[]) tgt = Reflection.lub(tgt);
100                 //System.err.println("setting field " + field.getName() + " on " + c.getName() + " to " + tgt);
101                 try {
102                     field.set(o, tgt);
103                 } catch (Exception e) {
104                     e.printStackTrace();
105                 }
106             }
107         }
108         return o;
109     }
110
111     public Class resolveClass(Tree<Object> t, Class c) throws Exception {
112         if (c==null) return null;
113         if (c==int.class) return c;
114         if (c==String.class) return c;
115         //System.out.println("resolving " + c.getName());
116         if (Reflection.isConcrete(c)) return c;
117         Class ret = null;
118         Class[] subs = (Class[])c.getField("subclasses").get(null);
119         OUTER: for(int i=0; i<subs.length; i++) {
120             //System.err.println("trying " + subs[i].getName());
121             for(int j=0; j<t.numChildren(); j++)
122                 if (Reflection.getField(subs[i], t.label(j)+"")==null) {
123                     //System.err.println("skipping due to " + t.label(j));
124                     continue OUTER;
125                 }
126             if (ret != null)
127                 throw new RuntimeException("couldn't decide between two classes:\n  " + subs[i].getName() + "\n  " + ret.getName());
128             ret = subs[i];
129         }
130         if (t.head()==null) return null;
131         if (ret==null) throw new RuntimeException("couldn't find a class to match tree: " + t);
132         return ret;
133     }
134     */
135 }