- return false;
- }
- public int[] buildSequence(Production p, String[] names, 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[argTagged];
- int j = 0;
- for(int i=0; i<argtags.length; i++)
- if (argtags[i]!=null)
- ret[j++] = i;
- return ret;
- } else {
- return null;
- }
- }
- }
- public static class TargetClass extends Target {
- public final Class _class;
- public TargetClass(Class _class) { this._class = _class; }
- public String getName() { return _class.getSimpleName(); }
- public tag getTag() { return (tag)_class.getAnnotation(tag.class); }
- public nonterminal getNonTerminal() { return (nonterminal)_class.getAnnotation(nonterminal.class); }
- public boolean buildSequence(Production p) {
- Field[] f = _class.getDeclaredFields();
- String[] names = new String[f.length];
- arg[] argtags = new arg[f.length];
- for(int i=0; i<f.length; i++) {
- names[i] = f[i].getName();
- argtags[i] = f[i].getAnnotation(arg.class);
- }
- int[] ret = buildSequence(p, names, argtags);
- if (ret!=null) return true;
- for(Constructor c : _class.getConstructors())
- if (new TargetConstructor(c).buildSequence(p))
- return true;
- return false;
- }
- public Sequence makeSequence(Production p) {
- return Sequence.rewritingSequence(_class, p.elements, p.labels, p.drops);
- }
- }
- public static class TargetConstructor extends Target {
- public final Constructor _ctor;
- public TargetConstructor(Constructor _ctor) { this._ctor = _ctor; }
- public String getName() { return _ctor.getName(); }
- public tag getTag() { return (tag)_ctor.getAnnotation(tag.class); }
- public nonterminal getNonTerminal() { return (nonterminal)_ctor.getAnnotation(nonterminal.class); }
- public boolean buildSequence(Production p) {
- Annotation[][] annotations = _ctor.getParameterAnnotations();
- int len = annotations.length;
- int ofs = 0;
- String name = _ctor.getDeclaringClass().getName();
- if (name.indexOf('$') > name.lastIndexOf('.')) {
- len--;
- ofs++;
- }
- String[] names = new String[len];
- arg[] argtags = new arg[len];
- for(int i=0; i<names.length; i++)
- for(Annotation a : annotations[i+ofs])
- if (a instanceof arg)
- argtags[i+ofs] = (arg)a;
- int[] ret = buildSequence(p, names, argtags);
- if (ret!=null) return true;
- return false;
- }
- public Sequence makeSequence(Production p) {
- return Sequence.rewritingSequence(_ctor, p.elements, p.labels, p.drops);
- }
- }
- public static class TargetMethod extends Target {
- public final Method _method;
- public TargetMethod(Method _method) { this._method = _method; }
- public String getName() { return _method.getName(); }
- public tag getTag() { return (tag)_method.getAnnotation(tag.class); }
- public nonterminal getNonTerminal() { return (nonterminal)_method.getAnnotation(nonterminal.class); }
- public boolean buildSequence(Production p) {
- Annotation[][] annotations = _method.getParameterAnnotations();
- String[] names = new String[annotations.length];
- arg[] argtags = new arg[annotations.length];
- for(int i=0; i<names.length; i++)
- for(Annotation a : annotations[i])
- if (a instanceof arg)
- argtags[i] = (arg)a;
- int[] ret = buildSequence(p, names, argtags);
- if (ret!=null) return true;
- return false;
- }
- public Sequence makeSequence(Production p) {
- return Sequence.rewritingSequence(_method, p.elements, p.labels, p.drops);
- }