import edu.berkeley.sbp.util.*;
import edu.berkeley.sbp.*;
import edu.berkeley.sbp.chr.*;
+import edu.berkeley.sbp.bind.*;
import java.util.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
}
}
-
- public static class bind {
- /**
- * Constructors, classes, and methods with this attribute will
- * match every production of the nonterminal called "value()"
- * that is arg-compatible. If value() is undefined, then the
- * class/constructor/method name is used.
- */
- /*
- @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
- */
-
- @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
-
- /**
- * Constructors, classes, and methods with this attribute will
- * match every tree tagged with "value()" that is arg-compatible.
- * If value() is undefined, then the class/constructor/method
- * name is used.
- */
-
- @Retention(RetentionPolicy.RUNTIME) public static @interface as { String value() default ""; }
-
-
- /**
- * If any parameter to a method or field in a class has a named
- * arg-tag, that parameter/field matches the child of the tree
- * which either has that label or else is a reference to a
- * nonterminal with the corresponding name.
- *
- * The remaining non-named arg-tags match the remaining children
- * of the tree in sequential order.
- *
- * If any arg-tagged parameters/fields remain, the match fails.
- * If there were no arg-tagged parameters-fields, it is as if all
- * of them were non-named and arg-tagged.
- *
- * A method/constructor is arg-compatible if all of its arguments
- * are arg-compatible.
- *
- * A class is arg-compatible if all of its fields are
- * arg-compatible, or if one of its constructors is arg-compatible.
- *
- */
- @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; }
- }
-
+
public static class Production {
public String tag;
public String nonTerminal;
}
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);
- }
+ 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 Reflection.Bindable _bindable;
public Target(Object o) { this(Reflection.Bindable.create(o)); }
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 Object plant(Object[] fields) { return _bindable.impose(fields); }
public boolean isRaw() { return _bindable.isAnnotationPresent(bind.raw.class); }
- public Object invokeRaw(Iterable<Tree<Object>> t) { return _bindable.impose(new Object[] { t }); }
public boolean isCompatible(Production p) {
bind.as t = getBindAs();
}
}
public Sequence makeSequence(Production p) {
- return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this),
+ return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this, _bindable, isRaw()),
p.elements, p.labels, p.drops);
}
- public class TargetReducer implements Tree.TreeFunctor<Object,Object> {
+ public static class TargetReducer implements Tree.TreeFunctor<Object,Object> {
private Production p;
private int[] map;
private String name;
- public TargetReducer(Production p, int[] map, String name) {
+ private Reflection.Bindable _bindable;
+ private boolean _israw;
+ public TargetReducer(Production p, int[] map, String name, Reflection.Bindable b, boolean raw) {
this.p = p;
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 invokeRaw(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)
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 plant(o2);
+ return _bindable.impose(o2);
}
}
}
+
+
public static Union cached = null;
public static Union make() {
if (cached != null) return cached;
return u;
}
+
+
public static class MG {
public static @bind.as("grammar") class Grammar {
public NonTerminal get(String s) {