import java.util.*;
import java.io.*;
-public class MetaGrammar extends ReflectiveWalker {
+public class MetaGrammar extends StringWalker {
public static Union make() throws Exception {
return ((MetaGrammar)new MetaGrammar().walk(meta)).done();
return n;
}
+ public String string(Iterable<Tree<String>> children) {
+ String ret = "";
+ for(Tree<String> t : children) ret += string(t);
+ return ret;
+ }
public String string(Tree<String> tree) {
String ret = "";
if (tree.head()!=null) ret += tree.head();
- for(Tree<String> t : tree.children()) ret += string(t);
+ ret += string(tree.children());
return ret;
}
}
+ public Object walk(String tag, Object[] argo) {
+ if (argo.length==0) return super.walk(tag, argo);
+ if (argo==null) return tag;
+ if (tag==null || "".equals(tag)) return argo;
+ return super.walk(tag, argo);
+ }
+
//////////////////////////////////////////////////////////////////////////////
public class PreSequence {
}
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) throws IOException {
- return new TestCase(input, output, grammar, false); }
- public TestCase testcase(String input, Union grammar) throws IOException {
- return new TestCase(input, new String[0], grammar, false); }
- public TestCase tibcase(String input, String[] output, Union grammar) throws IOException {
- return new TestCase(input, output, grammar, true); }
- public TestCase tibcase(String input, Union grammar) throws IOException {
- return new TestCase(input, new String[0], grammar, true); }
- public MetaGrammar grammar(Object[] o) { return this; }
-
public Object walk(Tree<String> tree) {
- if ("grammar".equals(tree.head())) { walkChildren(tree); return done("s"); }
- else return super.walk(tree);
+ try {
+ if ("grammar".equals(tree.head())) { walkChildren(tree); return done("s"); }
+ else if ("output".equals(tree.head())) return string(tree.children());
+ else if ("input".equals(tree.head())) return string(tree.children());
+ else if ("tibcase".equals(tree.head()) || "testcase".equals(tree.head())) {
+ String input = string(tree.child(0));
+ String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
+ Union grammar = (Union)walk(tree, tree.numChildren()-1);
+ return new TestCase(input, output, grammar, "tibcase".equals(tree.head()));
+ } else if ("ts".equals(tree.head())) return walk(tree, 0);
+ else return super.walk(tree);
+ } catch (Exception e) {
+ throw new Error(e);
+ }
}
}