1 package edu.berkeley.sbp.misc;
4 import java.lang.reflect.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.tib.*;
8 import edu.berkeley.sbp.chr.*;
9 import edu.berkeley.sbp.util.*;
11 public class RegressionTests {
13 public static boolean yes = false;
14 public static boolean graph = false;
16 public static void main(String[] s) throws Exception {
18 boolean profile = false;
19 if (s[0].equals("-graph")) {
21 String[] s2 = new String[s.length-1];
22 System.arraycopy(s, 1, s2, 0, s2.length);
25 if (s[0].equals("-profile")) {
27 String[] s2 = new String[s.length-1];
28 System.arraycopy(s, 1, s2, 0, s2.length);
32 //MetaGrammar mg0 = new MetaGrammar();
33 //mg0.walk(MetaGrammar.meta);
34 //System.out.println(mg0);
35 System.err.println("parsing " + s[0]);
36 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
37 //System.out.println(mg);
38 Union meta = MetaGrammar.make(res, "s");
39 System.err.println("parsing " + s[1]);
40 SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
41 res = new CharParser(meta).parse(sis).expand1();
42 Union testcasegrammar = MetaGrammar.make(res, "ts");
43 if (testcasegrammar==null) return;
44 CharParser parser = new CharParser(testcasegrammar);
47 System.out.println("\nready...");
51 long now = System.currentTimeMillis();
52 System.err.println("parsing " + s[2]);
53 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
55 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
57 System.out.println("\ndone");
61 System.err.println("expanding...");
63 TestCase[] expanded = (TestCase[])new TestCaseBuilder().walk(r2.expand1());
64 System.err.println("executing...");
65 for(TestCase tc : expanded) tc.execute();
67 } catch (Throwable t) {
68 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
69 System.err.println(t);
76 public static class TestCase {
77 private final boolean tib;
78 private final boolean jav;
79 public final String input;
80 public final String[] output;
81 public final Union grammar;
82 public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
87 this.grammar = grammar;
89 public String toString() {
90 String ret = "testcase {\n" + " input \""+input+"\";\n";
91 for(String s : output) ret += " output \""+s+"\";\n";
92 ret += grammar +"\n}\n";
95 public boolean execute() throws Exception {
96 Forest<String> res = null;
97 ParseFailed pfe = null;
98 CharParser parser = new CharParser(grammar);
99 //parser.helpgc = false;
102 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
103 : parser.parse(new StringReader(input));
104 } catch (ParseFailed pf) {
107 //ystem.out.println("res=="+res);
109 FileOutputStream fos = new FileOutputStream("out.dot");
110 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
111 GraphViz gv = new GraphViz();
116 System.out.println(parser);
118 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
119 System.out.print("\r");
120 if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
121 System.out.print("\033[31m");
122 System.out.println("PARSE FAILED");
123 System.out.print("\033[0m");
124 if (pfe != null) pfe.printStackTrace();
126 System.out.print("\r \r");
128 HashSet<String> outs = new HashSet<String>();
129 if (output != null) for(String s : output) outs.add(s.trim());
131 for (Tree<String> r : results) {
132 String s = r.toString().trim();
133 if (outs.contains(s)) { outs.remove(s); continue; }
134 if (!bad) System.out.println(input);
135 System.out.print("\033[33m");
136 System.out.println(" GOT: " + s);
139 for(String s : outs) {
140 if (!bad) System.out.println(input);
141 System.out.print("\033[31m");
142 System.out.println("EXPECTED: " + s);
146 System.out.println("\033[0m");
149 System.out.println("\r\033[32mPASS\033[0m ");
154 public static class TestCaseBuilder extends StringWalker {
155 public Object walk(Tree<String> tree) {
157 if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
158 else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
159 else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
160 else if ("testcase".equals(tree.head())) {
161 String input = MetaGrammar.string(tree.child(0));
162 String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
163 Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
164 TestCase tc = new TestCase(input, output, grammar, false, false);
166 } else if ("ts".equals(tree.head())) return walk(tree, 0);
167 else if (tree.head() == null) {
168 Object[] ret = new Object[tree.numChildren()];
169 for(int i=0; i<ret.length; i++)
170 ret[i] = walk(tree.child(i));
171 return Reflection.lub(ret);
173 return super.walk(tree);
174 } catch (Exception e) {
180 public static class JavaGrammar extends MetaGrammar {
181 public Object convertLabel(String label) { return new ClassMark(label); }
182 public Object convertFieldLabel(String label) { return new FieldMark(label); }
184 private static class FieldMark {
185 public final String field;
186 public FieldMark(String field) { this.field = field; }
187 public String toString() { return "."+field; }
189 private static class ClassMark {
190 public final String clazz;
191 public ClassMark(String clazz) { this.clazz = clazz; }
192 public String toString() { return clazz+"$"; }
195 public static Object build(Tree<Object> t, Class c) throws Exception {
196 System.out.println("** build " + c.getName() + " " + t.toPrettyString());
198 if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
200 if (t.numChildren()==0) o = t.head();
201 else if (t.head()==null) return build2(t, c);
202 else if (t.head() instanceof FieldMark) {
203 return build2(new Tree(null, new Tree[] { t }), c);
205 throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
207 return Reflection.rebuild(o, c);
209 private static Object build2(Tree<Object> t, Class c) throws Exception {
210 System.out.println("** build2 " + c.getName() + " " + t.toPrettyString());
211 if (!Reflection.isConcrete(c)) {
212 Field f = c.getField("subclasses");
213 if (f==null) throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
214 Class[] subs = (Class[])f.get(null);
215 OUTER: for(int i=0; i<subs.length; i++) {
216 for(Tree<Object> t2 : t) {
217 Object o2 = t2.head();
218 System.out.println("checking " + o2 + " in " + subs[i].getName());
219 if (o2 instanceof FieldMark) {
220 if (subs[i].getField(((FieldMark)o2).field)==null) continue OUTER;
227 Object o = c.newInstance();
228 for(Tree<Object> t2 : t) {
229 Object o2 = t2.head();
230 if (o2 instanceof FieldMark) {
231 FieldMark f = (FieldMark)o2;
232 Field field = c.getField(ReflectiveWalker.mangle(f.field));
233 Object tgt = build(t2.child(0), field.getType());
234 System.out.println("setting " + f.field +
235 " on a " + o.getClass().getName() +
236 " to " + (tgt==null?"null":tgt.getClass().getName()));
240 System.out.println("returning a " + o.getClass().getName());
243 public static Object build(Tree<Object> t) throws Exception {
245 if (h instanceof ClassMark) {
246 ClassMark cm = (ClassMark)h;
247 Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
251 return h==null ? null : h.toString();
254 public static interface Expr {
255 public static Class[] subclasses = new Class[] { _plus_.class, num.class };
257 public static class _plus_ implements Expr {
260 public String toString() { return left + "+" + right; }
262 public static class num implements Expr {
264 public String toString() { return "["+val+"]"; }
269 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }