checkpoint
authoradam <adam@megacz.com>
Thu, 22 Dec 2005 02:55:33 +0000 (21:55 -0500)
committeradam <adam@megacz.com>
Thu, 22 Dec 2005 02:55:33 +0000 (21:55 -0500)
darcs-hash:20051222025533-5007d-3829ae63ff60fea9ada5b0842802ced548a9d895.gz

src/edu/berkeley/sbp/misc/MetaGrammar.java
src/edu/berkeley/sbp/misc/RegressionTests.java
tests/testcase.g

index 2751db7..3501ee6 100644 (file)
@@ -4,7 +4,7 @@ import edu.berkeley.sbp.*;
 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();
@@ -71,10 +71,15 @@ public class MetaGrammar extends ReflectiveWalker {
         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;
     }
 
@@ -148,6 +153,13 @@ public class MetaGrammar extends ReflectiveWalker {
     }
 
 
+    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 {
index 74eecd8..943c611 100644 (file)
@@ -123,21 +123,21 @@ public class RegressionTests {
     }
 
     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);
+            }
         }
     }
 
index 33c00b7..175f1e3 100644 (file)
@@ -5,5 +5,5 @@ test     ::= ^"testcase" "{" input output +/ ws   grammar "}" /ws
            | ^"testcase" "{" input                grammar "}" /ws
            | ^"tibcase"  "{" input output +/ ws   grammar "}" /ws
            | ^"tibcase"  "{" input                grammar "}" /ws
-output   ::= "output" quoted ";" /ws
+output   ::= ^"output" quoted ";" /ws
 input    ::= "input"  quoted ";" /ws