public TreeBindable(Bindable b) { this._bindable = b; }
private int[] buildSequence(Production p) {
- Annotation[][] annotations = _bindable.getArgAnnotations();
- Class[] types = _bindable.getArgTypes();
- 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 TreeBindable.this.buildSequence(p, names, argtags, types);
+ return p.buildSequence(_bindable);
}
public String getName() { return _bindable.getSimpleName(); }
return false;
}
- private int[] buildSequence(Production p, String[] names, bind.arg[] argtags, Class[] types) {
- int argTagged = 0;
- boolean hasloc = types.length>0 && types[0]==Input.Region.class;
- for(int i=0; i<argtags.length; i++) {
- if (i==0 && types[0]==Input.Region.class) continue;
- if (argtags[i] != null)
- argTagged++;
- }
- int numNames = names.length;
- if (hasloc) numNames--;
-
- // FIXME: can be smarter here
- if (argTagged==p.count) {
- int[] ret = new int[argtags.length];
- int j = 0;
- for(int i=0; i<argtags.length; i++) {
- if (i==0 && types[0]==Input.Region.class) continue;
- if (argtags[i]==null) continue;
- if (argtags[i].value().equals(""))
- ret[i] = j++;
- else {
- ret[i] = -1;
- for(int k=0; k<names.length; k++)
- if (argtags[i].value().equals(names[k])){
- ret[i] = k;
- break;
- }
- if (ret[i]==-1) return null;
- }
- }
- return ret;
- } else if (numNames==p.count) {
- int[] ret = new int[p.count];
- for(int i=0; i<p.count; i++) ret[i] = i+(hasloc?1:0);
- return ret;
- } else {
- return null;
- }
- }
public Sequence makeSequence(final Production p) {
if (_bindable.getArgTypes().length > 0 &&
_bindable,
isRaw()) {
public Object invoke(Iterable<Tree<Object>> t) {
- if (_israw) return _bindable.impose(new Object[] { t });
ArrayList ret = new ArrayList();
ret.add(region);
for(Tree tc : t) {
p.drops);
}
+ if (isRaw())
+ return Sequence.rewritingSequence(new Tree.BindingFunctor(_bindable.createBinding()),
+ p.elements,
+ p.drops);
return Sequence.rewritingSequence(new TreeBindableReducer(buildSequence(p),
- _bindable,
- isRaw()),
+ _bindable),
p.elements,
p.drops);
}
public static class TreeBindableReducer implements Tree.TreeFunctor<Object,Object>, ToJava {
int[] map;
Bindable _bindable;
- boolean _israw;
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(", ");
- sb.append(_israw ? "true" : "false");
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;
- this._israw = raw;
}
public String toString() { return "reducer-"+_bindable.toString(); }
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)
}
}
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);
+ return _bindable.createBinding(map).invoke(o);
}
}