X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTibDoc.java;h=10576732572ee0437ff1cdf76fddad0ac42ceb5d;hp=705ac3e303d1411eebbf31e8a23737f6c5d167cf;hb=59eead6bf0f2d9a7abeba03f3fdab04625521faf;hpb=45d799349e635f1a99e3974e4504a43d5a7aaf33 diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index 705ac3e..1057673 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -5,241 +5,180 @@ 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"); } + public static char urlescape(int a, int b) { return (char)(10*a+b); } + + + // Template Classes ////////////////////////////////////////////////////////////////////////////// + + 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 Doc { + // Structural ////////////////////////////////////////////////////////////////////////////// + + public static class Doc implements ToHTML { public Header head; public Body body; - } - public static class kv { public String key; public Text[] val; } - public static class Header { - public void attrs(kv[] kvs) { - for(int i=0; i { + public static class KeyVal { public String key; public Text[] val; } + public void attrs(KeyVal[] KeyVals) { for(int i=0; i 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() - System.out.println(); - Doc doc = (Doc)new ReflectiveGrammar(TibDoc.class).build(f.expand1()); - System.out.println(doc); - /* - 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(); - */ - } 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(); - } + + // 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
\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 class StringifyWalker extends ReflectiveWalker { - public Object walk(String head, Object[] children) { - if ("stringify".equals(head)) { - StringBuffer ret = new StringBuffer(); - for(Tree t : (Tree)children[0]) ret.append(t); - return new Tree(null, ret.toString()); - } - if (children.length==0) return new Tree(null, head, new Tree[0]); - return new Tree(null, head, (Tree[])Reflection.lub(children)); + 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("\n"); } } + public static class OL extends List { public String tag() { return "ol"; } } + public static class UL extends List { public String tag() { return "ul"; } } - 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(); + + + // 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); } } - public static class HTMLWalker extends ReflectiveWalker { - //public void header() { throw new Error(); } - public String li(Object o) { return "
  • "+o+"
  • "; } - public String li(Object a, Object o) { return "
  • "+o+"
  • "; } - public String ul(String[] li) { return "
      "+join(li,"")+"
    "; } - public String ol(String[] li) { return "
      "+join(li,"")+"
    "; } - public String hr() { return "\n
    \n"; } - public String br() { return "\n
    \n"; } - public String it(Object o) { return ""+o+""; } - public String tt(Object o) { return ""+o+""; } - public String underline(Object o) { return "
      "+o+"
    "; } - public String p(Object o) { return "

    "+o+"

    "; } - public String smallcap(Object o) { return ""+o+""; } - public String blockquote(Object o) { return "
    "+o+"
    "; } - public String superscript(Object o) { return ""+o+""; } - public String subscript(Object o) { return ""+o+""; } - public String bold(Object o) { return ""+o+""; } - public String strikethrough(Object o) { throw new Error();/*return ""+o+"";*/ } - public Object top(Object o) { return ""+o+""; } - 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 String url(String method) { - public String link(Object text, Object target) { - return ""+text+""; - } - 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(); - } - private String escapify(Object o) { - String s = o==null ? "" : o.toString(); - StringBuffer sb = new StringBuffer(); - for(int i=0; i': sb.append(">"); break; - case '\'': sb.append("'"); break; - case '\"': sb.append("""); break; - default: sb.append(s.charAt(i)); break; - } + + // 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 lone(String s) { - return new Tree(null, s, new Tree[0]); + 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 Object walk(Tree t) { - String head = t.head(); - if ("stringify".equals(head)) { - StringBuffer ret = new StringBuffer(); - for(Tree child : t.child(0)) ret.append(child); - return ret.toString(); - } - return super.walk(t); + } + + 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); } - protected Object defaultWalk(String head, Object[] children) { - Tree[] kids = new Tree[children.length]; - for(int i=0; i)children[i]; - else kids[i] = lone(children[i].toString()); - } - return new Tree(null, head, kids); + } + 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 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\n"); + for(Text t : points) sb.tag("li", t); + sb.append("\n"); + } + } + public static @bind class OL extends List { public String tag() { return "ol"; } } + public static @bind class UL extends List { public String tag() { return "ul"; } } + + public static class HR extends Paragraph { + public @bind HR() { } + public @bind HR(Object o) { } + public void toHTML(HTML h) { h.tag("hr"); } + } + + public static class Blockquote extends Paragraph { + Text[] text; + public @bind Blockquote(Text[] t) { this.text = t; } + public void toHTML(HTML h) { h.tag("blockquote", new P(text)); } + } + + public abstract static class Text extends Dump implements ToHTML { } + public @bind 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 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 @bind.arg Text[] text; + public void toHTML(HTML h) { + if (htmlTag()!=null) { + h.openTag(htmlTag(), htmlTagParams()); + } + for(Text t : text) t.toHTML(h); + if (htmlTag()!=null) { + h.closeTag(htmlTag()); + } + } + public String htmlTag() { return null; } + public Object[] htmlTagParams() { return null; } + } + public static @bind class Verbatim extends Text { public char[] c; public void toHTML(HTML h) { } } + //public @bind class Blockquote extends TextWrap { } + public static @bind class Underline extends TextWrap { public String htmlTag() { return "u"; } } + public static @bind class Footnote extends TextWrap { public String htmlTag() { return "small"; } } + public static @bind class TT extends TextWrap { public String htmlTag() { return "tt"; } } + //public @bind class Citation extends Text { "[" word "]" } + public static @bind class Strikethrough extends TextWrap { public String htmlTag() { return "strike"; } } + public static @bind class Superscript extends TextWrap { public String htmlTag() { return "sup"; } } + public static @bind class Subscript extends TextWrap { public String htmlTag() { return "sub"; } } + public static @bind class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } } + public static @bind class Keyword extends TextWrap { public String htmlTag() { return "sc"; } } + public static @bind class Bold extends TextWrap { public String htmlTag() { return "b"; } } + public static @bind class Italic extends TextWrap { public String htmlTag() { return "i"; } } + + public abstract static class Command extends Text { } + public static @bind class Today extends Command { public void toHTML(HTML h) { } } + public static @bind 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.appendLiterally("—"); } } + + 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 { + + Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); + + Union tibgram = MetaGrammar.make(res, "s", new AnnotationGrammarBindingResolver(TD.class)); + + 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); + TD.Doc doc = (TD.Doc)result; + System.out.println(doc); + + StringBuffer sb = new StringBuffer(); + ToHTML.HTML html = new ToHTML.HTML(sb); + doc.toHTML(html); + FileOutputStream fos = new FileOutputStream("out.html"); + PrintWriter p = new PrintWriter(new OutputStreamWriter(fos)); + p.println(sb); + p.flush(); + p.close(); + + + + /* + System.out.println("parsing " + s[0]); + Tree 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(); + } + + } + }