checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / TibDoc.java
index b8a4c48..df32b10 100644 (file)
@@ -5,13 +5,16 @@
 package edu.berkeley.sbp.tib;
 import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.meta.*;
 import edu.berkeley.sbp.util.*;
 import edu.berkeley.sbp.chr.*;
+import edu.berkeley.sbp.bind.*;
 import java.util.*;
 import java.io.*;
+import static edu.berkeley.sbp.meta.MetaGrammar.*;
 
 public class TibDoc {
-
+    /*
     public static Text lf()     { Chars ret = new Chars(); ret.text = "\n"; return ret; }
     public static Text cr()     { Chars ret = new Chars(); ret.text = "\r"; return ret; }
     public static Text emdash() { return new Entity("mdash"); }
@@ -43,8 +46,8 @@ public class TibDoc {
         public Body   body;
         public void toHTML(ToHTML.HTML sb) { sb.tag("html", body); }
         public static class Header extends HashMap<String, Text[]> {
-            public static class kv { public String key; public Text[] val; }
-            public void attrs(kv[] kvs) { for(int i=0; i<kvs.length; i++) this.put(kvs[i].key, kvs[i].val); }
+            public static class KeyVal { public String key; public Text[] val; }
+            public void attrs(KeyVal[] KeyVals) { for(int i=0; i<KeyVals.length; i++) this.put(KeyVals[i].key, KeyVals[i].val); }
         }
         public static class Body implements ToHTML {
             public Section[] sections;
@@ -97,8 +100,8 @@ public class TibDoc {
     public static class TT            extends Text.TextArray  { public String tag() { return "tt"; } }
     public static class Underline     extends Text.TextArray  { public String tag() { return "u"; } }
     public static class Italic        extends Text.TextArray  { public String tag() { return "i"; } }
-    public static class Citation      extends Text.TextArray  { /* FIXME */ }
-    public static class Footnote      extends Text.TextArray  { /* FIXME */ }
+    public static class Citation      extends Text.TextArray  { } // FIXME
+    public static class Footnote      extends Text.TextArray  { } // FIXME
     public static class LineBreak     extends Text            { public void toHTML(ToHTML.HTML sb) { sb.tag("br"); } }
     public static class Today         extends Text            { }
     public static class Euro          extends Text            { public void toHTML(ToHTML.HTML sb) { sb.entity(8364); } }
@@ -173,50 +176,8 @@ 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();
-        }
-    }
 
+    */
     /*
         public static void prefix(PrintWriter p) {
             p.println("% generated by TIBDOC");
@@ -362,5 +323,217 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
 
 
     */
