- public static class ReflectiveMeta extends MetaGrammar.Meta {
- private final Class _cl;
- private final Class[] _inner;
- public ReflectiveMeta() {
- this(MG.class);
- }
- public ReflectiveMeta(Class c) {
- this._cl = c;
- this._inner = c.getDeclaredClasses();
- }
- public ReflectiveMeta(Class c, Class[] inner) {
- this._cl = c;
- this._inner = inner;
- }
- private boolean match(Method m, String s) { return match(m.getAnnotation(bind.as.class), null, s); }
- private boolean match(bind.as t, Class c, String s) {
- if (t==null) return false;
- if (t.value().equals(s)) return true;
- if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
- return false;
- }
- /*
- private boolean match(nonterminal t, Class c, String s) {
- if (t==null) return false;
- if (t.value().equals(s)) return true;
- if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
- return false;
- }
- */
- private boolean match(Class c, String s, String nonTerminalName) {
- if (match((bind.as)c.getAnnotation(bind.as.class), c, s)) return true;
- //if (match((nonterminal)c.getAnnotation(bind.as.class), c, nonTerminalName)) return true;
- return false;
- }
- public boolean match(Constructor con, String s, String nonTerminalName) {
- Class c = con.getDeclaringClass();
- if (match((bind.as)con.getAnnotation(bind.as.class), null, s)) return true;
- //if (match((nonterminal)con.getAnnotation(bind.as.class), c, s)) return true;
- return false;
- }
- public Object repeatTag() {
- return new Tree.ArrayBuildingTreeFunctor<Object>();
- }
- public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
- Production p = new Production(tag, nonTerminalName, els, labels, drops);
- for(Method m : _cl.getMethods())
- if (new Target(m).isCompatible(p))
- return new Target(m).makeSequence(p);
- for(Class c : _inner)
- for(Constructor con : c.getConstructors())
- if (new Target(con).isCompatible(p))
- return new Target(con).makeSequence(p);
- for(Class c : _inner)
- if (new Target(c).isCompatible(p))
- return new Target(c).makeSequence(p);
- return null;
- }
- public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
- Sequence ret = tryResolveTag(tag, nonTerminalName, els, labels, drops);
- if (ret != null) return ret;
- String message = "could not find a Java method/class/ctor matching tag \""+tag+
- "\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments";
- if (harsh) {
- throw new RuntimeException(message);
- } else {
- System.err.println(message);
- return Sequence.rewritingSequence(tag, els, labels, drops);
- }
- }
- }
-
-
- public static class Production {
- public String tag;
- public String nonTerminal;
- public Object[] labels;
- public boolean[] drops;
- public Element[] elements;
- public int count = 0;
- public Production(String tag, String nonTerminal, Element[] elements, Object[] labels, boolean[] drops) {
- this.tag = tag;
- this.elements = elements;
- this.nonTerminal = nonTerminal;
- this.labels = labels;
- this.drops = drops;
- for(int i=0; i<drops.length; i++)
- if (!drops[i])
- count++;
- }
- }
-
- public static class Target {
- public int[] buildSequence(Production p) {
- Annotation[][] annotations = _bindable.getArgAnnotations();
- String[] names = _bindable.getArgNames();
- String name = _bindable.getSimpleName();
- int len = annotations.length;
- int ofs = 0;
- bind.arg[] argtags = new bind.arg[len];
- for(int i=0; i<names.length; i++)
- for(Annotation a : annotations[i+ofs])
- if (a instanceof bind.arg)
- argtags[i+ofs] = (bind.arg)a;
- return Target.this.buildSequence(p, names, argtags);
- }
- private Bindable _bindable;
-
- public Target(Object o) { this(Bindable.create(o)); }
- public Target(Bindable b) { this._bindable = b; }
-
- public String getName() { return _bindable.getSimpleName(); }
- public bind.as getBindAs() { return (bind.as)_bindable.getAnnotation(bind.as.class); }
- //public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(bind.as.class); }
- public String toString() { return _bindable.getSimpleName(); }
- public boolean isRaw() { return _bindable.isAnnotationPresent(bind.raw.class); }
-
- public boolean isCompatible(Production p) {
- bind.as t = getBindAs();
- if (t != null &&
- (t.value().equals(p.tag) ||
- (t.value().equals("") && getName().equals(p.tag))))
- return buildSequence(p)!=null;
-
- bind.as n = getBindAs();
- if (n != null &&
- (n.value().equals(p.nonTerminal) ||
- (n.value().equals("") && getName().equals(p.nonTerminal))))
- return buildSequence(p)!=null;
-
- return false;
- }
-
- public int[] buildSequence(Production p, String[] names, bind.arg[] argtags) {
- int argTagged = 0;
- for(int i=0; i<argtags.length; i++)
- if (argtags[i] != null)
- argTagged++;
-
- // FIXME: can be smarter here
- if (names.length==p.count) {
- int[] ret = new int[p.count];
- for(int i=0; i<p.count; i++) ret[i] = i;
- return ret;
- } else if (argTagged==p.count) {
- int[] ret = new int[argtags.length];
- int j = 0;
- for(int i=0; i<argtags.length; i++)
- ret[i] = argtags[i]==null ? -1 : (j++);
- return ret;
- } else {
- return null;
- }
- }
- public Sequence makeSequence(Production p) {
- return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this, _bindable, isRaw()),
- p.elements, p.labels, p.drops);
- }
-
- }
-
- public static class TargetReducer implements Tree.TreeFunctor<Object,Object> {
- private int[] map;
- private String name;
- private Bindable _bindable;
- private boolean _israw;
- public TargetReducer(Production p, int[] map, String name, Bindable b, boolean raw) {
- this.map = map;
- this.name = name;
- this._bindable = b;
- this._israw = raw;
- }
- public String toString() { return name; }
- public Object invoke(Iterable<Tree<Object>> t) {
- if (_israw) return _bindable.impose(new Object[] { t });
- ArrayList ret = new ArrayList();
- for(Tree tc : t) {
- if (tc.head() != null && tc.head() instanceof Functor)
- ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
- else if (tc.numChildren() == 0)
- ret.add(tc.head());
- else {
- System.err.println("FIXME: don't know what to do about " + tc);
- ret.add(null);
- }
- }
- System.err.println("input tree: " + t);
- Object[] o = (Object[])ret.toArray(new Object[0]);
- int max = 0;
- for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
- Object[] o2 = new Object[max+1];
- for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
- return _bindable.impose(o2);