1 package edu.berkeley.sbp.misc;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.*;
9 public class RegressionTests {
11 public static boolean yes = false;
12 public static class MyWalker extends ReflectiveWalker {
13 public String top(Object[] o) { return "top("+join(o)+")"; }
14 public String str(String[] s) { String ret = ""; for(String st : s) ret += st; return ret; }
15 public String join(Object[] o) { String ret = ""; for(Object st : o) ret += st; return ret; }
16 public String whilex(Object s, Object y) { return "while("+s+") " + y; }
17 public String seq(Object[] statements) {
19 for(Object s : statements) ret += s + ";\n";
23 public String bl(String s) { return "{" + s + "}"; }
27 public static void main(String[] s) throws Exception {
29 boolean profile = false;
30 if (s[0].equals("-profile")) {
32 String[] s2 = new String[s.length-1];
33 System.arraycopy(s, 1, s2, 0, s2.length);
37 //MetaGrammar mg0 = new MetaGrammar();
38 //mg0.walk(MetaGrammar.meta);
39 //System.out.println(mg0);
40 Tree<String> res = new Parser(MetaGrammar.make(), CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(s[0]))));
41 MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res);
42 //System.out.println(mg);
43 Union meta = mg.done();
44 SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
45 res = new Parser(meta, CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(sis), "parsing " + s[1] + " using " + s[0]));
46 Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
47 if (testcasegrammar==null) return;
48 CharToken.Stream cs = new CharToken.Stream(new InputStreamReader(new FileInputStream(s[2])), "parsing " + s[2] + " using " + s[1]);
49 Parser parser = new Parser(testcasegrammar, CharToken.top());
52 System.out.println("\nready...");
55 Forest<String> r2 = parser.parse(cs);
57 System.out.println("\ndone");
61 for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
63 } catch (Throwable t) {
64 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
65 System.err.println(t);
72 public static class TestCase {
73 public final String input;
74 public final String[] output;
75 public final Union grammar;
76 public TestCase(String input, String[] output, Union grammar) {
79 this.grammar = grammar;
81 public String toString() {
82 String ret = "testcase {\n" + " input \""+input+"\";\n";
83 for(String s : output) ret += " output \""+s+"\";\n";
84 ret += grammar +"\n}\n";
87 public boolean execute() throws Exception {
88 Forest<String> res = new Parser(grammar,
89 CharToken.top()).parse(new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":""));
90 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
91 System.out.print("\r");
92 if (results.size() == 0 && output.length > 0) {
93 System.out.print("\033[31m");
94 System.out.println("PARSE FAILED");
95 System.out.print("\033[0m");
97 System.out.println("\r \r");
99 HashSet<String> outs = new HashSet<String>();
100 if (output != null) for(String s : output) outs.add(s.trim());
102 for (Tree<String> r : results) {
103 String s = r.toString().trim();
104 if (outs.contains(s)) { outs.remove(s); continue; }
105 System.out.print("\033[33m");
106 System.out.println(" GOT: " + s);
109 for(String s : outs) {
110 System.out.print("\033[31m");
111 System.out.println("EXPECTED: " + s);
115 System.out.print("\033[0m");
118 System.out.println("\033[32mPASS\033[0m");
123 public static class TestCaseBuilder extends MetaGrammar {
124 public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
125 public TestCase[] ts(TestCase[] ts) { return ts; }
126 public TestCase testcase(String input, String[] output, Union grammar) { return new TestCase(input, output, grammar); }
127 public TestCase testcase(String input, Union grammar) { return new TestCase(input, new String[0], grammar); }
128 public MetaGrammar grammar(Object[] o) { return this; }
129 public Object walk(String tag, Object[] args) {
130 if ("grammar".equals(tag)) return done("s");
131 else return super.walk(tag, args);
135 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }