+
+ // Main //////////////////////////////////////////////////////////////////////////////
+
+ public static class Dump implements Reflection.Show {
+ public String toString() { return Reflection.show(this); }
+ }
+
+ public static class TD {
+
+ public @nonterminal static class Doc extends Dump {
+ public @arg Header head;
+ public @arg Body body;
+ }
+
+ public @nonterminal static class Header extends Dump {
+ public @arg KeyVal[] attrs;
+ // FIXME: it would be nice to be able to
+ // void KeyVal(String, String) { ... } imperatively
+ }
+
+ public @nonterminal static class Body extends Dump {
+ public Section[] sections;
+ }
+
+ public @nonterminal("Section") static class Section extends Dump {
+ public String header;
+ public Paragraph[] paragraphs;
+ }
+
+ public @nonterminal static class KeyVal extends Dump {
+ public @arg String key;
+ public @arg Object val;
+ }
+
+ public static class Paragraph extends Dump { }
+ public @tag("P") static class P extends Paragraph {
+ public Text[] text;
+ }
+ public @tag("HR") static class HR extends Paragraph { }
+ public @tag("Blockquote") static class Blockquote extends Paragraph {
+ Object text;
+ }
+
+ public static class Text extends Dump { }
+ public @tag static class Chars extends Text {
+ public String text;
+ }
+ public @tag static class Block extends Text { public Text[] text; }
+ public static class TextWrap extends Text {
+ public Text text;
+ }
+ public static @tag class Verbatim extends Text { public char[] c; }
+ //public @tag class Blockquote extends TextWrap { }
+ public static @tag class Underline extends TextWrap { }
+ public static @tag class Footnote extends TextWrap { }
+ public static @tag class TT extends TextWrap { }
+ //public @tag class Citation extends Text { "[" word "]" }
+ public static @tag class Strikethrough extends TextWrap { }
+ //public @tag class Superscript extends TextWrap { }
+ //public @tag class Subscript extends TextWrap { }
+ public static @tag class Smallcap extends TextWrap { }
+ //public @tag class Keyword extends TextWrap { }
+ public static @tag class Bold extends TextWrap { }
+ public static @tag class Italic extends TextWrap { }
+
+ public static class Command extends Text { }
+ public static @tag class Today extends Command { }
+ public static @tag class LineBreak extends Command { }
+
+ public static class Glyph extends Text { }
+ public static @tag("emdash") class Emdash extends Glyph { }
+ }
+
+ public static void main(String[] s) throws Exception {
+ try {
+
+ Demo.ReflectiveMeta m =
+ new Demo.ReflectiveMeta(TibDoc.TD.class,
+ new Class[] {
+ TibDoc.TD.Doc.class,
+ TibDoc.TD.Header.class,
+ TibDoc.TD.Section.class,
+ TibDoc.TD.Body.class,
+ TibDoc.TD.Paragraph.class,
+ TibDoc.TD.P.class,
+ TibDoc.TD.HR.class,
+ TibDoc.TD.Blockquote.class,
+ TibDoc.TD.KeyVal.class,
+ TibDoc.TD.Text.class,
+ TibDoc.TD.TextWrap.class,
+ TibDoc.TD.Verbatim.class,
+ TibDoc.TD.Blockquote.class,
+ TibDoc.TD.Underline.class,
+ TibDoc.TD.Footnote.class,
+ TibDoc.TD.TT.class,
+ TibDoc.TD.Strikethrough.class,
+ //TibDoc.TD.Superscript.class,
+ //TibDoc.TD.Subscript.class,
+ TibDoc.TD.Smallcap.class,
+ TibDoc.TD.Bold.class,
+ //TibDoc.TD.Keyword.class,
+ TibDoc.TD.Italic.class,
+ TibDoc.TD.Today.class,
+ TibDoc.TD.LineBreak.class,
+ TibDoc.TD.Chars.class,
+ TibDoc.TD.Emdash.class,
+ });
+ Tree<String> 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);
+
+ 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());
+
+ Reducer red = (Reducer)t.head();
+ Object result = red.reduce(t);
+ System.out.println((TD.Doc)result);
+ /*
+ System.out.println("parsing " + s[0]);
+ Tree<String> 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("\nparsing " + s[1]);
+ Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1])));
+ 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);
+
+ FileOutputStream fos = new FileOutputStream("out.html");
+ PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
+ 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));
+ GraphViz gv = new GraphViz();
+ a.ambiguity.toGraphViz(gv);
+ gv.dump(p);
+ p.flush();
+ p.close();
+ a.printStackTrace();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+