more improvements to TibDoc
authoradam <adam@megacz.com>
Sun, 5 Mar 2006 07:29:27 +0000 (02:29 -0500)
committeradam <adam@megacz.com>
Sun, 5 Mar 2006 07:29:27 +0000 (02:29 -0500)
darcs-hash:20060305072927-5007d-016843b612d3e5fd3cfd5b3d907e844379366cb3.gz

src/edu/berkeley/sbp/misc/ReflectiveGrammar.java
src/edu/berkeley/sbp/tib/TibDoc.java
src/edu/berkeley/sbp/util/Reflection.java
src/edu/berkeley/sbp/util/ToHTML.java
tests/input.tibdoc
tests/tibdoc.g

index 146335c..ea1b03e 100644 (file)
@@ -55,7 +55,6 @@ public class ReflectiveGrammar extends MetaGrammar {
         } else if (h==arrayMark && c==null) {
             c = Object[].class;            
         }
-        if (c==null) System.out.println(h + " -- " + t.toPrettyString());
         if (c.isArray()) {
             Object[] ret = new Object[t.numChildren()];
             for(int i=0; i<ret.length; i++)
@@ -73,7 +72,7 @@ public class ReflectiveGrammar extends MetaGrammar {
     }
 
     public Object buildBody(Tree<Object> t, Class c) throws Exception {
-        System.out.println("buildBody " + (c==null?null:c.getName()) + " " + t);
+        //System.out.println("buildBody " + (c==null?null:c.getName()) + " " + t);
         c = resolveClass(t, c);
         if (c==null) return buildHead(t, null);
         Object o = c.newInstance();
@@ -98,7 +97,7 @@ public class ReflectiveGrammar extends MetaGrammar {
             else {
                 Object tgt = Reflection.rebuild(buildHead(t.child(i), field.getType()), field.getType());
                 //if (tgt instanceof Object[]) tgt = Reflection.lub(tgt);
-                System.err.println("setting field " + field.getName() + " on " + c.getName() + " to " + tgt);
+                //System.err.println("setting field " + field.getName() + " on " + c.getName() + " to " + tgt);
                 try {
                     field.set(o, tgt);
                 } catch (Exception e) {
@@ -113,15 +112,15 @@ public class ReflectiveGrammar extends MetaGrammar {
         if (c==null) return null;
         if (c==int.class) return c;
         if (c==String.class) return c;
-        System.out.println("resolving " + c.getName());
+        //System.out.println("resolving " + c.getName());
         if (Reflection.isConcrete(c)) return c;
         Class ret = null;
         Class[] subs = (Class[])c.getField("subclasses").get(null);
         OUTER: for(int i=0; i<subs.length; i++) {
-            System.err.println("trying " + subs[i].getName());
+            //System.err.println("trying " + subs[i].getName());
             for(int j=0; j<t.numChildren(); j++)
                 if (Reflection.getField(subs[i], t.label(j)+"")==null) {
-                    System.err.println("skipping due to " + t.label(j));
+                    //System.err.println("skipping due to " + t.label(j));
                     continue OUTER;
                 }
             if (ret != null)
index 3a53cf2..4dccac8 100644 (file)
@@ -12,8 +12,9 @@ import java.io.*;
 
 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 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); }
 
 
@@ -22,16 +23,16 @@ public class TibDoc {
     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 static class TextArray extends Text {
-        public Text[] t;
-        public String tag() { return null; }
-        public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), t); }
+        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 static class TextArray extends Text {
+            public Text[] t;
+            public String tag() { return null; }
+            public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), t); }
+        }
     }
 
 
@@ -59,35 +60,74 @@ public class TibDoc {
         }
     }
 
-    // Tags //////////////////////////////////////////////////////////////////////////////
 
