X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTibDoc.java;h=3a53cf24029593c07eedba07461c8d33cdbe1413;hb=2177e4743ea680e24f0cc0525185e8f95d0dd41b;hp=1a2fe23a4ee3193b1442126e16bc337923da42e2;hpb=afc33b68c5e6f480626c6c03429e0edb4119b553;p=sbp.git diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index 1a2fe23..3a53cf2 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -3,59 +3,270 @@ // 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.*; public class TibDoc { - - public static void main(String[] s) throws Exception { - System.out.println("parsing " + s[0]); - Tree 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(f); - //System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString()); - System.out.println(((Tree)new HTMLWalker().walk(f.expand1())).toPrettyString()); + + 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 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 kv { public String key; public Text[] val; } + public void attrs(kv[] kvs) { for(int i=0; i"); + for(int i=0; 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 { + 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 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 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()); + 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 class Italic extends Text { + public Text[] body; + public void toHTML(ToHTML.HTML sb) { + sb.append(""); + sb.append(body); + sb.append(""); + } + } + + public static class LineBreak extends Text { + public void toHTML(ToHTML.HTML sb) { + sb.append("
"); + } + } + public static class Today extends Text { } + public static class Euro extends Text { + public void toHTML(ToHTML.HTML sb) { + sb.append("€"); + } + } + 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(""); + sb.append(word); + sb.append(""); + } + } + public static class LinkText extends Link { + public Text[] text; + public URI href; + public void toHTML(ToHTML.HTML sb) { + sb.append(""); + sb.append(text); + sb.append(""); } - if (children.length==0) return new Tree(null, head, new Tree[0]); - return new Tree(null, head, (Tree[])Reflection.lub(children)); } } - public static class HTMLWalker extends ReflectiveWalker { - public void stringify() { /*mode = HEADER;*/throw new Error(); } - 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 new Tree(null, ret.toString()); + + // 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 TextArray implements Paragraph { + public void toHTML(ToHTML.HTML sb) { + sb.append("\n

"); + super.toHTML(sb); + sb.append("

\n"); } - return super.walk(t); } - public Object walk(String head, Object[] children) { - if (children.length==0) return new Tree(null, head, new Tree[0]); - return new Tree(null, head, (Tree[])Reflection.lub(children)); + 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("
  • "); + sb.append(t); + sb.append("
  • \n"); + } + 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"; } } + + + // Main ////////////////////////////////////////////////////////////////////////////// + + public static void main(String[] s) throws Exception { + try { + 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(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(); + 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); + /* + 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(); + } + } + + /* public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }