demo: edu.berkeley.sbp.jar
$(java) -cp $< edu.berkeley.sbp.misc.Demo \
- tests/meta.g \
- tests/meta.g
+ tests/demo.g \
+ '(11+2*3)-44'
regress:
make boot
public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
MetaGrammar.Production p = new MetaGrammar.Production(tag, nonTerminalName, els, labels, drops);
+ if (labels != null && labels.length > 0 && "numeric".equals(labels[0]))
+ System.out.println("here! ");
for(Method m : _cl.getMethods())
if (new MetaGrammar.Target(m).isCompatible(p))
return new MetaGrammar.Target(m).makeSequence(p);
return buildSequence(p)!=null;
bind b = getBind();
- System.out.println(_bindable.getClass().getSimpleName() + ": " + _bindable.getSimpleName());
+ //System.out.println(_bindable.getClass().getSimpleName() + ": " + _bindable.getSimpleName());
if (b != null && getName().equals(p.tag))
return buildSequence(p)!=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) {
+ if (argTagged==p.count) {
int[] ret = new int[argtags.length];
int j = 0;
- for(int i=0; i<argtags.length; i++)
- ret[i] = argtags[i]==null ? -1 : (j++);
+ for(int i=0; i<argtags.length; i++) {
+ 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 (names.length==p.count) {
+ int[] ret = new int[p.count];
+ for(int i=0; i<p.count; i++) ret[i] = i;
return ret;
} else {
return null;
ret.add(null);
}
}
- System.err.println("input tree: " + t);
+ //System.err.println("input tree: " + t);
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);
}
public static Union make(Tree t, String s) { return make(t, s, new AnnotationGrammarBindingResolver()); }
public static Union make(Tree t, String s, GrammarBindingResolver rm) {
- System.out.println("Head: " + t.head());
Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
MetaGrammarBindings.Grammar g = (MetaGrammarBindings.Grammar)red.invoke(t.children());
Context cx = new Context(g,rm);
Union u = null;
for(MetaGrammarBindings.NonTerminal nt : g.nonterminals) {
- System.out.println(nt.name);
Union el = (Union)cx.get(nt.name);
StringBuffer st = new StringBuffer();
el.toString(st);
- System.err.println(st);
if (nt.name.equals(s)) u = el;
}
return u;
}
public static class NonTerminal extends Un {
public String name = null;
- public @bind NonTerminal(@bind.arg("Word") String name,
- @bind.arg("RHS") Seq[][] sequences) {
+ public @bind NonTerminal(@bind.arg String name,
+ @bind.arg Seq[][] sequences) {
this.name = name;
this.sequences = sequences;
}
public static String show(Object o) {
if (o==null) return "null";
- if (o instanceof Show) return ((Show)o).toString();
+ if (o instanceof Show) return show((Show)o);
if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
Object[] arr = (Object[])o;
StringBuffer ret = new StringBuffer();
if (f[i].getType() == String.class) tgt = stringify(tgt);
// FUGLY
tgt = coerce(tgt, f[i].getType());
- System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
+ //System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
f[i].set(ret, tgt);
}
return ret;
if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
// FUGLY
tgt = Reflection.coerce(tgt, argTypes[i]);
- System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
+ //System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
args[i] = tgt;
}
- System.err.println("invoking " + _method + " with " + Reflection.show(args));
+ //System.err.println("invoking " + _method + " with " + Reflection.show(args));
return _method.invoke(null, args);
} catch (Exception e) {
throw new RuntimeException(e);
if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
// FUGLY
tgt = Reflection.coerce(tgt, argTypes[i]);
- System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
+ //System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
args[i] = tgt;
}
return _ctor.newInstance(args);
if (c == char.class) {
return o.toString().charAt(0);
}
+ if (c==int.class || c==Integer.class) {
+ String s = (String)coerce(o, String.class);
+ return new Integer(Integer.parseInt(s));
+ }
if (o.getClass().isArray() &&
o.getClass().getComponentType().isArray() &&
for(int i=0; i<((Object[])o).length; i++) {
Object ob = (((Object[])o)[i]);
if (ob != null) {
- System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
+ //System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
ok = false;
}
}
if (ok) {
- System.err.println("hit with " + c.getComponentType().getName());
+ //System.err.println("hit with " + c.getComponentType().getName());
return Array.newInstance(c.getComponentType(), ((Object[])o).length);
}
}