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 // priorities are all messy and dont get serialized
11 // 2. Java MetaGrammar (proof of concept)
12 // 3. Ivan's MetaGrammar
13 // 4. Documentation format
16 // TODO: better API for interfacing with Java
17 // TODO: error messages
18 // TODO: integrate with TIB
23 // MetaGrammar (necessary/relevant?)
24 // Tree<String> (cleanup?)
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)
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
40 public class RegressionTests {
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) {
50 for(Object s : statements) ret += s + ";\n";
54 public String bl(String s) { return "{" + s + "}"; }
58 public static void main(String[] s) throws Exception {
60 boolean profile = false;
61 if (s[0].equals("-profile")) {
63 String[] s2 = new String[s.length-1];
64 System.arraycopy(s, 1, s2, 0, s2.length);
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());
78 System.out.println("\nready...");
81 Forest<String> r2 = parser.parse(cs);
83 System.out.println("\ndone");
87 for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
89 } catch (Throwable t) {
90 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
91 System.err.println(t);
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) {
104 this.output = output;
105 this.grammar = grammar;
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";
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");
123 System.out.println("\r \r");
125 HashSet<String> outs = new HashSet<String>();
126 for(String s : output) outs.add(s.trim());
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);
135 for(String s : outs) {
136 System.out.print("\033[31m");
137 System.out.println("EXPECTED: " + s);
141 System.out.print("\033[0m");
144 System.out.println("\033[32mPASS\033[0m");
149 public static class TestCaseBuilder extends MetaGrammar {
150 public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
151 public TestCase testcase(String input, String[] output, Union grammar) { return new TestCase(input, output, grammar); }
152 public MetaGrammar grammar(Object[] o) { return this; }
153 public Object walk(String tag, Object[] args) {
154 if ("testcase".equals(tag)) {
155 if (args.length==2) return testcase((String)args[0], new String[0], (Union)args[1]);
156 return testcase((String)args[0], (String[])args[1], (Union)args[2]); }
157 else if ("grammar".equals(tag)) return done("s");
158 else return super.walk(tag, args);
162 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }