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) {
69 for(int i=0; i<12; i++) {
70 //System.out.println("length " + st.length());
72 long nowy = System.currentTimeMillis();
76 System.out.println("length " + st.length() + " => " + ((System.currentTimeMillis()-nowy)/1000.0) + " " + GSS.shifts + " " + GSS.reductions);
82 } catch (Throwable t) {
83 System.err.println("\n\nexception thrown, class == " + t.getClass().getName());
84 System.err.println(t);
91 public static class TestCase {
92 private final boolean tib;
93 private final boolean jav;
94 public /*final*/ String input;
95 public final String[] output;
96 public final Union grammar;
97 public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
101 this.output = output;
102 this.grammar = grammar;
104 public String toString() {
105 String ret = "testcase {\n" + " input \""+input+"\";\n";
106 for(String s : output) ret += " output \""+s+"\";\n";
107 ret += grammar +"\n}\n";
110 public boolean execute() throws Exception {
111 Forest<String> res = null;
112 ParseFailed pfe = null;
113 CharParser parser = new CharParser(grammar);
114 //parser.helpgc = false;
117 ? /*new CharParser(grammar).parse(new Tib(input))*/ null
118 : parser.parse(new StringReader(input));
119 } catch (ParseFailed pf) {
122 //ystem.out.println("res=="+res);
125 FileOutputStream fos = new FileOutputStream("out.dot");
126 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
127 GraphViz gv = new GraphViz();
132 System.out.println(parser);
134 Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
135 System.out.print("\r");
136 if (results == null || (results.size() == 0 && (output!=null && output.length > 0))) {
137 System.out.print("\033[31m");
138 System.out.println("PARSE FAILED");
139 System.out.print("\033[0m");
140 if (pfe != null) pfe.printStackTrace();
142 System.out.print("\r \r");
144 HashSet<String> outs = new HashSet<String>();
145 if (output != null) for(String s : output) outs.add(s.trim());
147 for (Tree<String> r : results) {
148 String s = r.toString().trim();
149 if (outs.contains(s)) { outs.remove(s); continue; }
150 if (!bad) System.out.println(input);
151 System.out.print("\033[33m");
152 System.out.println(" GOT: " + s);
155 for(String s : outs) {
156 if (!bad) System.out.println(input);
157 System.out.print("\033[31m");
158 System.out.println("EXPECTED: " + s);
162 System.out.println("\033[0m");
165 System.out.println("\r\033[32mPASS\033[0m ");
171 public static class TestCaseBuilder extends StringWalker {
172 public Object walk(Tree<String> tree) {
174 if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
175 else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
176 else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
177 else if ("testcase".equals(tree.head())) {
178 String input = MetaGrammar.string(tree.child(0));
179 String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
180 Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
181 TestCase tc = new TestCase(input, output, grammar, false, false);
183 } else if ("ts".equals(tree.head())) return walk(tree, 0);
184 else if (tree.head() == null) {
185 Object[] ret = new Object[tree.numChildren()];
186 for(int i=0; i<ret.length; i++)
187 ret[i] = walk(tree.child(i));
188 return Reflection.lub(ret);
190 return super.walk(tree);
191 } catch (Exception e) {
197 public static class JavaGrammar extends MetaGrammar {
198 public Object convertLabel(String label) { return new ClassMark(label); }
199 public Object convertFieldLabel(String label) { return new FieldMark(label); }
201 private static class FieldMark {
202 public final String field;
203 public FieldMark(String field) { this.field = field; }
204 public String toString() { return "."+field; }
206 private static class ClassMark {
207 public final String clazz;
208 public ClassMark(String clazz) { this.clazz = clazz; }
209 public String toString() { return clazz+"$"; }
212 public static Object build(Tree<Object> t, Class c) throws Exception {
213 System.out.println("** build " + c.getName() + " " + t.toPrettyString());
215 if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
217 if (t.numChildren()==0) o = t.head();
218 else if (t.head()==null) return build2(t, c);
219 else if (t.head() instanceof FieldMark) {
220 return build2(new Tree(null, new Tree[] { t }), c);
222 throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
224 return Reflection.rebuild(o, c);
226 private static Object build2(Tree<Object> t, Class c) throws Exception {
227 System.out.println("** build2 " + c.getName() + " " + t.toPrettyString());
228 if (!Reflection.isConcrete(c)) {
229 Field f = c.getField("subclasses");
230 if (f==null) throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
231 Class[] subs = (Class[])f.get(null);
232 OUTER: for(int i=0; i<subs.length; i++) {
233 for(Tree<Object> t2 : t) {
234 Object o2 = t2.head();
235 System.out.println("checking " + o2 + " in " + subs[i].getName());
236 if (o2 instanceof FieldMark) {
237 if (subs[i].getField(((FieldMark)o2).field)==null) continue OUTER;
244 Object o = c.newInstance();
245 for(Tree<Object> t2 : t) {
246 Object o2 = t2.head();
247 if (o2 instanceof FieldMark) {
248 FieldMark f = (FieldMark)o2;
249 Field field = c.getField(ReflectiveWalker.mangle(f.field));
250 Object tgt = build(t2.child(0), field.getType());
251 System.out.println("setting " + f.field +
252 " on a " + o.getClass().getName() +
253 " to " + (tgt==null?"null":tgt.getClass().getName()));
257 System.out.println("returning a " + o.getClass().getName());
260 public static Object build(Tree<Object> t) throws Exception {
262 if (h instanceof ClassMark) {
263 ClassMark cm = (ClassMark)h;
264 Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
268 return h==null ? null : h.toString();
271 public static interface Expr {
272 public static Class[] subclasses = new Class[] { _plus_.class, num.class };
274 public static class _plus_ implements Expr {
277 public String toString() { return left + "+" + right; }
279 public static class num implements Expr {
281 public String toString() { return "["+val+"]"; }
286 private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }