--- /dev/null
+package edu.berkeley.sbp.meta;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.chr.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.bind.*;
+import java.util.*;
+import java.lang.annotation.*;
+import java.lang.reflect.*;
+import java.io.*;
+
+// FIXME: non-static methods
+public class AnnotationGrammarBindings extends Grammar.Bindings {
+
+ private static boolean harsh = true;
+
+ private final Class _cl;
+ private HashMap<String,Class[]> _inner = new HashMap<String,Class[]>();
+ private HashMap<String,Method[]> _allMethods = new HashMap<String,Method[]>();
+
+ public AnnotationGrammarBindings(Class c) {
+ this._cl = c;
+ add(c, "");
+ }
+
+ public void add(Class c, String prefix) {
+
+ ArrayList<Class> alc = new ArrayList<Class>();
+ if (_inner.get(prefix) != null)
+ for(Class cc : _inner.get(prefix))
+ alc.add(cc);
+
+ ArrayList<Method> alm = new ArrayList<Method>();
+ if (_allMethods.get(prefix) != null)
+ for(Method m : _allMethods.get(prefix))
+ alm.add(m);
+
+ add(c, alc, alm, prefix);
+ this._inner.put(prefix, (Class[])alc.toArray(new Class[0]));
+ this._allMethods.put(prefix, (Method[])alm.toArray(new Method[0]));
+ }
+
+ public Object repeatTag() { return new Tree.ArrayBuildingTreeFunctor<Object>(); }
+
+ public Sequence tryResolveTag(Production p) {
+
+ String key = p.tag==null?p.nonTerminal:p.tag;
+ if (key==null) return null;
+
+ String prefix = key.indexOf('.')==-1 ? "" : key.substring(0, key.lastIndexOf('.'));
+ String suffix = key.indexOf('.')==-1 ? key : key.substring(key.lastIndexOf('.')+1);
+
+ p = new Production(suffix, p.elements, p.drops);
+ for(Method m : _allMethods.get(prefix))
+ if (p.isCompatible(m))
+ return p.makeSequence(m);
+ for(Class c : _inner.get(prefix))
+ for(Constructor con : c.getConstructors())
+ if (p.isCompatible(con))
+ return p.makeSequence(con);
+ for(Class c : _inner.get(prefix))
+ if (p.isCompatible(c))
+ return p.makeSequence(c);
+
+ return null;
+
+ }
+
+
+ // helper
+
+ private static void add(Class cl, ArrayList<Class> alc, ArrayList<Method> alm, String prefix) {
+ if (cl==null) return;
+ for(Method m : cl.getDeclaredMethods())
+ alm.add(m);
+ for(Class c : cl.getDeclaredClasses()) {
+ alc.add(c);
+ add(c, alc, alm, prefix);
+ }
+ if (cl.getSuperclass() != Object.class)
+ add(cl.getSuperclass(), alc, alm, prefix);
+ }
+
+}