checkpoint
authoradam <adam@megacz.com>
Sun, 2 Jul 2006 23:08:32 +0000 (19:08 -0400)
committeradam <adam@megacz.com>
Sun, 2 Jul 2006 23:08:32 +0000 (19:08 -0400)
darcs-hash:20060702230832-5007d-06c5b64a9c1a5ecf1abf090755c4f76009d0f4c1.gz

src/edu/berkeley/sbp/misc/Demo.java
src/edu/berkeley/sbp/misc/RegressionTests.java
src/edu/berkeley/sbp/misc/StringWalker.java
tests/regression.tc
tests/testcase.g

index 92c9194..6384939 100644 (file)
@@ -33,6 +33,15 @@ public class Demo {
         System.out.println(t.toPrettyString());
     }
 
         System.out.println(t.toPrettyString());
     }
 
+    public static class ReflectiveMetaPlain extends ReflectiveMeta {
+        public Object repeatTag() { return null; }
+        public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
+            return null; }
+        public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
+            return Sequence.rewritingSequence(tag, els, labels, drops);
+        }
+    }
+
     public static class ReflectiveMeta extends MetaGrammar.Meta {
         private final Class _cl;
         private final Class[] _inner;
     public static class ReflectiveMeta extends MetaGrammar.Meta {
         private final Class _cl;
         private final Class[] _inner;
@@ -47,7 +56,7 @@ public class Demo {
                      MG.Seq.class,
                      MG.NonTerminalReference.class,
                      MG.StringLiteral.class,
                      MG.Seq.class,
                      MG.NonTerminalReference.class,
                      MG.StringLiteral.class,
-                     MG.Tree.class,
+                     MG.XTree.class,
                      MG.CharClass.class
                  });
         }
                      MG.CharClass.class
                  });
         }
