p.flush();
os.close();
}
-
- public static class TreeBindable {
-
- private Bindable _bindable;
- public TreeBindable(Object o) { this(Bindable.create(o)); }
- public TreeBindable(Bindable b) { this._bindable = b; }
-
- private int[] buildSequence(Production p) {
- return p.buildSequence(_bindable);
- }
-
- public String getName() { return _bindable.getSimpleName(); }
- public bind getBind() { return (bind)_bindable.getAnnotation(bind.class); }
- public bind.as getBindAs() { return (bind.as)_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();
- bind b = getBind();
-
- if (t != null &&
- (t.value().equals(p.tag)))
- return buildSequence(p)!=null;
-
- if (t != null &&
- ((t.value().equals("") && getName().equals(p.tag))))
- return buildSequence(p)!=null;
- if (b != null && getName().equals(p.tag))
- return buildSequence(p)!=null;
-
- if (t != null &&
- (t.value().equals(p.tag)))
- return buildSequence(p)!=null;
- if (t != null &&
- ((t.value().equals("") && getName().equals(p.tag))))
- return buildSequence(p)!=null;
- if (b != null && getName().equals(p.tag))
- return buildSequence(p)!=null;
-
- return false;
- }
-
- public Sequence makeSequence(final Production p) {
-
- if (_bindable.getArgTypes().length > 0 &&
- _bindable.getArgTypes()[0] == Input.Region.class) {
- Functor<Input.Region,Object> func = new Functor<Input.Region,Object>() {
- public Object invoke(final Input.Region region) {
- return
- new TreeBindableReducer(buildSequence(p),
- _bindable,
- isRaw()) {
- public Object invoke(Iterable<Tree<Object>> t) {
- ArrayList ret = new ArrayList();
- ret.add(region);
- 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);
- }
- }
- 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+2];
- for(int i=0; i<o.length; i++) o2[map[i]+1] = o[i];
- o2[0] = region;
- return _bindable.impose(o2);
- }
- };
- }
- };
- return Sequence.regionRewritingSequence(func,
- p.elements,
- p.drops);
- }
-
- if (isRaw())
- return Sequence.rewritingSequence(new Tree.BindingFunctor(_bindable.createBinding()),
- p.elements,
- p.drops);
- return Sequence.rewritingSequence(new TreeBindableReducer(buildSequence(p),
- _bindable),
- p.elements,
- p.drops);
- }
-
- }
-
- public static class TreeBindableReducer implements Tree.TreeFunctor<Object,Object>, ToJava {
- int[] map;
- Bindable _bindable;
-
- public void toJava(StringBuffer sb) {
- sb.append("new MetaGrammar.TreeBindableReducer(new int[] {");
- for(int i=0; i<map.length; i++) sb.append((i+"")+(i<map.length-1 ? "," : ""));
- sb.append("}, ");
- _bindable.toJava(sb);
- sb.append(")");
- }
-
- public TreeBindableReducer(int[] map, Bindable b) { this(map,b, false); }
- public TreeBindableReducer(int[] map, Bindable b, boolean raw) {
- this.map = map;
- this._bindable = b;
- }
- public String toString() { return "reducer-"+_bindable.toString(); }
- public Object invoke(Iterable<Tree<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);
- }
- }
- Object[] o = (Object[])ret.toArray(new Object[0]);
- return _bindable.createBinding(map).invoke(o);
- }
- }
-
public static Union make() { return make(MetaGrammarTree.meta, "s"); }
public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver(MetaGrammarBindings.class)); }