-
- public static class JavaGrammar extends MetaGrammar {
- public Object convertLabel(String label) { return new ClassMark(label); }
- public Object convertFieldLabel(String label) { return new FieldMark(label); }
-
- private static class FieldMark {
- public final String field;
- public FieldMark(String field) { this.field = field; }
- public String toString() { return "."+field; }
- }
- private static class ClassMark {
- public final String clazz;
- public ClassMark(String clazz) { this.clazz = clazz; }
- public String toString() { return clazz+"$"; }
- }
-
- public static Object build(Tree<Object> t, Class c) throws Exception {
- System.out.println("** build " + c.getName() + " " + t.toPrettyString());
- Object h = t.head();
- if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
- Object o = null;
- if (t.numChildren()==0) o = t.head();
- else if (t.head()==null) return build2(t, c);
- else if (t.head() instanceof FieldMark) {
- return build2(new Tree(null, new Tree[] { t }), c);
- } else {
- throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
- }
- return Reflection.rebuild(o, c);
- }
- private static Object build2(Tree<Object> t, Class c) throws Exception {
- System.out.println("** build2 " + c.getName() + " " + t.toPrettyString());
- if (!Reflection.isConcrete(c)) {
- Field f = c.getField("subclasses");
- if (f==null) throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
- Class[] subs = (Class[])f.get(null);
- OUTER: for(int i=0; i<subs.length; i++) {
- for(Tree<Object> t2 : t) {
- Object o2 = t2.head();
- System.out.println("checking " + o2 + " in " + subs[i].getName());
- if (o2 instanceof FieldMark) {
- if (subs[i].getField(((FieldMark)o2).field)==null) continue OUTER;
- }
- }
- c = subs[i];
- break;
- }
- }
- Object o = c.newInstance();
- for(Tree<Object> t2 : t) {
- Object o2 = t2.head();
- if (o2 instanceof FieldMark) {
- FieldMark f = (FieldMark)o2;
- Field field = c.getField(ReflectiveWalker.mangle(f.field));
- Object tgt = build(t2.child(0), field.getType());
- System.out.println("setting " + f.field +
- " on a " + o.getClass().getName() +
- " to " + (tgt==null?"null":tgt.getClass().getName()));
- field.set(o, tgt);
- }
- }
- System.out.println("returning a " + o.getClass().getName());
- return o;
- }
- public static Object build(Tree<Object> t) throws Exception {
- Object h = t.head();
- if (h instanceof ClassMark) {
- ClassMark cm = (ClassMark)h;
- Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
- return build2(t, c);
- }
-
- return h==null ? null : h.toString();
- }
-
- public static interface Expr {
- public static Class[] subclasses = new Class[] { _plus_.class, num.class };
- }
- public static class _plus_ implements Expr {
- public Expr left;
- public Expr right;
- public String toString() { return left + "+" + right; }
- }
- public static class num implements Expr {
- public String val;
- public String toString() { return "["+val+"]"; }
- }
-
- }
-