checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / TibDoc.java
index 538d808..26f8174 100644 (file)
 // You may not use this file except in compliance with the License.
 
 package edu.berkeley.sbp.tib;
-//import org.ibex.util.*;
-//import org.ibex.io.*;
 import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.misc.*;
 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 {
-    
-    public static void main(String[] s) throws Exception {
-        System.out.println("parsing " + s[0]);
-        Tree<String> res = new CharToken.CharToStringParser(MetaGrammar.make()).parse(new CharToken.Stream(new FileInputStream(s[0]))).expand1();
-        MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res);
-        //System.out.println(gram);
-        Union mg = gram.done();
-        
-        System.out.println("\nparsing " + s[1]);
-        Forest f = new CharToken.CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1])));
-
-        System.out.println();
-        System.out.println(f);
-        System.out.println();
-        System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString());
-
-        String st = new HTMLWalker().walk(f.expand1()).toString();
-        System.out.println(st);
-        FileOutputStream fos = new FileOutputStream("out.html");
-        PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
-        p.println(st);
-        p.flush();
-        p.close();
-    }
+    /*
+    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"); }
+    public static char urlescape(int a, int b) { return (char)(10*a+b); }
 
-    public static class StringifyWalker extends ReflectiveWalker {
-        public Object walk(String head, Object[] children) {
-            if ("stringify".equals(head)) {
-                StringBuffer ret = new StringBuffer();
-                for(Tree<String> t : (Tree<String>)children[0]) ret.append(t);
-                return new Tree<String>(null, ret.toString());
-            }
-            if (children.length==0) return new Tree<String>(null, head, new Tree[0]);
-            return new Tree<String>(null, head, (Tree<String>[])Reflection.lub(children));
-        }
-    }
 
-    public static String join(String[] sa, String sep) {
-        StringBuffer ret = new StringBuffer();
-        boolean first = true;
-        for(String s : sa) {
-            if (!first) ret.append(sep);
-            first = false;
-            ret.append(s);
-        }
-        return ret.toString();
-    }
+    // Template Classes //////////////////////////////////////////////////////////////////////////////
 
-    public static class HTMLWalker extends ReflectiveWalker {
-        //public void header() { throw new Error(); }
-        public String li(Object o) { return "<li>"+o+"</li>"; }
-        public String ul(String[] li) { return "<ul>"+join(li,"")+"</ul>"; }
-        public String ol(String[] li) { return "<ol>"+join(li,"")+"</ol>"; }
-        public String hr() { return "\n<hr/>\n"; }
-        public String it(Object o) { return "<i>"+o+"</i>"; }
-        public String tt(Object o) { return "<tt>"+o+"</tt>"; }
-        public String underline(Object o) { return "<ul>"+o+"</ul>"; }
-        public String p(Object o) { return "<p>"+o+"</p>"; }
-        public String smallcap(Object o) { return "<span style='font-variant: small-caps'>"+o+"</span>"; }
-        public String blockquote(Object o) { return "<blockquote>"+o+"</blockquote>"; }
-        public String superscript(Object o) { return "<sup>"+o+"</sup>"; }
-        public String subscript(Object o) { return "<sub>"+o+"</sub>"; }
-        public String bold(Object o) { return "<b>"+o+"</b>"; }
-        public String strikethrough(Object o) { throw new Error();/*return "<b>"+o+"</b>";*/ }
-        public Object top(Object o) { return "<html><body>"+o+"</body></html>"; }
-        public Object doc(Object header, Object body) { return body; }
-        public String text(Object[] body) {
-            StringBuffer ret = new StringBuffer();
-            for(Object o : body) { ret.append(o); ret.append(" "); }
-            return ret.toString();
-        }
-        public String body(String[] sections) { return join(sections, "\n\n"); }
-        public String domain(String[] parts) { return join(parts, "."); }
-        public String ip(String[] parts) { return join(parts, "."); }
-        public String emailaddr(String user, String host) {
-            return link(user+"@"+host, "mailto:"+user+"@"+host);
+    public static abstract class Text implements ToHTML {
+        public static final Class[] subclasses = new Class[] { Chars.class, URL.class, Email.class };
+        public void toHTML(ToHTML.HTML sb) { }
+        public static class TextString extends Text {
+            public String text;
+            public String tag() { return null; }
+            public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), text); }
         }
