checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / RegressionTests.java
1 package edu.berkeley.sbp.misc;
2 import java.io.*;
3 import java.util.*;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.*;
8
9 // priorities are all messy and dont get serialized
10 // 1. Error messages
11 // 2. Java MetaGrammar (proof of concept)
12 // 3. Ivan's MetaGrammar
13 // 4. Documentation format
14 //       - TIB
15
16 // TODO: better API for interfacing with Java
17 // TODO: error messages
18 // TODO: integrate with TIB
19
20 // Element
21 // Walk
22 // ParseTable / GSS
23 // MetaGrammar (necessary/relevant?)
24 // Tree<String> (cleanup?)
25 // Union.SubUnion
26 // Repeat
27
28 // FEATURE: serialization of ParseTable's, generation of Java code
29 // FEATURE: infer reject elements for literals
30 // FEATURE: prefer whitespace higher up
31 // FEATURE: full conjunctive and boolean grammars
32 // FEATURE: "ambiguity modulo dropped fragments"?  can this be checked for statically?  eliminated statically?
33 //            - drop stuff during the parsing process (drop nodes)
34
35 // LATER: Element<A> -- parameterize over the input token type?  Makes a huge mess...
36 // LATER: Go back to where Sequence is not an Element?
37 //            - The original motivation for making Sequence "first class" was the fact that 
38 //              in order to do associativity right you need to have per-Sequence follow sets
39
40 public class RegressionTests {
41
42     public static boolean yes = false;
43     public static class MyWalker extends ReflectiveWalker {
44         public String top(Object[] o) { return "top("+join(o)+")"; }
45         public String str(String[] s) { String ret = ""; for(String st : s) ret += st; return ret; }
46         public String join(Object[] o) { String ret = ""; for(Object st : o) ret += st; return ret; }
47         public String whilex(Object s, Object y) { return "while("+s+") " + y; }
48         public String seq(Object[] statements) {
49             String ret = "";
50             for(Object s : statements) ret += s + ";\n";
51             return ret;
52         }
53         /*
54         public String bl(String s) { return "{" + s + "}"; }
55         */
56     };
57
58     public static void main(String[] s) throws Exception {
59         try {
60             boolean profile = false;
61             if (s[0].equals("-profile")) {
62                 profile = true;
63                 String[] s2 = new String[s.length-1];
64                 System.arraycopy(s, 1, s2, 0, s2.length);
65                 s = s2;
66             }
67
68             Tree<String> res = new Parser(MetaGrammar.make(), CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(new FileInputStream(s[0]))));
69             Union meta = ((MetaGrammar)new MetaGrammar().walk(res)).done();
70             SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
71             res = new Parser(meta, CharToken.top()).parse1(new CharToken.Stream(new InputStreamReader(sis), "parsing " + s[1] + " using " + s[0]));
72             Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
73             if (testcasegrammar==null) return;
74             CharToken.Stream cs = new CharToken.Stream(new InputStreamReader(new FileInputStream(s[2])), "parsing " + s[2] + " using " + s[1]);
75             Parser parser = new Parser(testcasegrammar, CharToken.top());
76
77             if (profile) {
78                 System.out.println("\nready...");
79                 System.in.read();
80             }
81             Forest<String> r2 = parser.parse(cs);
82             if (profile) {
83                 System.out.println("\ndone");
84                 System.in.read();
85                 System.exit(0);
86             }
87             for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
88
89         } catch (Throwable t) {
90             System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
91             System.err.println(t);
92             System.err.println();
93             t.printStackTrace();
94             System.err.println();
95         }
96     }
97
98     public static class TestCase {
99         public final String input;
100         public final String[] output;
101         public final Union grammar;
102         public TestCase(String input, String[] output, Union grammar) {
103             this.input = input;
104             this.output = output;
105             this.grammar = grammar;
106         }
107         public String toString() {
108             String ret = "testcase {\n" + "  input \""+input+"\";\n";
109             for(String s : output) ret += "  output \""+s+"\";\n";
110             ret += grammar +"\n}\n";
111             return ret;
112         }
113         public boolean execute() throws Exception {
114             Forest<String> res = new Parser(grammar,
115                                             CharToken.top()).parse(new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":""));
116             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
117             System.out.print("\r");
118             if (results.size() == 0 && output.length > 0) {
119                 System.out.print("\033[31m");
120                 System.out.println("PARSE FAILED");
121                 System.out.print("\033[0m");
122             } else {
123                 System.out.println("\r                                                                                                              \r");
124             }
125             HashSet<String> outs = new HashSet<String>();
126             for(String s : output) outs.add(s.trim());
127             boolean bad = false;
128             for (Tree<String> r : results) {
129                 String s = r.toString().trim();
130                 if (outs.contains(s)) { outs.remove(s); continue; }
131                 System.out.print("\033[33m");
132                 System.out.println("     GOT: " + s);
133                 bad = true;
134             }
135             for(String s : outs) {
136                 System.out.print("\033[31m");
137                 System.out.println("EXPECTED: " + s);
138                 bad = true;
139             }
140             if (bad) {
141                 System.out.print("\033[0m");
142                 return true;
143             }             
144             System.out.println("\033[32mPASS\033[0m");
145             return false;
146         }
147     }
148
149     public static class TestCaseBuilder extends MetaGrammar {
150         public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
151         public TestCase[] ts(TestCase[] ts) { return ts; }
152         public TestCase testcase(String input, String[] output, Union grammar) { return new TestCase(input, output, grammar); }
153         public MetaGrammar grammar(Object[] o) { return this; }
154         public Object walk(String tag, Object[] args) {
155             if ("testcase".equals(tag)) {
156                 if (args.length==2) return testcase((String)args[0], new String[0], (Union)args[1]); 
157                 return testcase((String)args[0], (String[])args[1], (Union)args[2]); }
158             else if ("grammar".equals(tag)) return done("s");
159             else return super.walk(tag, args);
160         }
161     }
162
163     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
164 }
165