move TestCase junk out of GrammarAST
[sbp.git] / src / edu / berkeley / sbp / meta / GrammarAST.java
index 0ea8ae3..b52335e 100644 (file)
@@ -42,19 +42,18 @@ public class GrammarAST {
     // Methods //////////////////////////////////////////////////////////////////////////////
 
     private Union buildGrammar(Tree t, String rootNonTerminal) {
-        return ((GrammarAST.GrammarNode)walk(t)).build(rootNonTerminal);
+        Object o = walk(t);
+        if (o instanceof Union) return (Union)o;
+        return ((GrammarAST.GrammarNode)o).build(rootNonTerminal);
     }
 
-    public Object[] walkChildren(Tree t) {
+    private Object[] walkChildren(Tree t) {
         Object[] ret = new Object[t.size()];
-        for(int i=0; i<ret.length; i++) {
+        for(int i=0; i<ret.length; i++)
             ret[i] = walk(t.child(i));
-            if (ret[i] instanceof Object[])
-                ret[i] = Reflection.lub((Object[])ret[i]);
-        }
         return Reflection.lub(ret);
     }
-    private String stringifyChildren(Tree t) {
+    private static String stringifyChildren(Tree t) {
         StringBuffer sb = new StringBuffer();
         for(int i=0; i<t.size(); i++) {
             sb.append(t.child(i).head());
@@ -62,7 +61,7 @@ public class GrammarAST {
         }
         return sb.toString();
     }
-    private String unescape(Tree t) {
+    private static String unescape(Tree t) {
         StringBuffer sb = new StringBuffer();
         for(int i=0; i<t.size(); i++)
             sb.append(t.child(i).head()+stringifyChildren(t.child(i)));
@@ -119,16 +118,15 @@ public class GrammarAST {
         if (head.equals("<<"))  return new DropNode(new AtomNode(new char[] { CharAtom.right, CharAtom.right }));
         if (head.equals("~"))   return new TildeNode(walkElement(t.child(0)));
         if (head.equals("~~"))  return new Seq(new RepeatNode(new TildeNode(new AtomNode()), null, true,  true,  false)).andnot(walkSeq(t.child(0)));
-        if (head.equals("Range") && t.size()==2 && ">".equals(t.child(0).head()))
-            return new char[] { CharAtom.left, CharAtom.left };
-        if (head.equals("Range") && t.size()==2 && "<".equals(t.child(0).head()))
-            return new char[] { CharAtom.right, CharAtom.right };
-        if (head.equals("Range") && t.size()==1) return new char[] { unescape(t).charAt(0), unescape(t).charAt(0) };
-        if (head.equals("Range")) return new char[] { unescape(t).charAt(0), unescape(t).charAt(1) };
+        if (head.equals("Range")) {
+            if (t.size()==2 && ">".equals(t.child(0).head())) return new char[] { CharAtom.left, CharAtom.left };
+            if (t.size()==2 && "<".equals(t.child(0).head())) return new char[] { CharAtom.right, CharAtom.right };
+            if (t.size()==1) return new char[] { unescape(t).charAt(0), unescape(t).charAt(0) };
+            return new char[] { unescape(t).charAt(0), unescape(t).charAt(1) };
+        }
         if (head.equals("\"\"")) return "";
-        if (head.equals("\n")) return "\n";
-        if (head.equals("\r")) return "\r";
-        if (head.equals("grammar.Grammar")) return walkChildren(t);
+        if (head.equals("\n"))   return "\n";
+        if (head.equals("\r"))   return "\r";
         if (head.equals("SubGrammar")) return GrammarAST.buildFromAST(t.child(0), "s", includes);
         if (head.equals("NonTerminal"))
             return new NonTerminalNode(walkString(t.child(0)),
@@ -141,13 +139,6 @@ public class GrammarAST {
                     seq[i] = seq[i].tag(tag);
             return new NonTerminalNode(tag, seqs, false, null, false);
         }
-        if (head.equals("TestCase"))
-            return new RegressionTests.TestCase(walkString(t.child(0)),
-                                                walkString(t.child(1)),
-                                                (String[])Reflection.coerce(walkChildren(t.child(2)), String[].class),
-                                                (Union)walk(t.child(3)),
-                                                false,
-                                                false);
         if (head.equals("#import")) {
             String fileName = (String)stringifyChildren(t.child(0));
             for(File f : includes) {