checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / TibDoc.java
index e0414c8..fbe833e 100644 (file)
@@ -9,6 +9,7 @@ import edu.berkeley.sbp.util.*;
 import edu.berkeley.sbp.chr.*;
 import java.util.*;
 import java.io.*;
+import static edu.berkeley.sbp.misc.Demo.*;
 
 public class TibDoc {
     /*
@@ -173,49 +174,6 @@ public class TibDoc {
 
 
 
-    // Main //////////////////////////////////////////////////////////////////////////////
-
-    public static void main(String[] s) throws Exception {
-        try {
-            System.out.println("parsing " + s[0]);
-            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
-            MetaGrammar gram = new Tib.Grammar(TibDoc.class);
-            gram = (MetaGrammar)gram.walk(res);
-            System.out.println("\nparsing " + s[1]);
-            Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1])));
-            System.out.println();
-            System.out.println(f.expand1().toPrettyString());
-            System.out.println();
-            Doc doc = (Doc)new ReflectiveGrammar(TibDoc.class).build(f.expand1());
-            System.out.println(doc);
-            System.out.println();
-            System.out.println();
-            System.out.println();
-            System.out.println();
-            StringBuffer sb = new StringBuffer();
-            doc.toHTML(new ToHTML.HTML(sb));
-            System.out.println(sb);
-
-            FileOutputStream fos = new FileOutputStream("out.html");
-            PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
-            p.println(sb);
-            p.flush();
-            p.close();
-
-        } catch (Ambiguous a) {
-            FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
-            PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
-            GraphViz gv = new GraphViz();
-            a.ambiguity.toGraphViz(gv);
-            gv.dump(p);
-            p.flush();
-            p.close();
-            a.printStackTrace();
-            
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
 
     */
     /*
@@ -363,5 +321,113 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
 
 
     */
+
+    // Main //////////////////////////////////////////////////////////////////////////////
+
+    public static class TD {
+
+        public @nonterminal("Doc") static class Doc {
+            public @arg("head") Header head;
+            public @arg("body") Body body;
+            public String toString() { return "doc:{"+head+","/*+body*/+"}"; }
+        }
+
+        public @nonterminal("Header") static class Header {
+            public @arg("attrs") KV[] attrs;
+            // FIXME: it would be nice to be able to
+            // void kv(String, String) { ... } imperatively
+            public String toString() {
+                StringBuffer ret = new StringBuffer();
+                ret.append("<");
+                for(KV kv : attrs) ret .append(kv);
+                ret.append(">");
+                return ret.toString();
+            }
+        }
+        
+        public @nonterminal("Body") static class Body {
+            public Object[] sections;
+            // FIXME: it would be nice to be able to
+            // void kv(String, String) { ... } imperatively
+            public String toString() {
+                StringBuffer ret = new StringBuffer();
+                ret.append("<");
+                for(Object kv : sections) ret .append(kv);
+                ret.append(">");
+                return ret.toString();
+            }
+        }
+        
+        public @nonterminal("kv") static class KV {
+            public @arg("key") String key;
+            public @arg("val") Object val;
+            public String toString() { return "KV["+key+"="+val+"]"; }
+        }
+    }
+
+    public static void main(String[] s) throws Exception {
+        try {
+
+            Demo.ReflectiveMeta m =
+                new Demo.ReflectiveMeta(TibDoc.TD.class,
+                                        new Class[] {
+                                            TibDoc.TD.Doc.class,
+                                            TibDoc.TD.Header.class,
+                                            TibDoc.TD.Body.class,
+                                            TibDoc.TD.KV.class
+                                        });
+            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
+            MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
+            MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
+            Union tibgram = mgf.get("s").build(bc);
+
+            System.err.println("parsing " + s[1]);
+            Tree t = new CharParser(tibgram).parse(new Tib(new FileInputStream(s[1]))).expand1();
+            System.out.println("tree:\n" + t.toPrettyString());
+            
+            Reducer red = (Reducer)t.head();
+            Object result = red.reduce(t);
+            System.out.println((TD.Doc)result);
+            /*
+            System.out.println("parsing " + s[0]);
+            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
+            MetaGrammar gram = new Tib.Grammar(TibDoc.class);
+            gram = (MetaGrammar)gram.walk(res);
+            System.out.println("\nparsing " + s[1]);
+            Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1])));
+            System.out.println();
+            System.out.println(f.expand1().toPrettyString());
+            System.out.println();
+            Doc doc = (Doc)new ReflectiveGrammar(TibDoc.class).build(f.expand1());
+            System.out.println(doc);
+            System.out.println();
+            System.out.println();
+            System.out.println();
+            System.out.println();
+            StringBuffer sb = new StringBuffer();
+            doc.toHTML(new ToHTML.HTML(sb));
+            System.out.println(sb);
+
+            FileOutputStream fos = new FileOutputStream("out.html");
+            PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
+            p.println(sb);
+            p.flush();
+            p.close();
+            */
+        } catch (Ambiguous a) {
+            FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
+            PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
+            GraphViz gv = new GraphViz();
+            a.ambiguity.toGraphViz(gv);
+            gv.dump(p);
+            p.flush();
+            p.close();
+            a.printStackTrace();
+            
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
 }