X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTibDoc.java;h=10576732572ee0437ff1cdf76fddad0ac42ceb5d;hp=a86fb2b21b258a3297abae91e23e7ef4e40f3153;hb=59eead6bf0f2d9a7abeba03f3fdab04625521faf;hpb=87695da08b22551ad4a7695cd3144903364dd4bb diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index a86fb2b..1057673 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -5,11 +5,13 @@ 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.misc.Demo.*; +import static edu.berkeley.sbp.meta.MetaGrammar.*; public class TibDoc { /* @@ -44,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 { - public static class kv { public String key; public Text[] val; } - public void attrs(kv[] kvs) { for(int i=0; i"); - return ret.toString(); - } + // void KeyVal(String, String) { ... } imperatively } - public @nonterminal("Body") static class Body { + public @bind.as("B") static class Body extends Dump implements ToHTML { public Section[] sections; - // FIXME: it would be nice to be able to - // void kv(String, String) { ... } imperatively - public String toString() { - StringBuffer ret = new StringBuffer(); - ret.append("<"); - for(Object kv : sections) ret .append(kv); - ret.append(">"); - return ret.toString(); - } + public void toHTML(HTML h) { for(Section s : sections) s.toHTML(h); } } - public @nonterminal("Section") static class Section { - public Object @arg header; - public Object[] @arg paragraphs; + public @bind.as("Section") static class Section extends Dump implements ToHTML { + public String header; + public Paragraph[] paragraphs; + public void toHTML(HTML h) { + h.tag("h1", header); + for(Paragraph p : paragraphs) h.tag("p", p); + } } - public @nonterminal("kv") static class KV { - public @arg("key") String key; - public @arg("val") Object val; - public String toString() { return "KV["+key+"="+val+"]"; } + public @bind 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("email") static Object email(@bind.arg Object a, @bind.arg Object c) { return null; } + public static @bind class Login { + public @bind.arg String username; + public @bind.arg String password; + } + + public @bind.as("Euro") Object euro() { return null; } + public @bind.as("Citation") Object cite(Object o) { return new Chars("*cite*"); } + public @bind.as("Symbol") Object sym(Object o) { return null; } + + public static abstract class List extends Text { + public @bind.arg 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 @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 { - 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.KV.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); + + 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()); - Reducer red = (Reducer)t.head(); - Object result = red.reduce(t); - System.out.println((TD.Doc)result); + 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(); @@ -404,15 +548,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix 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)); @@ -433,6 +569,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix } catch (Exception e) { e.printStackTrace(); } + } }