-    public static class Keyword       extends TextString { public String tag() { return "tt"; } }
-    public static class Bold          extends TextArray  { public String tag() { return "b"; } }
-    public static class Smallcap      extends TextArray  { public String tag() { return "sc"; } }
-    public static class Subscript     extends TextString { public String tag() { return "sub"; } }
-    public static class Superscript   extends TextString { public String tag() { return "super"; } }
-    public static class Strikethrough extends TextArray  { public String tag() { return "strike"; } }
-    public static class TT            extends TextArray  { public String tag() { return "tt"; } }
-    public static class Underline     extends TextArray  { public String tag() { return "u"; } }
-    public static class Citation      extends TextArray  { /* FIXME */ }
-    public static class Footnote      extends TextArray  { /* FIXME */ }
-
-    public static class Chars extends TextString { }
-    public static class Symbol extends TextString { }
-
-    public static interface Host extends ToHTML { }
-    public static class DNS implements Host {
-        public String[] part;
+    // 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("<tt>");
-            for(int i=0; i<part.length; i++)
-                sb.append((i==0 ? "" : ".")+part[i]);
-            sb.append("</tt>");
+            sb.append("<"+tag()+">\n");
+            for(Text[] t : points) sb.tag("li", t);
+            sb.append("</"+tag()+">\n");
         }
     }
-    public static class IP  implements Host {
-        public int a, b, c, d;
-        public void toHTML(ToHTML.HTML sb) { sb.append("<tt>"+a+"."+b+"."+c+"."+d+"</tt>"); }
+    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 Object word;
+        public URI href;
+        public void toHTML(ToHTML.HTML sb) { sb.tag("a", new Object[] { "href", href }, word); }
+    }
+    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]);
+            }
+        }
+        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 interface URI extends ToHTML {
@@ -120,7 +160,7 @@ public class TibDoc {
         }
     }
 
-    public static class Login               {
+    public static class Login {
         public String username;
         public String password;
         public void toHTML(ToHTML.HTML sb) {
@@ -130,90 +170,7 @@ public class TibDoc {
             sb.append('@');
         }        
     }
-    public static class Italic extends Text {
-        public Text[] body;
-        public void toHTML(ToHTML.HTML sb) {
-            sb.append("<i>");
-            sb.append(body);
-            sb.append("</i>");
-        }
-    }
 
-    public static class LineBreak extends Text {
-        public void toHTML(ToHTML.HTML sb) {
-            sb.append("<br/>");
-        }
-    }
-    public static class Today extends Text { }
-    public static class Euro extends Text {
-        public void toHTML(ToHTML.HTML sb) {
-            sb.append("&#8364;");
-        }
-    }
-    public static class Link extends Text {
-        public static final Class[] subclasses = new Class[] { LinkWord.class, LinkText.class };
-        public static class LinkWord extends Link {
-            public String word;
-            public URI href;
-            public void toHTML(ToHTML.HTML sb) {
-                sb.append("<a href='");
-                href.toHTML(sb);
-                sb.append("'>");
-                sb.append(word);
-                sb.append("</a>");
-            }
-        }
-        public static class LinkText extends Link {
-            public Text[] text;
-            public URI href;
-            public void toHTML(ToHTML.HTML sb) {
-                sb.append("<a href='");
-                href.toHTML(sb);
-                sb.append("'>");
-                sb.append(text);
-                sb.append("</a>");
-            }
-        }
-    }
-
-
-    // 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 TextArray implements Paragraph {
-            public void toHTML(ToHTML.HTML sb) {
-                sb.append("\n<p> ");
-                super.toHTML(sb);
-                sb.append(" </p>\n");
-            }
-        }
-        public static class Blockquote extends TextArray implements Paragraph {
-            public String tag() { return "blockquote"; }
-        }
-    }
-
-
-    // Lists //////////////////////////////////////////////////////////////////////////////
-
-    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.append("  <li> ");
-                sb.append(t);
-                sb.append("  </li>\n");
-            }
-            sb.append("</"+tag()+">\n");
-        }
-    }
-    public static class OL extends List { public String tag() { return "ol"; } }
-    public static class UL extends List { public String tag() { return "ul"; } }
 
 
     // Main //////////////////////////////////////////////////////////////////////////////
