From 92632cd2b60dc20e157128696edc81e3a07eaec6 Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 12 Jan 2006 04:05:07 -0500 Subject: [PATCH] checkpoint darcs-hash:20060112090507-5007d-205864d6b875b5d13370ff8a287e4e3f9e82e924.gz --- src/edu/berkeley/sbp/misc/ReflectiveWalker.java | 5 +- src/edu/berkeley/sbp/tib/TibDoc.java | 81 ++++++++++++++++++++--- tests/input.tibdoc | 7 +- tests/tibdoc.g | 57 ++++++++-------- 4 files changed, 108 insertions(+), 42 deletions(-) diff --git a/src/edu/berkeley/sbp/misc/ReflectiveWalker.java b/src/edu/berkeley/sbp/misc/ReflectiveWalker.java index c838a5e..186fa3b 100644 --- a/src/edu/berkeley/sbp/misc/ReflectiveWalker.java +++ b/src/edu/berkeley/sbp/misc/ReflectiveWalker.java @@ -72,12 +72,13 @@ public class ReflectiveWalker extends StringWalker { Member m = member(normalize(tag), 0, false); if (m!=null) Reflection.fuzzyInvoke(target, m); } + protected Object defaultWalk(String tag, Object[] argo) { return super.walk(tag, argo); } public Object walk(String tag, Object[] argo) { if (argo.length==0) return super.walk(tag, argo); if (argo==null) return tag; if (tag==null || "".equals(tag)) return argo; - Member m = tag==null ? null : member(normalize(tag), argo.length, argo.length>0); - if (m==null) return super.walk(tag, argo); + Member m = tag==null ? null : member(normalize(tag), argo.length, false); + if (m==null) return defaultWalk(tag, argo); //System.out.println("preparing to invoke method " + (m==null ? "null" : (m.toString())) + " for sequence " + (owner()+"."+tag)); if (m != null) return Reflection.fuzzyInvoke(target, m, argo); if (argo.length==0) return null; diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index ed6a31e..538d808 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -22,9 +22,19 @@ public class TibDoc { System.out.println("\nparsing " + s[1]); Forest f = new CharToken.CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1]))); + + System.out.println(); System.out.println(f); - //System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString()); - System.out.println(((Tree)new HTMLWalker().walk(f.expand1())).toPrettyString()); + System.out.println(); + System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString()); + + 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(); } public static class StringifyWalker extends ReflectiveWalker { @@ -39,10 +49,59 @@ public class TibDoc { } } + 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(); + } + public static class HTMLWalker extends ReflectiveWalker { //public void header() { throw new Error(); } - public String ul(Object o) { return ""; } - private Tree lone(String s) { + public String li(Object o) { return "
  • "+o+"
  • "; } + public String ul(String[] li) { return ""; } + public String ol(String[] li) { return "
      "+join(li,"")+"
    "; } + public String hr() { return "\n
    \n"; } + public String it(Object o) { return ""+o+""; } + public String tt(Object o) { return ""+o+""; } + public String underline(Object o) { return ""; } + 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(null, sb.toString(), new Tree[0]); + return sb.toString(); + } + private Tree lone(String s) { + return new Tree(null, s, new Tree[0]); } 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 lone(ret.toString()); + return ret.toString(); } return super.walk(t); } - public Object walk(String head, Object[] children) { + protected Object defaultWalk(String head, Object[] children) { Tree[] kids = new Tree[children.length]; for(int i=0; i)children[i]; + else if (children[i] instanceof String) kids[i] = lone(escapify((String)children[i])); + else if (children[i] instanceof Tree) kids[i] = (Tree)children[i]; + else kids[i] = lone(children[i].toString()); } return new Tree(null, head, kids); } diff --git a/tests/input.tibdoc b/tests/input.tibdoc index de38bdc..dcd7fac 100644 --- a/tests/input.tibdoc +++ b/tests/input.tibdoc @@ -10,7 +10,7 @@ header You can visit {my website}->adam@megacz.com with a !hyperlink to it! The following demonstrates verbatim stuff [Knu68], as - well as a footnote ((like )) because are + well as a footnote ((like)) because are coool in an O(n^^3) way. "" this is a test of \sc{paragraph of fun} @@ -20,5 +20,6 @@ header Furthermore, we can try things like - * this - * this \ No newline at end of file + * this + + * this \ No newline at end of file diff --git a/tests/tibdoc.g b/tests/tibdoc.g index e77d53b..1df3fb8 100644 --- a/tests/tibdoc.g +++ b/tests/tibdoc.g @@ -24,7 +24,7 @@ s ::= Doc => top Doc ::= {Header} Body /ws => doc Header ::= "header" { kv */ ws } /ws => header -Body ::= Section*/ws => body +Body ::= Section*/ws => body Section ::= { SectionHeader Paragraph* /ws => section } SectionHeader ::= "==" SectionHeaderBody "==" SectionHeaderBody ::= "=" SectionHeaderBody "=" @@ -34,29 +34,30 @@ sp !::= " "** blank ::= sp "\n" sp "\n" ws kv ::= word "=" text /ws => kv1 - +wp !::= w++ num !::= [0-9]++ -Paragraph ::= { "\"\"" ws text } => "blockquote" - > { "*" " " ws text } => "ul" - > { "#" " " ws text } => "ol" +Paragraph ::= { "\"\"" ws text } => "blockquote" + > uli+/ws => "ul" + > { "# " text } => "ol" > { num " " ws text } => "ol" > { "---" "-"* } => "hr" > { text } => "p" +uli ::= { "* " text } => "li" -text ::= Item -Itemx ::= ws Item | () +text ::= Item => text +Itemx ::= ws Item + | () Item ::= blockquote - > pre Itemx => [] - > structured Itemx => [] - > structuredx Itemx => [] - > styled Itemx => [] - > qtext Itemx => [] + > pre Itemx => [] + > structured Itemx => [] + > structuredx Itemx => [] + > styled Itemx => [] + > qtext Itemx => [] > (alphanum++ => stringify) Itemx => [] - > symbol Itemx => [] -// > sym++ Itemx => [] - > Paragraph Itemx => [] - -symbol ::= sym++ + > symbol Itemx => [] + > (sym++ => stringify) Itemx => [] + > Paragraph => "" + > Paragraph Itemx => [] blockquote ::= "\"\"" text "\"\"" => "blockquote" | "\"\"" block => "blockquote" @@ -64,7 +65,7 @@ blockquote ::= "\"\"" text "\"\"" => "blockquote" qtext ::= "\"" text "\"" => "quoted" pre ::= "[verbatim]" { ~[]+ } /ws => "verbatim" // FIXME doesn't work -styled ::= "__" text "__" => ul +styled ::= "__" text "__" => underline | "((" text "))" => footnote | ( "[[" text "]]" => tt > "[" word "]" => citation @@ -95,22 +96,22 @@ glyph ::= "(r)" | "(c)" | "(tm)" | "--" // euro symbol? // only gets parsed once urlpath ::= urlchar* -username ::= [a-zA-Z0-9;/?:&=$\-_.+]++ -password ::= [a-zA-Z0-9;/?:&=$\-_.+]++ +username ::= [a-zA-Z0-9;/?:&=$\-_.+]++ => stringify +password ::= [a-zA-Z0-9;/?:&=$\-_.+]++ => stringify urlchar ::= [a-zA-Z0-9;/?:&=$\-_.+@] | "%" [0-9] [0-9] => "%" url ::= "mailto" ":" email - > method "://" url_login? host (":" port)? ("/" urlpath)? => "url" -url_login ::= username (":" password) "@" => "login" -method ::= [+\-.a-z0-9]+ -port ::= [0-9]+ - -domain ::= (part +/ ".") -> ~"." -part ::= [a-zA-Z0-9\-]++ // interesting use of boolean grammars + > method "://" url_login? host (":" nums)? ("/" urlpath)? => "url" +url_login ::= username (":" password) "@" => "login" +method ::= [+\-.a-z0-9]+ => stringify +domain ::= (part +/ ".") -> ~"." => domain +part ::= [a-zA-Z0-9\-]++ => stringify +// interesting use of boolean grammars // &~ ([\-0-9] ~[]* | ~[]* [\-0-9]) email ::= username "@" host -> ~[.] => emailaddr -host ::= [0-9]+ "." [0-9]+ "." [0-9]+ "." [0-9]+ => "ip" +nums ::= [0-9]++ => stringify +host ::= nums "." nums "." nums "." nums => "ip" | domain -- 1.7.10.4