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;
15 public static void main(String[] s) throws Exception {
17 boolean profile = false;
18 if (s[0].equals("-profile")) {
20 String[] s2 = new String[s.length-1];
21 System.arraycopy(s, 1, s2, 0, s2.length);
25 //MetaGrammar mg0 = new MetaGrammar();
26 //mg0.walk(MetaGrammar.meta);
27 //System.out.println(mg0);
28 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
29 MetaGrammar mg = (MetaGrammar)new MetaGrammar().walk(res);
30 //System.out.println(mg);
31 Union meta = mg.done();
32 SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
33 res = new CharParser(meta).parse(sis).expand1();
34 Union testcasegrammar = ((MetaGrammar)new MetaGrammar("ts").walk(res)).done("ts");
35 if (testcasegrammar==null) return;
36 CharParser parser = new CharParser(testcasegrammar);
39 System.out.println("\nready...");
43 long now = System.currentTimeMillis();
44 Forest<String> r2 = parser.parse(new FileInputStream(s[2]));
46 System.out.println("elapsed = " + (System.currentTimeMillis()-now) + "ms");
48 System.out.println("\ndone");
52 for(TestCase tc : (TestCase[])new TestCaseBuilder().walk(r2.expand1())) tc.execute();
54 } catch (Throwable t) {
55 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
56 System.err.println(t);
63 public static class TestCase {
64 private final boolean tib;
65 private final boolean jav;
66 public final String input;
67 public final String[] output;
68 public final Union grammar;
69 public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
74 this.grammar = grammar;
76 public String toString() {
77 String ret = "testcase {\n" + " input \""+input+"\";\n";
78 for(String s : output) ret += " output \""+s+"\";\n";
79 ret += grammar +"\n}\n";
82 public boolean execute() throws Exception {
84 Tree<Object> tree = new CharParser(grammar).parse(new StringReader(input)).expand1();
85 System.out.println(tree);
86 System.out.println(JavaGrammar.build(tree));
89 Forest<String> res = null;
90 ParseFailed pfe = null;
93 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
94 : new CharParser(grammar).parse(new StringReader(input));
95 } catch (ParseFailed pf) {
98 //ystem.out.println("res=="+res);
99 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
100 System.out.print("\r");
101 if (results.size() == 0 && output.length > 0) {
102 System.out.print("\033[31m");
103 System.out.println("PARSE FAILED");
104 System.out.print("\033[0m");
105 if (pfe != null) pfe.printStackTrace();
107 System.out.print("\r \r");
109 HashSet<String> outs = new HashSet<String>();
110 if (output != null) for(String s : output) outs.add(s.trim());
112 for (Tree<String> r : results) {
113 String s = r.toString().trim();
114 if (outs.contains(s)) { outs.remove(s); continue; }
115 if (!bad) System.out.println(input);
116 System.out.print("\033[33m");
117 System.out.println(" GOT: " + s);
120 for(String s : outs) {
121 if (!bad) System.out.println(input);
122 System.out.print("\033[31m");
123 System.out.println("EXPECTED: " + s);
127 System.out.println("\033[0m");
130 System.out.println("\r\033[32mPASS\033[0m ");
135 public static class TestCaseBuilder extends MetaGrammar {
136 public Object walk(Tree<String> tree) {
138 if ("grammar".equals(tree.head())) { walkChildren(tree); return done("s"); }
139 else if ("output".equals(tree.head())) return string(tree.children());
140 else if ("input".equals(tree.head())) return string(tree.children());
141 else if ("javacase".equals(tree.head()) || "tibcase".equals(tree.head()) || "testcase".equals(tree.head())) {
142 String input = string(tree.child(0));
143 String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
144 boolean tib = "tibcase".equals(tree.head());
145 boolean jav = "javacase".equals(tree.head());
146 MetaGrammar gram = jav ? new JavaGrammar() : tib ? new Tib.Grammar() : new MetaGrammar();
147 Union grammar = (Union)((MetaGrammar)(gram.walk(tree, tree.numChildren()-1))).done("s");
148 return new TestCase(input, output, grammar, tib, jav);
149 } else if ("ts".equals(tree.head())) return walk(tree, 0);
150 else return super.walk(tree);
151 } catch (Exception e) {
157 public static class JavaGrammar extends MetaGrammar {
158 public Object convertLabel(String label) { return new ClassMark(label); }
159 public Object convertFieldLabel(String label) { return new FieldMark(label); }
161 private static class FieldMark {
162 public final String field;
163 public FieldMark(String field) { this.field = field; }
164 public String toString() { return "."+field; }
166 private static class ClassMark {
167 public final String clazz;
168 public ClassMark(String clazz) { this.clazz = clazz; }
169 public String toString() { return clazz+"$"; }
172 public static Object build(Tree<Object> t, Class c) throws Exception {
173 System.out.println("** build " + c.getName() + " " + t.toPrettyString());
175 if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
177 if (t.numChildren()==0) o = t.head();
178 else if (t.head()==null) return build2(t, c);
179 else if (t.head() instanceof FieldMark) {
180 return build2(new Tree(null, new Tree[] { t }), c);
182 throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
184 return Reflection.rebuild(o, c);
186 private static Object build2(Tree<Object> t, Class c) throws Exception {
187 System.out.println("** build2 " + c.getName() + " " + t.toPrettyString());
188 if (!Reflection.isConcrete(c)) {
189 Field f = c.getField("subclasses");
190 if (f==null) throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
191 Class[] subs = (Class[])f.get(null);
192 OUTER: for(int i=0; i<subs.length; i++) {
193 for(Tree<Object> t2 : t) {
194 Object o2 = t2.head();
195 System.out.println("checking " + o2 + " in " + subs[i].getName());
196 if (o2 instanceof FieldMark) {
197 if (subs[i].getField(((FieldMark)o2).field)==null) continue OUTER;
204 Object o = c.newInstance();
205 for(Tree<Object> t2 : t) {
206 Object o2 = t2.head();
207 if (o2 instanceof FieldMark) {
208 FieldMark f = (FieldMark)o2;
209 Field field = c.getField(ReflectiveWalker.mangle(f.field));
210 Object tgt = build(t2.child(0), field.getType());
211 System.out.println("setting " + f.field +
212 " on a " + o.getClass().getName() +
213 " to " + (tgt==null?"null":tgt.getClass().getName()));
217 System.out.println("returning a " + o.getClass().getName());
220 public static Object build(Tree<Object> t) throws Exception {
222 if (h instanceof ClassMark) {
223 ClassMark cm = (ClassMark)h;
224 Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
228 return h==null ? null : h.toString();
231 public static interface Expr {
232 public static Class[] subclasses = new Class[] { _plus_.class, num.class };
234 public static class _plus_ implements Expr {
237 public String toString() { return left + "+" + right; }
239 public static class num implements Expr {
241 public String toString() { return "["+val+"]"; }
246 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }