X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTibDoc.java;h=3a53cf24029593c07eedba07461c8d33cdbe1413;hb=2177e4743ea680e24f0cc0525185e8f95d0dd41b;hp=c3b7871819a0699986ac1a9ac69a27eeb5012ac4;hpb=0db242407cbae04f1a55086bf2bd31f5ae1bb7a8;p=sbp.git diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index c3b7871..3a53cf2 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -3,39 +3,267 @@ // 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 extends ReflectiveWalker { - +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 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 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(""); + } + } + } + + + // 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"); + } + } + 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 { - 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)walk(f.expand1())).toString(0, 0, 120)); - System.out.println(((Tree)new TibDoc().walk(f.expand1())).toPrettyString()); - } - - public Tree walk(Tree tree) { - String head = tree.head(); - if ("stringify".equals(head)) { - StringBuffer ret = new StringBuffer(); - for(Tree t : tree.child(0)) ret.append(t); - return new Tree(null, ret.toString()); - } - Tree[] children = new Tree[tree.numChildren()]; - for(int i=0; i(null, head, children); + 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(); + } }