X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FDemo.java;h=993b285790ba9c90e8cf11a831afd147fafc0b09;hp=fa6d733125025a6b81bb70caf0f2ac2fd1912335;hb=8abe88ea7a5d7b1fc20c45b05e6397dad81e7cdb;hpb=0bf2fced924400a496fb154be71188864296564c diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index fa6d733..993b285 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -12,26 +12,66 @@ public class Demo { public static boolean harsh = false; - public static void main(String[] s) throws Exception { + public static void main(String[] args) throws Exception { + if (args.length != 2) { + System.err.println("usage: java " + Demo.class.getName() + " grammarfile.g com.yourdomain.package.ClassName"); + System.exit(-1); + } + //StringBuffer sbs = new StringBuffer(); + //((MetaGrammar)new MetaGrammar().walk(meta)).nt.get("e").toString(sbs); + //System.err.println(sbs); + String className = args[1].substring(args[1].lastIndexOf('.')+1); + String packageName = args[1].substring(0, args[1].lastIndexOf('.')); + String fileName = packageName.replace('.', '/') + "/" + className + ".java"; + + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); + StringBuffer out = new StringBuffer(); + + boolean skip = false; + for(String s = br.readLine(); s != null; s = br.readLine()) { + if (s.indexOf("DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) skip = true; + if (s.indexOf("DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) break; + if (!skip) out.append(s+"\n"); + } + out.append("\n // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n"); + + /* ReflectiveMeta m = new ReflectiveMeta(); - Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); + Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1(); MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res); MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf); Union meta = mgf.get("s").build(bc); - Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1(); - + Tree t = new CharParser(meta).parse(new FileInputStream(args[0])).expand1(); + */ + Tree t = DemoMetaGrammarTree.meta; Union u = Demo.make(t, "s"); System.err.println(); System.err.println("== parsing with parsed grammar ================================================================================="); - t = new CharParser((Union)u).parse(new FileInputStream(s[1])).expand1(); - System.out.println(t.toPrettyString()); - - System.err.println("== parsing with parsed-parsed grammar =========================================================================="); - t = new CharParser(new Context(t, m).build()).parse(new FileInputStream(s[1])).expand1(); + t = new CharParser((Union)u).parse(new FileInputStream(args[0])).expand1(); System.out.println(t.toPrettyString()); + //Forest fs = new CharParser(make()).parse(new FileInputStream(args[0])); + //System.out.println(fs.expand1()); + + //GraphViz gv = new GraphViz(); + //fs.toGraphViz(gv); + //FileOutputStream fox = new FileOutputStream("out.dot"); + //gv.dump(fox); + //fox.close(); + + t.toJava(out); + out.append("\n // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n"); + + for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n"); + br.close(); + + OutputStream os = new FileOutputStream(fileName); + PrintWriter p = new PrintWriter(new OutputStreamWriter(os)); + p.println(out.toString()); + p.flush(); + os.close(); } public static class ReflectiveMetaPlain extends ReflectiveMeta { @@ -43,7 +83,7 @@ public class Demo { } } - public static class ReflectiveMeta extends MetaGrammar.Meta { + public static class ReflectiveMeta /*extends MetaGrammar.Meta*/ { private final Class _cl; private final Class[] _inner; public ReflectiveMeta() { @@ -197,24 +237,34 @@ public class Demo { } } public Sequence makeSequence(Production p) { - return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this, _bindable, isRaw()), + return Sequence.rewritingSequence(new TargetReducer(buildSequence(p), _bindable, isRaw()), p.elements, p.labels, p.drops); } } - public static class TargetReducer implements Tree.TreeFunctor { + public static class TargetReducer implements Tree.TreeFunctor, ToJava { private int[] map; - private String name; private Bindable _bindable; private boolean _israw; - public TargetReducer(Production p, int[] map, String name, Bindable b, boolean raw) { + + public void toJava(StringBuffer sb) { + sb.append("new Demo.TargetReducer(new int[] {"); + for(int i=0; i> t) { if (_israw) return _bindable.impose(new Object[] { t }); ArrayList ret = new ArrayList(); @@ -241,6 +291,7 @@ public class Demo { public static Union cached = null; public static Union make() { + /* if (cached != null) return cached; try { ReflectiveMeta m = new ReflectiveMeta(); @@ -253,6 +304,8 @@ public class Demo { } catch (Exception e) { throw new RuntimeException(e); } + */ + return make(DemoMetaGrammarTree.meta, "s"); } public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); } public static Union make(Tree t, String s, ReflectiveMeta rm) {