@@ -136,6 +145,8 @@ public class Demo {
      */ 
     @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
 
      */ 
     @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
 
+    @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
+
     /**
      *  Constructors, classes, and methods with this attribute will
      *  match every tree tagged with "value()" that is arg-compatible.
     /**
      *  Constructors, classes, and methods with this attribute will
      *  match every tree tagged with "value()" that is arg-compatible.
@@ -230,6 +241,8 @@ public class Demo {
             return Sequence.rewritingSequence(new TargetReducer(p), p.elements, p.labels, p.drops);
         }
         public abstract Object plant(Object[] fields, int[] map);
             return Sequence.rewritingSequence(new TargetReducer(p), p.elements, p.labels, p.drops);
         }
         public abstract Object plant(Object[] fields, int[] map);
+        public boolean isRaw() { return false; }
+        public Object invokeRaw(Tree t) { return null; }
         public class TargetReducer implements Reducer {
             private Production p;
             private int[] map;
         public class TargetReducer implements Reducer {
             private Production p;
             private int[] map;
@@ -239,6 +252,7 @@ public class Demo {
             }
             public String toString() { return "reducer-"+Target.this; }
             public Object reduce(Tree t) {
             }
             public String toString() { return "reducer-"+Target.this; }
             public Object reduce(Tree t) {
+                if (isRaw()) return invokeRaw(t);
                 Object[] objects = new Object[t.numChildren()];
                 for(int i=0; i<t.numChildren(); i++) {
                     Tree tc = t.child(i);
                 Object[] objects = new Object[t.numChildren()];
                 for(int i=0; i<t.numChildren(); i++) {
                     Tree tc = t.child(i);
@@ -379,6 +393,14 @@ public class Demo {
             int[] ret = buildSequence(p, names, argtags);
             return ret;
         }
             int[] ret = buildSequence(p, names, argtags);
             return ret;
         }
+        public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
+        public Object invokeRaw(Tree t) {
+            try {
+                return _method.invoke(null, new Object[] { t });
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
         public Object plant(Object[] fields, int[] map) {
             try {
                 Class[] argTypes = _method.getParameterTypes();
         public Object plant(Object[] fields, int[] map) {
             try {
                 Class[] argTypes = _method.getParameterTypes();
@@ -407,11 +429,28 @@ public class Demo {
         if (c == char.class) {
             return o.toString().charAt(0);
         }
         if (c == char.class) {
             return o.toString().charAt(0);
         }
+
+        if (o.getClass().isArray() &&
+            o.getClass().getComponentType().isArray() &&
+            o.getClass().getComponentType().getComponentType() == String.class &&
+            c.isArray() &&
+            c.getComponentType() == String.class) {
+            String[] ret = new String[((Object[])o).length];
+            for(int i=0; i<ret.length; i++) {
+                StringBuffer sb = new StringBuffer();
+                for(Object ob : (Object[])(((Object[])o)[i]))
+                    sb.append(ob);
+                ret[i] = sb.toString();
+            }
+            return ret;
+        }
+
         if (c.isArray() && (c.getComponentType().isInstance(o))) {
             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
             ret[0] = o;
             return ret;
         }
         if (c.isArray() && (c.getComponentType().isInstance(o))) {
             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
             ret[0] = o;
             return ret;
         }
+
         if (o.getClass().isArray() && c.isArray()) {
             boolean ok = true;
             for(int i=0; i<((Object[])o).length; i++) {
         if (o.getClass().isArray() && c.isArray()) {
             boolean ok = true;
             for(int i=0; i<((Object[])o).length; i++) {
@@ -429,13 +468,29 @@ public class Demo {
         return o;
     }
 
         return o;
     }
 
-    public static Union make(Tree t, String s) {
-        ReflectiveMeta rm = new ReflectiveMeta();
+    public static Union cached = null;
+    public static Union make() {
+        if (cached != null) return cached;
+        try {
+            ReflectiveMeta m = new ReflectiveMeta();
+            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream("tests/meta.g")).expand1();
+            MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
+            MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
+            Union meta = mgf.get("s").build(bc);
+            Tree t = new CharParser(meta).parse(new FileInputStream("tests/meta.g")).expand1();
+            return cached = make(t, "s");
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+    public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
+    public static Union make(Tree t, String s, ReflectiveMeta rm) {
         Reducer red = (Reducer)t.head();
         MG.Grammar g = (MG.Grammar)red.reduce(t);
         Context cx = new Context(g,rm);
         Union u = null;
         for(MG.NonTerminal nt : g.nonterminals) {
         Reducer red = (Reducer)t.head();
         MG.Grammar g = (MG.Grammar)red.reduce(t);
         Context cx = new Context(g,rm);
         Union u = null;
         for(MG.NonTerminal nt : g.nonterminals) {
+            System.out.println(nt.name);
             Union el = (Union)cx.get(nt.name);
             StringBuffer st = new StringBuffer();
             el.toString(st);
             Union el = (Union)cx.get(nt.name);
             StringBuffer st = new StringBuffer();
             el.toString(st);
@@ -624,10 +679,12 @@ public class Demo {
                 return cx.get(nonTerminal);
             }
         }
                 return cx.get(nonTerminal);
             }
         }
+
         public static class StringLiteral        extends Constant {
             public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); }
             public boolean drop() { return true; }
         }
         public static class StringLiteral        extends Constant {
             public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); }
             public boolean drop() { return true; }
         }
+
         public static                     class CharClass            extends El {
             Range[] ranges;
             public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; }
         public static                     class CharClass            extends El {
             Range[] ranges;
             public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; }
@@ -639,7 +696,7 @@ public class Demo {
             }
         }
 
             }
         }
 
-        public static @tag("{")           class Tree                 extends El {
+        public static @tag("{")           class XTree                 extends El {
             public @arg Seq body;
             public Element build(Context cx) {
                 throw new Error();
             public @arg Seq body;
             public Element build(Context cx) {
                 throw new Error();
index 7a8e502..b186b43 100644 (file)
@@ -7,6 +7,7 @@ import edu.berkeley.sbp.misc.*;
 import edu.berkeley.sbp.tib.*;
 import edu.berkeley.sbp.chr.*;
 import edu.berkeley.sbp.util.*;
 import edu.berkeley.sbp.tib.*;
 import edu.berkeley.sbp.chr.*;
 import edu.berkeley.sbp.util.*;
+import static edu.berkeley.sbp.misc.Demo.*;
 
 public class RegressionTests {
 
 
 public class RegressionTests {
 
@@ -30,12 +31,13 @@ public class RegressionTests {
             }
 
             System.err.println("parsing " + s[0]);
             }
 
             System.err.println("parsing " + s[0]);
-            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
-            Union meta = MetaGrammar.make(res, "s");
+            Tree<String> res = new CharParser(Demo.make()).parse(new FileInputStream(s[0])).expand1();
+            Union meta = Demo.make(res, "s");
             System.err.println("parsing " + s[1]);
             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
             res = new CharParser(meta).parse(sis).expand1();
             System.err.println("parsing " + s[1]);
             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
             res = new CharParser(meta).parse(sis).expand1();
-            Union testcasegrammar = MetaGrammar.make(res, "ts");
+
+            Union testcasegrammar = Demo.make(res, "ts", new TestCaseMaker());
             if (testcasegrammar==null) return;
             CharParser parser = new CharParser(testcasegrammar);
 
             if (testcasegrammar==null) return;
             CharParser parser = new CharParser(testcasegrammar);
 
@@ -56,7 +58,8 @@ public class RegressionTests {
             }
             System.err.println("expanding...");
 
             }
             System.err.println("expanding...");
 
-            TestCase[] expanded = (TestCase[])new TestCaseBuilder().walk(r2.expand1());
+            Tree t = r2.expand1();
+            TestCase[] expanded = (TestCase[])((Demo.Reducer)t.head()).reduce(t);
             System.err.println("executing...");
             for(TestCase tc : expanded) {
                 tc.execute();
             System.err.println("executing...");
             for(TestCase tc : expanded) {
                 tc.execute();
@@ -84,17 +87,49 @@ public class RegressionTests {
         }
     }
 
         }
     }
 
+    public static class TestCaseMaker extends ReflectiveMeta {
+        public TestCaseMaker() {
+            super(TestCaseMakerHelper.class, new Class[] {
+                     MG.Grammar.class,
+                     MG.NonTerminal.class,
+                     MG.AnonUn.class,
+                     MG.Range.class,
+                     MG.El.class,
+                     MG.Seq.class,
+                     MG.NonTerminalReference.class,
+                     MG.StringLiteral.class,
+                     MG.XTree.class,
+                     MG.CharClass.class
+            });
+        }
+        public static class TestCaseMakerHelper extends MG {
+            public static @tag("grammaro") @raw Object grammaro(Tree t) {
+                System.out.println("working on " + t);
+                return Demo.make(t.child(0), "s", new ReflectiveMetaPlain());
+            }
+            //public static @tag("tca")           Object tca(Object[] o) throws IOException {
+            //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
+            public static @tag("tca")           Object tca(String input, String[] output, Union u) throws IOException {
+                return new TestCase(input, output, u, false, false); }
+            public static @tag("tcb")           Object tca(String input, Union u) throws IOException {
+                return new TestCase(input, new String[0], u, false, false); }
+            public static @tag("ts") TestCase[] go(TestCase[] cases) { return cases; }
+            public static @tag("o") Object o(Object[] s) { return s; }
+        }
+    }
+
     public static class TestCase {
         private final boolean tib;
         private final boolean jav;
         public /*final*/ String input;        
         public final String[] output;
         public final Union grammar;
     public static class TestCase {
         private final boolean tib;
         private final boolean jav;
         public /*final*/ String input;        
         public final String[] output;
         public final Union grammar;
+
         public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
             this.tib = tib;
             this.jav = jav;
             this.input = input;
         public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
             this.tib = tib;
             this.jav = jav;
             this.input = input;
-            this.output = output;
+            this.output = output==null ? new String[0] : output;
             this.grammar = grammar;
         }
         public String toString() {
             this.grammar = grammar;
         }
         public String toString() {
@@ -167,13 +202,13 @@ public class RegressionTests {
     public static class TestCaseBuilder extends StringWalker {
         public Object walk(Tree<String> tree) {
             try {
     public static class TestCaseBuilder extends StringWalker {
         public Object walk(Tree<String> tree) {
             try {
-                if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
+                if ("grammaro".equals(tree.head())) return Demo.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];
                 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");
+                    Union grammar = Demo.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);
                     TestCase tc = new TestCase(input, output, grammar, false, false);
                     return tc;
                 } else if ("ts".equals(tree.head())) return walk(tree, 0);
index ae150e6..db85259 100644 (file)
@@ -8,7 +8,9 @@ import java.lang.reflect.*;
 public abstract class StringWalker extends TreeWalker<String> {
     public void   walk(String tag) { }
     public Object walk(Tree<String> tree) {
 public abstract class StringWalker extends TreeWalker<String> {
     public void   walk(String tag) { }
     public Object walk(Tree<String> tree) {
-        walk(tree.head());
+        Object o = tree.head();
+        if (!(o instanceof String)) o = null; /* FIXME */
+        walk((String)o);
         return super.walk(tree);
     }
     public Object walk(String tag, Object[] tokens) {
         return super.walk(tree);
     }
     public Object walk(String tag, Object[] tokens) {
index 48a9bae..bd7d107 100644 (file)
@@ -391,9 +391,9 @@ testcase {
 }
 
 
 }
 
 
-testcase {
-    input "ab";
-    
-    s = a:"a" b:"b"
-}
+//testcase {
+//    input "ab";
+//    
+//    s = a:"a" b:"b"
+//}
 
 
index b1321b1..ba23723 100644 (file)
@@ -1,10 +1,6 @@
 ts         = ts:: !ws tests !ws
 tests      =      test */ ws
 ts         = ts:: !ws tests !ws
 tests      =      test */ ws
-test       =      ^"testcase" "{" input output +/ ws   (grammar::Grammar) "}" /ws
-           |      ^"testcase" "{" input                (grammar::Grammar) "}" /ws
-           |      ^"tibcase"  "{" input output +/ ws   (grammar::Grammar) "}" /ws
-           |      ^"tibcase"  "{" input                (grammar::Grammar) "}" /ws
-           |      ^"javacase"  "{" input output +/ ws   (grammar::Grammar) "}" /ws
-           |      ^"javacase"  "{" input                (grammar::Grammar) "}" /ws
-output     =      ^"output" Quoted ";" /ws
-input      =      "input"  Quoted ";" /ws
+test       =   tca::   "testcase" "{" input (o::(output +/ ws))   (grammaro::(grammar::Grammar)) "}" /ws
+           |   tcb::   "testcase" "{" input                       (grammaro::(grammar::Grammar)) "}" /ws
+output     =           "output" Quoted ";" /ws
+input      =           "input"  Quoted ";" /ws