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 org.ibex.util.*;
7 //import org.ibex.io.*;
8 import edu.berkeley.sbp.*;
9 import edu.berkeley.sbp.misc.*;
10 import edu.berkeley.sbp.util.*;
16 public static void main(String[] s) throws Exception {
17 System.out.println("parsing " + s[0]);
18 Tree<String> res = new CharToken.CharToStringParser(MetaGrammar.make()).parse(new CharToken.Stream(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 CharToken.CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1])));
25 System.out.println(f);
26 //System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString());
27 System.out.println(((Tree)new HTMLWalker().walk(f.expand1())).toPrettyString());
30 public static class StringifyWalker extends ReflectiveWalker {
31 public Object walk(String head, Object[] children) {
32 if ("stringify".equals(head)) {
33 StringBuffer ret = new StringBuffer();
34 for(Tree<String> t : (Tree<String>)children[0]) ret.append(t);
35 return new Tree<String>(null, ret.toString());
37 if (children.length==0) return new Tree<String>(null, head, new Tree[0]);
38 return new Tree<String>(null, head, (Tree<String>[])Reflection.lub(children));
42 public static class HTMLWalker extends ReflectiveWalker {
43 //public void header() { throw new Error(); }
44 public String ul(Object o) { return "<ul>"+o+"</ul>"; }
45 private Tree<String> lone(String s) {
46 StringBuffer sb = new StringBuffer();
47 for(int i=0; i<s.length(); i++) {
49 case '&': sb.append("&"); break;
50 case '<': sb.append("<"); break;
51 case '>': sb.append(">"); break;
52 case '\'': sb.append("'"); break;
53 case '\"': sb.append("""); break;
54 default: sb.append(s.charAt(i)); break;
57 return new Tree<String>(null, sb.toString(), new Tree[0]);
59 public Object walk(Tree<String> t) {
60 String head = t.head();
61 if ("stringify".equals(head)) {
62 StringBuffer ret = new StringBuffer();
63 for(Tree<String> child : t.child(0)) ret.append(child);
64 return lone(ret.toString());
68 public Object walk(String head, Object[] children) {
69 Tree<String>[] kids = new Tree[children.length];
70 for(int i=0; i<children.length; i++) {
71 if (children[i]==null) kids[i]=null;
72 else if (children[i] instanceof String) kids[i] = lone((String)children[i]);
73 else kids[i] = (Tree<String>)children[i];
75 return new Tree<String>(null, head, kids);
80 public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }
82 public static AST h(AST a) { return new Gather(a, Style.H); }
83 public static AST ul(AST a) { return new Gather(a, Style.UL); }
84 public static AST tt(AST a) { return new Gather(a, Style.TT); }
85 public static AST so(AST a) { return new Gather(a, Style.SO); }
86 public static AST it(AST a) { return new Gather(a, Style.IT); }
87 public static AST q(AST a) { return new Gather(a, Style.Q); }
88 public static AST b(AST a) { return new Gather(a, Style.B); }
89 public static AST pre(AST a) { return new Gather(a, Style.PRE); }
90 public static AST list(AST a) { return new Gather(a, Style.LIST); }
91 public static AST emdash() { return new Gather(Style.EMDASH); }
93 public static AST seq(AST a) { return new Gather(a); }
95 public static class Latex {
96 public static void emit(PrintWriter p, AST a) {
101 public static void emit2(PrintWriter p, AST ast, String head) {
102 for(AST a = ast.getFirstChild(); a != null; a = a.getNextSibling()) emit(p, a, head);
104 public static void emit(PrintWriter p, AST ast, String head) {
105 if (!(ast instanceof Gather)) {
106 if (ast.getNumberOfChildren()==0) {
107 p.print(ast.getText());
113 Gather a = (Gather)ast;
119 case H: p.println(); p.println(); p.print("\\"+head+"section{"); emit2(p, a, "sub"+head); p.println("}"); break;
120 case B: p.print("{\\bf{"); emit2(p, a, head); p.print("}}"); break;
121 case UL: p.print("{\\ul{"); emit2(p, a, head); p.print("}}"); break;
122 case IT: p.print("{\\it{"); emit2(p, a, head); p.print("}}"); break;
123 case TT: p.print("{\\tt{"); emit2(p, a, head); p.print("}}"); break;
124 case SO: p.print("{\\overstrike{"); emit2(p, a, head); p.print("}}"); break;
125 case Q: p.print("``"); emit2(p, a, head); p.print("''"); break;
126 case EMDASH: p.print(" \\emdash "); break;
127 case LIST: p.println(); p.println("\\startitemize[symbol]"); emit2(p, a, head); p.println("\\stopitemize"); break;
129 if (a.getFirstChild() != null) {
131 p.println("\\begin{verbatim}");
132 p.println(a.getFirstChild().getText());
133 p.println("\\end{verbatim}");
137 public static void prefix(PrintWriter p) {
138 p.println("% generated by TIBDOC");
139 for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
140 p.println("\\setuppapersize[letter]");
141 p.println("\\setuppagenumbering[location=]");
142 p.println("\\setupcolors[state=start]");
143 //"\\setupinteraction[title={Title},author={Me},"++
144 //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
145 //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
146 p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
147 p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
148 p.println("\\setuplayout[mypage]");
149 p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
150 p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
151 p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
152 p.println("\\usesymbols[uni]");
153 p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
154 p.println("\\setupbodyfont[myface, 11pt]");
155 p.println("\\setupwhitespace[7pt]");
156 p.println("\\def\\MyDroppedCaps%");
157 p.println(" {\\DroppedCaps");
158 p.println(" {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
159 p.println("\\starttext");
160 p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
161 p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
162 p.println("\\blank[1cm,force]");
163 //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
164 //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
165 //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
166 //p.println("\\startmypa");
167 //p.println("\\switchtobodyfont[sma");
170 public static void suffix(PrintWriter p) {
171 p.println("\\stoptext");
173 static String[] packages = new String[] { "supp-fun", "bib", "href" };
179 import Data.Array.IArray
187 toContex ll = prefix ++ (concatMap tl ll) ++ suffix
189 packages = [ "[supp-fun]",
192 prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
193 "\\setuppapersize[letter]\n" ++
194 "\\setuppagenumbering[location=]\n" ++
195 "\\setupcolors[state=start]\n" ++
196 --"\\setupinteraction[title={Title},author={Me},"++
197 --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
198 --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
199 "\\setuphead[section][style={\\ss\\bfa},\n" ++
201 " before=\\blank\\hairline\\nowhitespace,\n" ++
203 "\\definelayout[mypage][\n" ++
204 " backspace=1.75in, % the space for margin notes\n" ++
205 " cutspace=1.75in, % the space for right margin notes\n" ++
208 "\\setuplayout[mypage]\n" ++
209 "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
210 "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
211 "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
212 "\\usesymbols[uni]\n" ++
213 "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
214 "\\setupbodyfont[myface, 11pt]\n" ++
215 "\\setupwhitespace[7pt]\n" ++
216 "\\def\\MyDroppedCaps%\n" ++
217 " {\\DroppedCaps\n" ++
218 " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
220 "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
221 "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
222 "\\blank[1cm,force]\n" ++
223 "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
224 "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
225 "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
227 "\\switchtobodyfont[small]\n"
228 suffix = "\n\\stoptext\n"
229 addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
231 escapify ('%':t) = '\\':'%':(escapify t)
232 escapify ('$':t) = '\\':'$':(escapify t)
233 escapify (h:t) = h:(escapify t)
235 tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
236 tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
237 tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
238 tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
239 tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
240 --tl (Special _ Superscript l) =
241 --tl (Special _ Subscript l) =
242 --tl (Special _ (Image u) l) =
243 --tl (Special _ (Cite u) l) =
244 tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
245 (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
246 tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
247 tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
248 tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
249 tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
250 tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
251 tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
252 tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
253 tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
254 tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
255 tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
256 tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
257 tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
258 --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
259 --tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
260 tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
261 tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
262 tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
263 -- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
264 -- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
265 tl (Special _ Abstract l) =
266 "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
267 "\n\n"++(concatMap tl l)++"\\mypar" ++
268 "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
269 tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
270 tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
272 tl (BlankLine _) = "\n\n"
273 tl (Block _ l) = concatMap tl l
274 tl (Letter _ c) = escapify [c]