de044d5a47ee6a51aef1d2dac3de1986e7bb5d6f
[sbp.git] / src / edu / berkeley / sbp / meta / AnnotationGrammarBindingResolver.java
1 package edu.berkeley.sbp.meta;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.bind.*;
7 import java.util.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
10 import java.io.*;
11
12 // FIXME: non-static methods
13 public class AnnotationGrammarBindingResolver extends GrammarBindingResolver {
14
15     private static boolean harsh = true;
16
17     private final Class _cl;
18     private HashMap<String,Class[]> _inner = new HashMap<String,Class[]>();
19     private HashMap<String,Method[]> _allMethods = new HashMap<String,Method[]>();
20
21     public AnnotationGrammarBindingResolver(Class c) {
22         this._cl = c;
23         add(c, "");
24     }
25
26     public void add(Class c, String prefix) {
27
28         ArrayList<Class> alc = new ArrayList<Class>();
29         if (_inner.get(prefix) != null)
30             for(Class cc : _inner.get(prefix))
31                 alc.add(cc);
32
33         ArrayList<Method> alm = new ArrayList<Method>();
34         if (_allMethods.get(prefix) != null)
35             for(Method m : _allMethods.get(prefix))
36                 alm.add(m);
37
38         add(c, alc, alm, prefix);
39         this._inner.put(prefix, (Class[])alc.toArray(new Class[0]));
40         this._allMethods.put(prefix, (Method[])alm.toArray(new Method[0]));
41     }
42
43     public Object repeatTag() { return new Tree.ArrayBuildingTreeFunctor<Object>(); }
44
45     public Sequence tryResolveTag(Production p) {
46
47         String key = p.tag==null?p.nonTerminal:p.tag;
48         if (key==null) return null;
49
50         String prefix = key.indexOf('.')==-1 ? "" : key.substring(0, key.lastIndexOf('.'));
51         String suffix = key.indexOf('.')==-1 ? key : key.substring(key.lastIndexOf('.')+1);
52
53         p = new Production(suffix, p.elements, p.drops);
54         for(Method m : _allMethods.get(prefix))
55             if (p.isCompatible(m))
56                 return p.makeSequence(m);
57         for(Class c : _inner.get(prefix))
58             for(Constructor con : c.getConstructors())
59                 if (p.isCompatible(con))
60                     return p.makeSequence(con);
61         for(Class c : _inner.get(prefix))
62             if (p.isCompatible(c))
63                 return p.makeSequence(c);
64
65         return null;
66
67     }
68
69     public Sequence resolveTag(Production p) {
70         Sequence ret = tryResolveTag(p);
71         if (ret != null) return ret;
72         String message = "could not find a Java method/class/ctor matching tag \""+p.tag+
73             "\", nonterminal \""+"???"+"\" with " + p.elements.length + " arguments";
74         if (harsh) {
75             throw new RuntimeException(message);
76         } else {
77             return Sequence.rewritingSequence(p.tag, p.elements, p.drops);
78         }
79     }
80
81     // helper
82
83     private static void add(Class cl, ArrayList<Class> alc, ArrayList<Method> alm, String prefix) {
84         if (cl==null) return;
85         for(Method m : cl.getDeclaredMethods())
86             alm.add(m);
87         for(Class c : cl.getDeclaredClasses()) {
88             alc.add(c);
89             add(c, alc, alm, prefix);
90         }
91         if (cl.getSuperclass() != Object.class)
92             add(cl.getSuperclass(), alc, alm, prefix);
93     }
94
95 }