X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTibDoc.java;h=d3081866b9670e744fa3b94c8775954f1e9215ad;hb=979e9c595d25573302530e514b9d7fb847d5b043;hp=504e3a432dcb3948149668213c17dab5e1533b5c;hpb=0a0227b9180534d2a431f3d6e08a398bde2244c4;p=sbp.git diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index 504e3a4..d308186 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -3,70 +3,180 @@ // 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 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); + 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); } + } + } + + + // 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 { + public static class KeyVal { public String key; public Text[] val; } + public void attrs(KeyVal[] KeyVals) { for(int i=0; i\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("\n"); + } + } + 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 "\\usemodule"++x++"\n") packages) ++ + "\\setuppapersize[letter]\n" ++ + "\\setuppagenumbering[location=]\n" ++ + "\\setupcolors[state=start]\n" ++ + --"\\setupinteraction[title={Title},author={Me},"++ + --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++ + --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++ + "\\setuphead[section][style={\\ss\\bfa},\n" ++ + " number=no,\n" ++ + " before=\\blank\\hairline\\nowhitespace,\n" ++ + " ]\n" ++ + "\\definelayout[mypage][\n" ++ + " backspace=1.75in, % the space for margin notes\n" ++ + " cutspace=1.75in, % the space for right margin notes\n" ++ + " width=5in" ++ + "]\n" ++ + "\\setuplayout[mypage]\n" ++ + "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++ + "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++ + "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++ + "\\usesymbols[uni]\n" ++ + "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++ + "\\setupbodyfont[myface, 11pt]\n" ++ + "\\setupwhitespace[7pt]\n" ++ + "\\def\\MyDroppedCaps%\n" ++ + " {\\DroppedCaps\n" ++ + " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++ + "\\starttext\n" ++ + "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++ + "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++ + "\\blank[1cm,force]\n" ++ + "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++ + "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++ + "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++ + "\\startmypar"++ + "\\switchtobodyfont[small]\n" + suffix = "\n\\stoptext\n" + addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i) + escapify [] = [] + escapify ('%':t) = '\\':'%':(escapify t) + escapify ('$':t) = '\\':'$':(escapify t) + escapify (h:t) = h:(escapify t) + escapeMath s = s + tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n" + tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n" + tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t) + tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t) + tl (Special _ (Glyph EmDash) []) = "{\\emdash}" +--tl (Special _ Superscript l) = +--tl (Special _ Subscript l) = +--tl (Special _ (Image u) l) = +--tl (Special _ (Cite u) l) = + tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++ + (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n" + tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\" + tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}" + tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}" + tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}" + tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}" + tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}" + tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''" + tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}" + tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}" + tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n" + tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}" + tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++ + --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n" +--tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}" + tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n" + tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}" + tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n" +-- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns" +-- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n" + tl (Special _ Abstract l) = + "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++ + "\n\n"++(concatMap tl l)++"\\mypar" ++ + "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]" + tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]" + tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})" + tl (WS _) = " " + tl (BlankLine _) = "\n\n" + tl (Block _ l) = concatMap tl l + tl (Letter _ c) = escapify [c] + tl z = (show z) + + + + */ + + // 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; + } + + public @nonterminal("Section") static class Section extends Dump { + public String header; + public Paragraph[] paragraphs; + } + + public @nonterminal static class KeyVal extends Dump { + public @arg String key; + public @arg 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 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 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(); + } + } + }