- public static class TestCaseBuilder extends MetaGrammar {
- public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
- public TestCase[] ts(TestCase[] ts) { return ts; }
- public TestCase testcase(String input, String[] output, Union grammar) { return new TestCase(input, output, grammar); }
- public MetaGrammar grammar(Object[] o) { return this; }
- public Object walk(String tag, Object[] args) {
- if ("testcase".equals(tag)) {
- if (args.length==2) return testcase((String)args[0], new String[0], (Union)args[1]);
- return testcase((String)args[0], (String[])args[1], (Union)args[2]); }
- else if ("grammar".equals(tag)) return done("s");
- else return super.walk(tag, args);
+ public static class TestCaseBuilder extends StringWalker {
+ public Object walk(Tree<String> tree) {
+ try {
+ if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
+ else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
+ else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
+ else if ("testcase".equals(tree.head())) {
+ String input = MetaGrammar.string(tree.child(0));
+ String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
+ Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
+ TestCase tc = new TestCase(input, output, grammar, false, false);
+ return tc;
+ } else if ("ts".equals(tree.head())) return walk(tree, 0);
+ else if (tree.head() == null) {
+ Object[] ret = new Object[tree.numChildren()];
+ for(int i=0; i<ret.length; i++)
+ ret[i] = walk(tree.child(i));
+ return Reflection.lub(ret);
+ }
+ return super.walk(tree);
+ } catch (Exception e) {
+ throw new Error(e);
+ }