@@ -224,12 +181,8 @@ public class TibDoc {
             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(gram);
-            Union mg = gram.done();
-            
             System.out.println("\nparsing " + s[1]);
-            Forest f = new CharParser(mg).parse(new Tib(new FileInputStream(s[1])));
-            //((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString()
+            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();
@@ -242,15 +195,13 @@ public class TibDoc {
             StringBuffer sb = new StringBuffer();
             doc.toHTML(new ToHTML.HTML(sb));
             System.out.println(sb);
-            /*
-            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.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));
@@ -266,65 +217,7 @@ public class TibDoc {
         }
     }
 
-
     /*
-    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 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 void prefix(PrintWriter p) {
             p.println("% generated by TIBDOC");
             for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
index 5c30d44..42267ce 100644 (file)
@@ -6,7 +6,7 @@ import java.lang.reflect.*;
 public final class Reflection {
     
     public static Object rebuild(Object o, Class c) {
-        System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c);
+        //System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c);
         if (o==null || c.isAssignableFrom(o.getClass())) return o;
         if ((c == Character.class || c == Character.TYPE) && o instanceof String && ((String)o).length()==1) return new Character(((String)o).charAt(0));
         if (o instanceof Character && c == String.class) return o+"";
@@ -16,7 +16,7 @@ public final class Reflection {
                 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), a.length);
                 for(int i=0; i<ret.length; i++) {
                     Object o2 = rebuild(a[i], c.getComponentType());
-                    if (o2 != null) System.out.println("storing " + o2.getClass().getName() + " to " + c.getComponentType());
+                    //if (o2 != null) System.out.println("storing " + o2.getClass().getName() + " to " + c.getComponentType());
                     ret[i] = o2;
                 }
                 return ret;
index bec40bf..fe3b3a3 100644 (file)
@@ -8,10 +8,15 @@ public interface ToHTML {
     public static class HTML {
         private final StringBuffer sb;
         public HTML(StringBuffer sb) { this.sb = sb; }
+
         public void append(String s) {
             /* FIXME */
             sb.append(s);
         }
+
+        public void entity(int entity) { sb.append("&#"+entity+";"); }
+        public void entity(String entity) { sb.append("&"+entity+";"); }
+
         public void append(Object o) {
             if (o==null)                    append("<tt><font color=red>null</font></tt>");
             else if (o instanceof ToHTML)   ((ToHTML)o).toHTML(this);
@@ -28,17 +33,31 @@ public interface ToHTML {
             }
         }
 
-        public void tag(String s, Object o) {
+        public void tag(String s) {
+            sb.append("<");
+            append(s);
+            sb.append("/>");
+        }
+        public void tag(String s, Object o) { tag(s, null, o); }
+        public void tag(String s, Object[] attrs, Object o) {
             if (s != null) {
-                append("<");
+                sb.append("<");
                 append(s);
-                append(">");
+                if (attrs != null)
+                    for(int i=0; i<attrs.length; i+=2) {
+                        sb.append(' ');
+                        append(attrs[i]);
+                        sb.append("=\'");
+                        append(attrs[i+1]);
+                        sb.append("\'");
+                    }
+                sb.append(">");
             }
             append(o);
             if (s != null) {
-                append("</");
+                sb.append("</");
                 append(s);
-                append(">");
+                sb.append(">");
             }
         }
 
index 73d9ed6..4650275 100644 (file)
@@ -6,7 +6,7 @@ header
 == Introduction ==
 
   this is the body adam@megacz.com text \today
-  You can visit {my website}->adam@megacz.com with a !hyperlink to it!
+  You can visit {my website}->adam@megacz.com with -- a !hyperlink to it!
 
   The following demonstrates->http://www.slashdot.org/ verbatim stuff [[Knu68]], as
   well \br as \br a \br footnote ((like)) because are
index 216a77c..68f2b03 100644 (file)
@@ -124,7 +124,7 @@ structured    = command & "\\" [a-zA-Z0-9]++ block?
               > email
               > url
 
-glyph        = Euro:: "(e)" | "(r)" | "(c)" | "(tm)" | "--" | "..."
+glyph        = Euro:: "(e)" | "(r)" | "(c)" | "(tm)" | emdash:: "--" | "..."
 
 command      = Today::     "\\today"
              | LineBreak:: "\\br"