+
+    // Main //////////////////////////////////////////////////////////////////////////////
+
+    public static class Dump implements Reflection.Show {
+        public String toString() { return Reflection.show((Reflection.Show)this); }
+    }
+
+    public static class TD {
+
+        public @bind.as static class Doc extends Dump {
+            public @bind.arg Header head;
+            public @bind.arg Body body;
+        }
+
+        public @bind.as static class Header extends Dump {
+            public @bind.arg KeyVal[] attrs;
+            // FIXME: it would be nice to be able to
+            // void KeyVal(String, String) { ... } imperatively
+        }
+        
+        public @bind.as static class Body extends Dump {
+            public Section[] sections;
+        }
+        
+        public @bind.as("Section") static class Section extends Dump {
+            public String      header;
+            public Paragraph[] paragraphs;
+        }
+        
+        public @bind.as static class KeyVal extends Dump {
+            public @bind.arg String key;
+            public @bind.arg Object val;
+        }
+
+        public abstract static class Paragraph extends Dump implements ToHTML { }
+
+        public @bind.as("P") static class P extends Paragraph {
+            public Text[] text;
+            public P() { }
+            public P(Text[] text) { this.text = text; }
+            public void toHTML(HTML h) { if (text != null) for (Text t : text) if (t != null) t.toHTML(h); }
+            public String toString() {
+                StringBuffer sb = new StringBuffer();
+                ToHTML.HTML h = new ToHTML.HTML(sb);
+                toHTML(h);
+                return sb.toString();
+            }
+        }
+
+        public @bind.as("HR") static class HR extends Paragraph {
+            public void toHTML(HTML h) { h.tag("hr"); }
+        }
+
+        public @bind.as("Blockquote") static class Blockquote extends Paragraph {
+            Text[] text;
+            public void toHTML(HTML h) { h.tag("blockquote", new P(text)); }
+        }
+
+        public abstract static class Text extends Dump implements ToHTML { }
+        public @bind.as static class Chars extends Text {
+            public String text;
+            public Chars() { }
+            public Chars(String text) { this.text = text; }
+            public void toHTML(HTML h) { h.appendText(" " + text + " "); }
+            public String toString() { return text; }
+        }
+        public @bind.as static class Block extends Text {
+            public Text[] text;
+            public void toHTML(HTML h) { for(Text t : text) t.toHTML(h); }
+        }
+        public static class TextWrap extends Text {
+            public Text text;
+            public void toHTML(HTML h) {
+                if (htmlTag()!=null)
+                    h.tag(htmlTag(), htmlTagParams(), text);
+                else
+                    text.toHTML(h);
+            }
+            public String   htmlTag() { return null; }
+            public Object[] htmlTagParams() { return null; }
+        }
+        public static @bind.as class Verbatim   extends Text { public char[] c; public void toHTML(HTML h) { } }
+        //public @bind.as class Blockquote extends TextWrap { }
+        public static @bind.as class Underline extends TextWrap { public String htmlTag() { return "u"; } }
+        public static @bind.as class Footnote extends TextWrap { public String htmlTag() { return "small"; } }
+        public static @bind.as class TT extends TextWrap { public String htmlTag() { return "tt"; } }
+        //public @bind.as class Citation extends Text {       "[" word "]" }
+        public static @bind.as class Strikethrough extends TextWrap { public String htmlTag() { return "strikethrough"; } }
+        public static @bind.as class Superscript extends TextWrap { public String htmlTag() { return "sup"; } }
+        public static @bind.as class Subscript extends TextWrap { public String htmlTag() { return "sub"; } }
+        public static @bind.as class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } }
+        public static @bind.as class Keyword extends TextWrap { public String htmlTag() { return "sc"; } }
+        public static @bind.as class Bold extends TextWrap { public String htmlTag() { return "b"; } }
+        public static @bind.as class Italic extends TextWrap { public String htmlTag() { return "i"; } }
+
+        public abstract static class Command extends Text { }
+        public static @bind.as class Today extends Command { public void toHTML(HTML h) { } }
+        public static @bind.as class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } }
+
+        public abstract static class Glyph extends Text { }
+        public static @bind.as("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.append("&emdash;"); } }
+
+        public static class Link extends Text {
+            public Text[] t;
+            public Url u;
+            public @bind.as("LinkText") Link(Text[] t, Url u)  { this.t = t; this.u = u; }
+            public @bind.as("LinkChars") Link(String s, Url u) { this(new Text[] { new Chars(s) }, u); }
+            public void toHTML(HTML h) {
+                h.tag("a",
+                      new Object[] { "href", u==null ? "" : u.toString() },
+                      new P(t));
+            }
+        }
+
+        public static class Host {
+            public String name;
+            public String toString() { return name; }
+            public @bind.as("DNS") Host(String[][] parts) {
+                name = "";
+                for(String[] s : parts) {
+                    for(String ss : s)
+                        name += ss;
+                    name += ".";
+                }
+            }
+            public @bind.as("IP")  Host(int a, int b, int c, int d) { name = a+"."+b+"."+c+"."+d; }
+        }
+
+        public static class Url extends Text {
+            public String   method;
+            public Host     host;
+            public String   user;
+            public String   pass;
+            public String   port;
+            public String   path;
+            public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path) {
+                this.method = method;
+                this.user = login==null ? null : login.length >= 1 ? login[0] : null;
+                this.pass = login==null ? null : login.length >= 2 ? login[1] : null;
+                this.host = host;
+                this.port = port;
+                this.path = path;
+            }
+            public @bind.as("Mailto") Url(String email) { this("mailto", null, null, "25", email); }
+            public void toHTML(HTML h) { new Link(toString(), this).toHTML(h); }
+            public String toString() {
+                return method + "://" + host + "/" + path;
+            }
+        }
+        public static @bind.as("lf")        String lf() { return "\r"; }
+        public static @bind.as("cr")        String cr() { return "\n"; }
+        public static @bind.as("\"\"")      String empty() { return ""; }
+        public static @bind.as("urlescape") String urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0')))+""; }
+    }
+
+    public static void main(String[] s) throws Exception {
+            /*
+        try {
+
+               FIXME FIXME
+
+            MetaGrammar.ReflectiveMeta m =
+                new MetaGrammar.ReflectiveMeta(TibDoc.TD.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());
+            
+            Object result = ((Functor)t.head()).invoke(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();
+
+
+            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();
+        }
+            */
+    }
+
 }