-        //public String url(String method) {
-        public String link(Object text, Object target) {
-            return "<a href='"+target+"'>"+text+"</a>";
+        public static class TextArray extends Text {
+            public Text[] t;
+            public String tag() { return null; }
+            public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), t); }
         }
-        public String section(Object header, Object[] body) {
-            StringBuffer ret = new StringBuffer();
-            ret.append(header);
-            ret.append(" ");
-            for(Object o : body) ret.append(o);
-            return ret.toString();
+    }
+
+
+    // Structural //////////////////////////////////////////////////////////////////////////////
+
+    public static class Doc implements ToHTML {
+        public Header head;
+        public Body   body;
+        public void toHTML(ToHTML.HTML sb) { sb.tag("html", body); }
+        public static class Header extends HashMap<String, Text[]> {
+            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); }
         }
-        private String escapify(Object o) {
-            String s = o==null ? "" : o.toString();
-            StringBuffer sb = new StringBuffer();
-            for(int i=0; i<s.length(); i++) {
-                switch(s.charAt(i)) {
-                    case '&':  sb.append("&amp;"); break;
-                    case '<':  sb.append("&lt;"); break;
-                    case '>':  sb.append("&gt;"); break;
-                    case '\'': sb.append("&apos;"); break;
-                    case '\"': sb.append("&quot;"); break;
-                    default:   sb.append(s.charAt(i)); break;
+        public static class Body implements ToHTML {
+            public Section[] sections;
+            public void toHTML(ToHTML.HTML sb) { sb.append(sections); }
+            public static class Section implements ToHTML {
+                public Text header;
+                public Paragraph[] paragraphs;
+                public void toHTML(ToHTML.HTML sb) {
+                    sb.tag("h3", header);
+                    sb.append(paragraphs);
                 }
             }
-            return sb.toString();
         }
-        private Tree<String> lone(String s) {
-            return new Tree<String>(null, s, new Tree[0]);
+    }
+
+
+    // Paragraph //////////////////////////////////////////////////////////////////////////////
+
+    public static interface Paragraph extends ToHTML {
+        public static final Class[] subclasses = new Class[] { Blockquote.class, P.class, HR.class };
+        public static class HR                                implements Paragraph { public void toHTML(ToHTML.HTML sb) { sb.append("\n<hr>\n"); } }
+        public static class P          extends Text.TextArray implements Paragraph { public String tag() { return "p"; } }
+        public static class Blockquote extends Text.TextArray implements Paragraph { public String tag() { return "blockquote"; } }
+    }
+
+    public static abstract class List extends Text {
+        public Text[][] points;
+        public abstract String tag();
+        public void toHTML(ToHTML.HTML sb) {
+            sb.append("<"+tag()+">\n");
+            for(Text[] t : points) sb.tag("li", t);
+            sb.append("</"+tag()+">\n");
         }
-        public Object walk(Tree<String> t) {
-            String head = t.head();
-            if ("stringify".equals(head)) {
-                StringBuffer ret = new StringBuffer();
-                for(Tree<String> child : t.child(0)) ret.append(child);
-                return ret.toString();
+    }
+    public static class OL extends List { public String tag() { return "ol"; } }
+    public static class UL extends List { public String tag() { return "ul"; } }
+
+
+
+    // Tags //////////////////////////////////////////////////////////////////////////////
+
+    public static class Chars         extends Text.TextString { }
+    public static class Symbol        extends Text.TextString { }
+    public static class Keyword       extends Text.TextString { public String tag() { return "tt"; } }
+    public static class Subscript     extends Text.TextString { public String tag() { return "sub"; } }
+    public static class Superscript   extends Text.TextString { public String tag() { return "super"; } }
+    public static class Bold          extends Text.TextArray  { public String tag() { return "b"; } }
+    public static class Smallcap      extends Text.TextArray  { public String tag() { return "sc"; } }
+    public static class Strikethrough extends Text.TextArray  { public String tag() { return "strike"; } }
+    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 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); } }
+    public static class Link          extends Text {
+        public Text[] text;
+        public URI href;
+        public void toHTML(ToHTML.HTML sb) { sb.tag("a", new Object[] { "href", href }, text); }
+    }
+    public static class Entity        extends Text {
+        public final String entity;
+        public Entity(String entity) { this.entity = entity; }
+        public void toHTML(ToHTML.HTML sb) { sb.entity(entity); }
+    }
+
+
+    // Network //////////////////////////////////////////////////////////////////////////////
+
+    public static interface Host extends ToHTML {
+        public static class DNS implements Host {
+            public String[] part;
+            public void toHTML(ToHTML.HTML sb) {
+                for(int i=0; i<part.length; i++)
+                    sb.append((i==0 ? "" : ".")+part[i]);
             }
-            return super.walk(t);
         }
-        protected Object defaultWalk(String head, Object[] children) {
-            Tree<String>[] kids = new Tree[children.length];
-            for(int i=0; i<children.length; i++) {
-                if (children[i]==null) kids[i]=null;
-                else if (children[i] instanceof String) kids[i] = lone(escapify((String)children[i]));
-                else if (children[i] instanceof Tree) kids[i] = (Tree<String>)children[i];
-                else kids[i] = lone(children[i].toString());
-            }
-            return new Tree<String>(null, head, kids);
+        public static class IP  implements Host {
+            public int a, b, c, d;
+            public void toHTML(ToHTML.HTML sb) { sb.append(a+"."+b+"."+c+"."+d); }
         }
     }
 
-    /*
-    public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }
-
-    public static AST h(AST a)      { return new Gather(a, Style.H); }
-    public static AST ul(AST a)     { return new Gather(a, Style.UL); }
-    public static AST tt(AST a)     { return new Gather(a, Style.TT); }
-    public static AST so(AST a)     { return new Gather(a, Style.SO); }
-    public static AST it(AST a)     { return new Gather(a, Style.IT); }
-    public static AST q(AST a)      { return new Gather(a, Style.Q); }
-    public static AST b(AST a)      { return new Gather(a, Style.B); }
-    public static AST pre(AST a)    { return new Gather(a, Style.PRE); }
-    public static AST list(AST a)   { return new Gather(a, Style.LIST); }
-    public static AST emdash()      { return new Gather(Style.EMDASH); }
-
-    public static AST seq(AST a) { return new Gather(a); }
-
-    public static class Latex {
-        public static void emit(PrintWriter p, AST a) {
-            prefix(p);
-            emit(p, a, "");
-            suffix(p);
-        }
-        public static void emit2(PrintWriter p, AST ast, String head) {
-            for(AST a = ast.getFirstChild(); a != null; a = a.getNextSibling()) emit(p, a, head);
+    public static interface URI extends ToHTML {
+        public static final Class[] subclasses = new Class[] { URL.class, Email.class };
+    }
+    public static class URL extends Text    implements URI {
+        public String method;
+        public Login login;
+        public Host host;
+        public int port;
+        public String path;
+        public void toHTML(ToHTML.HTML sb) {
+            sb.append(method);
+            sb.append("://");
+            // login.toHTML(sb);   FIXME
+            host.toHTML(sb);
+            // sb.append(":");     FIXME
+            // sb.append(port);
+            sb.append("/");
+            sb.append(path);
         }
-        public static void emit(PrintWriter p, AST ast, String head) {
-            if (!(ast instanceof Gather)) {
-                if (ast.getNumberOfChildren()==0) {
-                    p.print(ast.getText());
-                } else {
-                    emit2(p, ast, head);
-                }
-                return;
-            }
-            Gather a = (Gather)ast;
-            if (a.style==null) {
-                emit2(p, a, head);
-                return;
-            }
-            switch(a.style) {
-                case H:    p.println(); p.println(); p.print("\\"+head+"section{"); emit2(p, a, "sub"+head); p.println("}"); break;
-                case B:    p.print("{\\bf{");                          emit2(p, a, head); p.print("}}"); break;
-                case UL:   p.print("{\\ul{");                          emit2(p, a, head); p.print("}}"); break;
-                case IT:   p.print("{\\it{");                          emit2(p, a, head); p.print("}}"); break;
-                case TT:   p.print("{\\tt{");                          emit2(p, a, head); p.print("}}"); break;
-                case SO:   p.print("{\\overstrike{");                  emit2(p, a, head); p.print("}}"); break;
-                case Q:    p.print("``");                              emit2(p, a, head); p.print("''"); break;
-                case EMDASH: p.print(" \\emdash "); break;
-                case LIST: p.println(); p.println("\\startitemize[symbol]"); emit2(p, a, head); p.println("\\stopitemize"); break;
-                case PRE:
-                    if (a.getFirstChild() != null) {
-                        p.println();
-                        p.println("\\begin{verbatim}");
-                        p.println(a.getFirstChild().getText());
-                        p.println("\\end{verbatim}");
-                    }
-            }
+    }
+    public static class Email extends Text  implements URI {
+        public String user;
+        public Host host;
+        public void toHTML(ToHTML.HTML sb) {
+            sb.append(user);
+            sb.append('@');
+            host.toHTML(sb);
         }
+    }
+
+    public static class Login {
+        public String username;
+        public String password;
+        public void toHTML(ToHTML.HTML sb) {
+            sb.append(username);
+            sb.append(':');
+            sb.append(password);
+            sb.append('@');
+        }        
+    }
+
+
+
+
+    */
+    /*
         public static void prefix(PrintWriter p) {
             p.println("% generated by TIBDOC");
             for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
@@ -341,5 +321,120 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
 
 
     */
+
+    // Main //////////////////////////////////////////////////////////////////////////////
+
+    public static class Dump implements Reflection.Show {
+        public String toString() { return Reflection.show(this); }
+    }
+
+    public static class TD {
+
+        public @nonterminal static class Doc extends Dump {
+            public @arg("head") Header head;
+            public @arg("body") Body body;
+        }
+
+        public @nonterminal static class Header extends Dump {
+            public @arg("attrs") KeyVal[] attrs;
+            // FIXME: it would be nice to be able to
+            // void KeyVal(String, String) { ... } imperatively
+        }
+        
+        public @nonterminal static class Body extends Dump {
+            public Section[] sections;
+            // FIXME: it would be nice to be able to
+            // void KeyVal(String, String) { ... } imperatively
+        }
+        
+        public @nonterminal("Section") static class Section extends Dump {
+            public String      header;
+            public Paragraph[] paragraphs;
+        }
+        
+        public @nonterminal static class KeyVal extends Dump {
+            public @arg("key") String key;
+            public @arg("val") Object val;
+        }
+
+        public static class Paragraph extends Dump { }
+        public @tag("P") static class P extends Paragraph {
+            Object text;
+        }
+        public @tag("HR") static class HR extends Paragraph { }
+        public @tag("Blockquote") static class Blockquote extends Paragraph {
+            Object text;
+        }
+    }
+
+    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.Section.class,
+                                            TibDoc.TD.Body.class,
+                                            TibDoc.TD.Paragraph.class,
+                                            TibDoc.TD.P.class,
+                                            TibDoc.TD.HR.class,
+                                            TibDoc.TD.Blockquote.class,
+                                            TibDoc.TD.KeyVal.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();
+        }
+    }
+
 }