public Body body;
public void toHTML(ToHTML.HTML sb) { sb.tag("html", body); }
public static class Header extends HashMap<String, Text[]> {
- public static class kv { public String key; public Text[] val; }
- public void attrs(kv[] kvs) { for(int i=0; i<kvs.length; i++) this.put(kvs[i].key, kvs[i].val); }
+ public static class KeyVal { public String key; public Text[] val; }
+ public void attrs(KeyVal[] KeyVals) { for(int i=0; i<KeyVals.length; i++) this.put(KeyVals[i].key, KeyVals[i].val); }
}
public static class Body implements ToHTML {
public Section[] sections;
// Main //////////////////////////////////////////////////////////////////////////////
+ public static class Dump implements Reflection.Show {
+ public String toString() { return Reflection.show(this); }
+ }
+
public static class TD {
- public @nonterminal("Doc") static class Doc {
+ public @nonterminal static class Doc extends Dump {
public @arg("head") Header head;
public @arg("body") Body body;
- public String toString() { return "doc:{"+head+","/*+body*/+"}"; }
}
- public @nonterminal("Header") static class Header {
- public @arg("attrs") KV[] attrs;
+ public @nonterminal static class Header extends Dump {
+ public @arg("attrs") KeyVal[] attrs;
// FIXME: it would be nice to be able to
- // void kv(String, String) { ... } imperatively
- public String toString() {
- StringBuffer ret = new StringBuffer();
- ret.append("<");
- for(KV kv : attrs) ret .append(kv);
- ret.append(">");
- return ret.toString();
- }
+ // void KeyVal(String, String) { ... } imperatively
}
- public @nonterminal("Body") static class Body {
+ public @nonterminal static class Body extends Dump {
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();
- }
+ // void KeyVal(String, String) { ... } imperatively
}
- public @nonterminal("Section") static class Section {
- public Object @arg header;
- public Object[] @arg paragraphs;
+ public @nonterminal("Section") static class Section extends Dump {
+ public String header;
+ public Paragraph[] paragraphs;
}
- public @nonterminal("kv") static class KV {
+ public @nonterminal static class KeyVal extends Dump {
public @arg("key") String key;
public @arg("val") Object val;
- public String toString() { return "KV["+key+"="+val+"]"; }
+ }
+
+ public static class Paragraph extends Dump { }
+ public @tag("P") static class P extends Paragraph {
+ Object text;
+ }
+ public @tag("HR") static class HR extends Paragraph { }
+ public @tag("Blockquote") static class Blockquote extends Paragraph {
+ Object text;
}
}
TibDoc.TD.Header.class,
TibDoc.TD.Section.class,
TibDoc.TD.Body.class,
- TibDoc.TD.KV.class
+ TibDoc.TD.Paragraph.class,
+ TibDoc.TD.P.class,
+ TibDoc.TD.HR.class,
+ TibDoc.TD.Blockquote.class,
+ TibDoc.TD.KeyVal.class
});
Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
return (Object[])Array.newInstance(c, i);
}
- public static String indent(String s, int indent) {
- if (s.indexOf('\n')==-1) return s;
- StringBuffer ret = new StringBuffer();
- for(int i=0; i<s.length(); i++) {
- char c = s.charAt(i);
- ret.append(c);
- if (c=='\n')
- for(int j=0; j<indent; j++)
- ret.append(' ');
- }
- return ret.toString();
- }
-
public static String show(Object o) {
if (o==null) return "null";
+ if (o instanceof Show) {
+ StringBuffer ret = new StringBuffer();
+ for(Field f : o.getClass().getFields()) {
+ ret.append("\n" + f.getName() + " = ");
+ try {
+ ret.append(show(f.get(o)));
+ } catch (Exception e) {
+ ret.append("**"+e+"**");
+ throw new RuntimeException(e);
+ }
+ }
+ return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
+ }
if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
Object[] arr = (Object[])o;
StringBuffer ret = new StringBuffer();
ret.append(o.getClass().getComponentType().getName());
- ret.append("["+arr.length+"]:\n");
+ ret.append("["+arr.length+"]:");
for(int i=0; i<arr.length; i++)
- ret.append(indent(show(arr[i]), 4) + "\n");
+ ret.append(StringUtil.indent("\n"+show(arr[i]), 4));
return ret.toString();
}
return null;
}
+ public static interface Show { }
}
s = Doc
Doc = head:{Header} body:Body /ws
-Header = "header" attrs:{ kv */ ws } /ws
+Header = "header" attrs:{ KeyVal */ ws } /ws
Body = {Section}*/ws
Section = SectionHeader Paragraph* /ws
SectionHeader = "==" SectionHeaderBody "=="
SectionHeaderBody = "=" SectionHeaderBody "="
- > !ws (Chars:: text:alphanum++) !ws
+ > !ws alphanum++ !ws
sp = " "**
blank = !sp "\n" !sp "\n" !ws
-kv = key:word "=" val:text /ws
+KeyVal = key:word "=" val:text /ws
wp = w++
num = [0-9]++
Paragraph = Blockquote:: { "\"\" " text }
> HR:: { "---" "-"* }
- > { P:: t:text }
+ > P:: { text }
onums = nums !(". "|") ")
any = ~[]*