1 // Copyright 2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
5 package edu.berkeley.sbp.tib;
6 import edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.misc.*;
8 import edu.berkeley.sbp.util.*;
9 import edu.berkeley.sbp.chr.*;
15 public static void main(String[] s) throws Exception {
17 System.out.println("parsing " + s[0]);
18 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
19 MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res);
20 //System.out.println(gram);
21 Union mg = gram.done();
23 System.out.println("\nparsing " + s[1]);
24 Forest f = new CharParser(mg).parse(new Tib(new FileInputStream(s[1])));
27 System.out.println(f);
29 System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString());
31 String st = new HTMLWalker().walk(f.expand1()).toString();
32 System.out.println(st);
33 FileOutputStream fos = new FileOutputStream("out.html");
34 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
38 } catch (Ambiguous a) {
39 FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
40 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
41 GraphViz gv = new GraphViz();
42 a.ambiguity.toGraphViz(gv);
47 } catch (Exception e) {
52 public static class StringifyWalker extends ReflectiveWalker {
53 public Object walk(String head, Object[] children) {
54 if ("stringify".equals(head)) {
55 StringBuffer ret = new StringBuffer();
56 for(Tree<String> t : (Tree<String>)children[0]) ret.append(t);
57 return new Tree<String>(null, ret.toString());
59 if (children.length==0) return new Tree<String>(null, head, new Tree[0]);
60 return new Tree<String>(null, head, (Tree<String>[])Reflection.lub(children));
64 public static String join(String[] sa, String sep) {
65 StringBuffer ret = new StringBuffer();
68 if (!first) ret.append(sep);
72 return ret.toString();
75 public static class HTMLWalker extends ReflectiveWalker {
76 //public void header() { throw new Error(); }
77 public String li(Object o) { return "<li>"+o+"</li>"; }
78 public String li(Object a, Object o) { return "<li>"+o+"</li>"; }
79 public String ul(String[] li) { return "<ul>"+join(li,"")+"</ul>"; }
80 public String ol(String[] li) { return "<ol>"+join(li,"")+"</ol>"; }
81 public String hr() { return "\n<hr/>\n"; }
82 public String it(Object o) { return "<i>"+o+"</i>"; }
83 public String tt(Object o) { return "<tt>"+o+"</tt>"; }
84 public String underline(Object o) { return "<ul>"+o+"</ul>"; }
85 public String p(Object o) { return "<p>"+o+"</p>"; }
86 public String smallcap(Object o) { return "<span style='font-variant: small-caps'>"+o+"</span>"; }
87 public String blockquote(Object o) { return "<blockquote>"+o+"</blockquote>"; }
88 public String superscript(Object o) { return "<sup>"+o+"</sup>"; }
89 public String subscript(Object o) { return "<sub>"+o+"</sub>"; }
90 public String bold(Object o) { return "<b>"+o+"</b>"; }
91 public String strikethrough(Object o) { throw new Error();/*return "<b>"+o+"</b>";*/ }
92 public Object top(Object o) { return "<html><body>"+o+"</body></html>"; }
93 public Object doc(Object header, Object body) { return body; }
94 public String text(Object[] body) {
95 StringBuffer ret = new StringBuffer();
96 for(Object o : body) { ret.append(o); ret.append(" "); }
97 return ret.toString();
99 public String body(String[] sections) { return join(sections, "\n\n"); }
100 public String domain(String[] parts) { return join(parts, "."); }
101 public String ip(String[] parts) { return join(parts, "."); }
102 public String emailaddr(String user, String host) {
103 return link(user+"@"+host, "mailto:"+user+"@"+host);
105 //public String url(String method) {
106 public String link(Object text, Object target) {
107 return "<a href='"+target+"'>"+text+"</a>";
109 public String section(Object header, Object[] body) {
110 StringBuffer ret = new StringBuffer();
113 for(Object o : body) ret.append(o);
114 return ret.toString();
116 private String escapify(Object o) {
117 String s = o==null ? "" : o.toString();
118 StringBuffer sb = new StringBuffer();
119 for(int i=0; i<s.length(); i++) {
120 switch(s.charAt(i)) {
121 case '&': sb.append("&"); break;
122 case '<': sb.append("<"); break;
123 case '>': sb.append(">"); break;
124 case '\'': sb.append("'"); break;
125 case '\"': sb.append("""); break;
126 default: sb.append(s.charAt(i)); break;
129 return sb.toString();
131 private Tree<String> lone(String s) {
132 return new Tree<String>(null, s, new Tree[0]);
134 public Object walk(Tree<String> t) {
135 String head = t.head();
136 if ("stringify".equals(head)) {
137 StringBuffer ret = new StringBuffer();
138 for(Tree<String> child : t.child(0)) ret.append(child);
139 return ret.toString();
141 return super.walk(t);
143 protected Object defaultWalk(String head, Object[] children) {
144 Tree<String>[] kids = new Tree[children.length];
145 for(int i=0; i<children.length; i++) {
146 if (children[i]==null) kids[i]=null;
147 else if (children[i] instanceof String) kids[i] = lone(escapify((String)children[i]));
148 else if (children[i] instanceof Tree) kids[i] = (Tree<String>)children[i];
149 else kids[i] = lone(children[i].toString());
151 return new Tree<String>(null, head, kids);
156 public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }
158 public static AST h(AST a) { return new Gather(a, Style.H); }
159 public static AST ul(AST a) { return new Gather(a, Style.UL); }
160 public static AST tt(AST a) { return new Gather(a, Style.TT); }
161 public static AST so(AST a) { return new Gather(a, Style.SO); }
162 public static AST it(AST a) { return new Gather(a, Style.IT); }
163 public static AST q(AST a) { return new Gather(a, Style.Q); }
164 public static AST b(AST a) { return new Gather(a, Style.B); }
165 public static AST pre(AST a) { return new Gather(a, Style.PRE); }
166 public static AST list(AST a) { return new Gather(a, Style.LIST); }
167 public static AST emdash() { return new Gather(Style.EMDASH); }
169 public static AST seq(AST a) { return new Gather(a); }
171 public static class Latex {
172 public static void emit(PrintWriter p, AST a) {
177 public static void emit2(PrintWriter p, AST ast, String head) {
178 for(AST a = ast.getFirstChild(); a != null; a = a.getNextSibling()) emit(p, a, head);
180 public static void emit(PrintWriter p, AST ast, String head) {
181 if (!(ast instanceof Gather)) {
182 if (ast.getNumberOfChildren()==0) {
183 p.print(ast.getText());
189 Gather a = (Gather)ast;
195 case H: p.println(); p.println(); p.print("\\"+head+"section{"); emit2(p, a, "sub"+head); p.println("}"); break;
196 case B: p.print("{\\bf{"); emit2(p, a, head); p.print("}}"); break;
197 case UL: p.print("{\\ul{"); emit2(p, a, head); p.print("}}"); break;
198 case IT: p.print("{\\it{"); emit2(p, a, head); p.print("}}"); break;
199 case TT: p.print("{\\tt{"); emit2(p, a, head); p.print("}}"); break;
200 case SO: p.print("{\\overstrike{"); emit2(p, a, head); p.print("}}"); break;
201 case Q: p.print("``"); emit2(p, a, head); p.print("''"); break;
202 case EMDASH: p.print(" \\emdash "); break;
203 case LIST: p.println(); p.println("\\startitemize[symbol]"); emit2(p, a, head); p.println("\\stopitemize"); break;
205 if (a.getFirstChild() != null) {
207 p.println("\\begin{verbatim}");
208 p.println(a.getFirstChild().getText());
209 p.println("\\end{verbatim}");
213 public static void prefix(PrintWriter p) {
214 p.println("% generated by TIBDOC");
215 for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
216 p.println("\\setuppapersize[letter]");
217 p.println("\\setuppagenumbering[location=]");
218 p.println("\\setupcolors[state=start]");
219 //"\\setupinteraction[title={Title},author={Me},"++
220 //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
221 //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
222 p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
223 p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
224 p.println("\\setuplayout[mypage]");
225 p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
226 p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
227 p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
228 p.println("\\usesymbols[uni]");
229 p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
230 p.println("\\setupbodyfont[myface, 11pt]");
231 p.println("\\setupwhitespace[7pt]");
232 p.println("\\def\\MyDroppedCaps%");
233 p.println(" {\\DroppedCaps");
234 p.println(" {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
235 p.println("\\starttext");
236 p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
237 p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
238 p.println("\\blank[1cm,force]");
239 //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
240 //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
241 //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
242 //p.println("\\startmypa");
243 //p.println("\\switchtobodyfont[sma");
246 public static void suffix(PrintWriter p) {
247 p.println("\\stoptext");
249 static String[] packages = new String[] { "supp-fun", "bib", "href" };
255 import Data.Array.IArray
263 toContex ll = prefix ++ (concatMap tl ll) ++ suffix
265 packages = [ "[supp-fun]",
268 prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
269 "\\setuppapersize[letter]\n" ++
270 "\\setuppagenumbering[location=]\n" ++
271 "\\setupcolors[state=start]\n" ++
272 --"\\setupinteraction[title={Title},author={Me},"++
273 --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
274 --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
275 "\\setuphead[section][style={\\ss\\bfa},\n" ++
277 " before=\\blank\\hairline\\nowhitespace,\n" ++
279 "\\definelayout[mypage][\n" ++
280 " backspace=1.75in, % the space for margin notes\n" ++
281 " cutspace=1.75in, % the space for right margin notes\n" ++
284 "\\setuplayout[mypage]\n" ++
285 "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
286 "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
287 "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
288 "\\usesymbols[uni]\n" ++
289 "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
290 "\\setupbodyfont[myface, 11pt]\n" ++
291 "\\setupwhitespace[7pt]\n" ++
292 "\\def\\MyDroppedCaps%\n" ++
293 " {\\DroppedCaps\n" ++
294 " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
296 "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
297 "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
298 "\\blank[1cm,force]\n" ++
299 "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
300 "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
301 "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
303 "\\switchtobodyfont[small]\n"
304 suffix = "\n\\stoptext\n"
305 addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
307 escapify ('%':t) = '\\':'%':(escapify t)
308 escapify ('$':t) = '\\':'$':(escapify t)
309 escapify (h:t) = h:(escapify t)
311 tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
312 tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
313 tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
314 tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
315 tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
316 --tl (Special _ Superscript l) =
317 --tl (Special _ Subscript l) =
318 --tl (Special _ (Image u) l) =
319 --tl (Special _ (Cite u) l) =
320 tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
321 (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
322 tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
323 tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
324 tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
325 tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
326 tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
327 tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
328 tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
329 tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
330 tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
331 tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
332 tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
333 tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
334 --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
335 --tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
336 tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
337 tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
338 tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
339 -- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
340 -- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
341 tl (Special _ Abstract l) =
342 "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
343 "\n\n"++(concatMap tl l)++"\\mypar" ++
344 "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
345 tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
346 tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
348 tl (BlankLine _) = "\n\n"
349 tl (Block _ l) = concatMap tl l
350 tl (Letter _ c) = escapify [c]