+
+ public static void main(String[] s) throws Exception {
+ System.out.println("parsing " + s[0]);
+ Tree<String> res = new CharParser(MetaGrammar.make()).parse(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 CharParser(mg).parse(new Tib(new FileInputStream(s[1])));
+
+ System.out.println();
+ System.out.println(f);
+ 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 {
+ public Object walk(String head, Object[] children) {
+ if ("stringify".equals(head)) {
+ StringBuffer ret = new StringBuffer();
+ for(Tree<String> t : (Tree<String>)children[0]) ret.append(t);
+ return new Tree<String>(null, ret.toString());
+ }
+ if (children.length==0) return new Tree<String>(null, head, new Tree[0]);
+ return new Tree<String>(null, head, (Tree<String>[])Reflection.lub(children));
+ }
+ }
+
+ 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 li(Object o) { return "<li>"+o+"</li>"; }
+ public String li(Object a, Object o) { return "<li>"+o+"</li>"; }
+ public String ul(String[] li) { return "<ul>"+join(li,"")+"</ul>"; }
+ public String ol(String[] li) { return "<ol>"+join(li,"")+"</ol>"; }
+ public String hr() { return "\n<hr/>\n"; }
+ public String it(Object o) { return "<i>"+o+"</i>"; }
+ public String tt(Object o) { return "<tt>"+o+"</tt>"; }
+ public String underline(Object o) { return "<ul>"+o+"</ul>"; }
+ public String p(Object o) { return "<p>"+o+"</p>"; }
+ public String smallcap(Object o) { return "<span style='font-variant: small-caps'>"+o+"</span>"; }
+ public String blockquote(Object o) { return "<blockquote>"+o+"</blockquote>"; }
+ public String superscript(Object o) { return "<sup>"+o+"</sup>"; }
+ public String subscript(Object o) { return "<sub>"+o+"</sub>"; }
+ public String bold(Object o) { return "<b>"+o+"</b>"; }
+ public String strikethrough(Object o) { throw new Error();/*return "<b>"+o+"</b>";*/ }
+ public Object top(Object o) { return "<html><body>"+o+"</body></html>"; }
+ 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 "<a href='"+target+"'>"+text+"</a>";
+ }
+ 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<s.length(); i++) {
+ switch(s.charAt(i)) {
+ case '&': sb.append("&"); break;
+ case '<': sb.append("<"); break;
+ case '>': sb.append(">"); break;
+ case '\'': sb.append("'"); break;
+ case '\"': sb.append("""); break;
+ default: sb.append(s.charAt(i)); break;
+ }
+ }
+ return sb.toString();
+ }
+ private Tree<String> lone(String s) {
+ return new Tree<String>(null, s, new Tree[0]);
+ }
+ public Object walk(Tree<String> t) {
+ String head = t.head();
+ if ("stringify".equals(head)) {
+ StringBuffer ret = new StringBuffer();
+ for(Tree<String> child : t.child(0)) ret.append(child);
+ return ret.toString();
+ }
+ return super.walk(t);
+ }
+ protected Object defaultWalk(String head, Object[] children) {
+ Tree<String>[] kids = new Tree[children.length];
+ for(int i=0; i<children.length; i++) {
+ if (children[i]==null) kids[i]=null;
+ else if (children[i] instanceof String) kids[i] = lone(escapify((String)children[i]));
+ else if (children[i] instanceof Tree) kids[i] = (Tree<String>)children[i];
+ else kids[i] = lone(children[i].toString());
+ }
+ return new Tree<String>(null, head, kids);
+ }
+ }
+