From d87c46fcaa7a709fd42a296c4684b725f39a429e Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 6 Jul 2006 03:33:15 -0400 Subject: [PATCH] checkpoint darcs-hash:20060706073315-5007d-19a507d76df9c8cbd9f7ec4d1db7c97f00853792.gz --- src/edu/berkeley/sbp/meta/MetaGrammarBindings.java | 12 ++- src/edu/berkeley/sbp/tib/TibDoc.java | 106 +++++++++++++------- tests/tibdoc.g | 31 +++--- 3 files changed, 97 insertions(+), 52 deletions(-) diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 2866107..8be2fba 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -212,7 +212,17 @@ public class MetaGrammarBindings { public static @bind.as("{") class XTree extends El { public @bind.arg Seq body; public Element build(MetaGrammar.Context cx) { - throw new Error(); + Union u = new Union(); + Sequence s = body.build(cx, u, false); + Union u2 = new Union(); + u2.add(Sequence.singleton(new Element[] { + CharRange.leftBrace, + cx.get("ws"), + u, + cx.get("ws"), + CharRange.rightBrace + }, 2)); + return u2; } } diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index df32b10..27f6ae1 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -332,27 +332,33 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix public static class TD { - public @bind.as static class Doc extends Dump { + public @bind static class Doc extends Dump implements ToHTML { public @bind.arg Header head; public @bind.arg Body body; + public void toHTML(HTML h) { body.toHTML(h); } } - public @bind.as static class Header extends Dump { + public @bind.as("H") static class Header extends Dump { public @bind.arg KeyVal[] attrs; // FIXME: it would be nice to be able to // void KeyVal(String, String) { ... } imperatively } - public @bind.as static class Body extends Dump { + public @bind.as("B") static class Body extends Dump implements ToHTML { public Section[] sections; + public void toHTML(HTML h) { for(Section s : sections) s.toHTML(h); } } - public @bind.as("Section") static class Section extends Dump { + 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 @bind.as static class KeyVal extends Dump { + public @bind static class KeyVal extends Dump { public @bind.arg String key; public @bind.arg Object val; } @@ -372,24 +378,49 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix } } - public @bind.as("HR") static class HR extends Paragraph { + 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 null; } + 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 @bind.as("Blockquote") static class Blockquote extends Paragraph { + 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.as static class Chars extends Text { + 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.as static class Block extends Text { + public @bind static class Block extends Text { public Text[] text; public void toHTML(HTML h) { for(Text t : text) t.toHTML(h); } } @@ -404,23 +435,23 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix public String htmlTag() { return null; } public Object[] htmlTagParams() { return null; } } - public static @bind.as class Verbatim extends Text { public char[] c; public void toHTML(HTML h) { } } - //public @bind.as class Blockquote extends TextWrap { } - public static @bind.as class Underline extends TextWrap { public String htmlTag() { return "u"; } } - public static @bind.as class Footnote extends TextWrap { public String htmlTag() { return "small"; } } - public static @bind.as class TT extends TextWrap { public String htmlTag() { return "tt"; } } - //public @bind.as class Citation extends Text { "[" word "]" } - public static @bind.as class Strikethrough extends TextWrap { public String htmlTag() { return "strikethrough"; } } - public static @bind.as class Superscript extends TextWrap { public String htmlTag() { return "sup"; } } - public static @bind.as class Subscript extends TextWrap { public String htmlTag() { return "sub"; } } - public static @bind.as class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } } - public static @bind.as class Keyword extends TextWrap { public String htmlTag() { return "sc"; } } - public static @bind.as class Bold extends TextWrap { public String htmlTag() { return "b"; } } - public static @bind.as class Italic extends TextWrap { public String htmlTag() { return "i"; } } + 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 "strikethrough"; } } + 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.as class Today extends Command { public void toHTML(HTML h) { } } - public static @bind.as class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } } + 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.append("&emdash;"); } } @@ -479,27 +510,30 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix } public static void main(String[] s) throws Exception { - /* try { - FIXME FIXME - - MetaGrammar.ReflectiveMeta m = - new MetaGrammar.ReflectiveMeta(TibDoc.TD.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()); Object result = ((Functor)t.head()).invoke(t); - System.out.println((TD.Doc)result); - */ + 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]); @@ -518,7 +552,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix 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)); @@ -532,7 +566,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix } catch (Exception e) { e.printStackTrace(); } - */ + } } diff --git a/tests/tibdoc.g b/tests/tibdoc.g index 4dc497f..17be297 100644 --- a/tests/tibdoc.g +++ b/tests/tibdoc.g @@ -56,16 +56,16 @@ nw = ~[\r\n\ ] s = Doc -Doc = head:{Header} body:Body /ws -Header = "header" attrs:{ KeyVal */ ws } /ws -Body = {Section}*/ws +Doc = head:Header body:Body /ws +Header = H:: { "header" { KeyVal */ ws } /ws } +Body = B:: {Section}*/ws Section = SectionHeader Paragraph* /ws SectionHeader = "==" SectionHeaderBody "==" SectionHeaderBody = "=" SectionHeaderBody "=" - > !ws alphanum++ !ws + > ws! alphanum++ ws! sp = " "** -blank = !sp "\n" !sp "\n" !ws +blank = sp! "\n" sp! "\n" ws! KeyVal = key:word "=" val:text /ws wp = w++ @@ -75,17 +75,18 @@ Paragraph = Blockquote:: { "\"\" " text } > HR:: { "---" "-"* } > P:: { text } -onums = nums !(". "|") ") +onums = nums (". "|") ")! any = ~[]* -uli = "* " (!ws text &~ any !(oli|uli)) -oli = !("# "|onums) (!ws text &~ any !(oli|uli)) +uli = "* " (ws! text &~ any (oli|uli)!) +oli = ("# "|onums)! (ws! text &~ any (oli|uli)!) text = Item -Itemx = !ws Item +Itemx = ws! Item | () -Item = "[]":: blockquote Itemx - > "[]":: { UL:: uli+/ws } Itemx +Item =// "[]":: blockquote Itemx +// > + "[]":: { UL:: uli+/ws } Itemx | "[]":: { OL:: oli+/ws } Itemx > "[]":: pre Itemx > "[]":: link Itemx @@ -96,7 +97,7 @@ Item = "[]":: blockquote Itemx > "[]":: (Symbol:: sym++) Itemx > "[]":: { Block:: text } Itemx -blockquote = Blockquote:: "\"\"" (block | text "\"\"") +//blockquote = Blockquote:: "\"\"" (block | text "\"\"") pre = Verbatim:: "[verbatim]" { ~[]+ } /ws // FIXME doesn't work @@ -115,9 +116,9 @@ styled = Underline:: "__" text "__" block = { text } link = LinkText:: text:({ text }) "->" href:(url|email) - > LinkChars:: text:alphanum++ !ws "->" href:(url|email) + > LinkChars:: text:alphanum++ ws! "->" href:(url|email) -structured = command & "\\" !([a-zA-Z0-9]++) block? +structured = command & "\\" ([a-zA-Z0-9]++)! block? > glyph > email > url @@ -156,7 +157,7 @@ part = [a-zA-Z0-9\-]++ // interesting use of boolean grammars // &~ ([\-0-9] ~[]* | ~[]* [\-0-9]) -email = Email:: user:username "@" host:host -> ~[.] +email = user:username "@" host:host -> ~[.] nums = [0-9]++ host = IP:: nums "." nums "." nums "." nums | DNS:: domain -- 1.7.10.4