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.*;
15 public static void main(String[] s) throws Exception {
16 System.out.println("parsing " + s[0]);
17 Tree<String> res = new CharToken.CharToStringParser(MetaGrammar.make()).parse1(new CharToken.Stream(new FileInputStream(s[0])));
18 MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res);
19 //System.out.println(gram);
20 Union mg = gram.done();
22 System.out.println("\nparsing " + s[1]);
23 Forest f = new CharToken.CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1])));
24 System.out.println(f);
25 System.out.println(((Tree)walk(f.expand1())).toString(0, 0, 120));
28 public static Tree<String> walk(Tree<String> tree) {
29 String head = tree.head();
30 if ("stringify".equals(head)) {
32 for(Tree<String> t : tree.child(0)) ret += t;
33 return new Tree<String>(null, ret);
35 Tree<String>[] children = new Tree[tree.numChildren()];
36 for(int i=0; i<children.length; i++) children[i] = walk(tree.child(i));
37 return new Tree<String>(null, head, children);
42 public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }
44 public static AST h(AST a) { return new Gather(a, Style.H); }
45 public static AST ul(AST a) { return new Gather(a, Style.UL); }
46 public static AST tt(AST a) { return new Gather(a, Style.TT); }
47 public static AST so(AST a) { return new Gather(a, Style.SO); }
48 public static AST it(AST a) { return new Gather(a, Style.IT); }
49 public static AST q(AST a) { return new Gather(a, Style.Q); }
50 public static AST b(AST a) { return new Gather(a, Style.B); }
51 public static AST pre(AST a) { return new Gather(a, Style.PRE); }
52 public static AST list(AST a) { return new Gather(a, Style.LIST); }
53 public static AST emdash() { return new Gather(Style.EMDASH); }
55 public static AST seq(AST a) { return new Gather(a); }
57 public static class Latex {
58 public static void emit(PrintWriter p, AST a) {
63 public static void emit2(PrintWriter p, AST ast, String head) {
64 for(AST a = ast.getFirstChild(); a != null; a = a.getNextSibling()) emit(p, a, head);
66 public static void emit(PrintWriter p, AST ast, String head) {
67 if (!(ast instanceof Gather)) {
68 if (ast.getNumberOfChildren()==0) {
69 p.print(ast.getText());
75 Gather a = (Gather)ast;
81 case H: p.println(); p.println(); p.print("\\"+head+"section{"); emit2(p, a, "sub"+head); p.println("}"); break;
82 case B: p.print("{\\bf{"); emit2(p, a, head); p.print("}}"); break;
83 case UL: p.print("{\\ul{"); emit2(p, a, head); p.print("}}"); break;
84 case IT: p.print("{\\it{"); emit2(p, a, head); p.print("}}"); break;
85 case TT: p.print("{\\tt{"); emit2(p, a, head); p.print("}}"); break;
86 case SO: p.print("{\\overstrike{"); emit2(p, a, head); p.print("}}"); break;
87 case Q: p.print("``"); emit2(p, a, head); p.print("''"); break;
88 case EMDASH: p.print(" \\emdash "); break;
89 case LIST: p.println(); p.println("\\startitemize[symbol]"); emit2(p, a, head); p.println("\\stopitemize"); break;
91 if (a.getFirstChild() != null) {
93 p.println("\\begin{verbatim}");
94 p.println(a.getFirstChild().getText());
95 p.println("\\end{verbatim}");
99 public static void prefix(PrintWriter p) {
100 p.println("% generated by TIBDOC");
101 for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
102 p.println("\\setuppapersize[letter]");
103 p.println("\\setuppagenumbering[location=]");
104 p.println("\\setupcolors[state=start]");
105 //"\\setupinteraction[title={Title},author={Me},"++
106 //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
107 //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
108 p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
109 p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
110 p.println("\\setuplayout[mypage]");
111 p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
112 p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
113 p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
114 p.println("\\usesymbols[uni]");
115 p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
116 p.println("\\setupbodyfont[myface, 11pt]");
117 p.println("\\setupwhitespace[7pt]");
118 p.println("\\def\\MyDroppedCaps%");
119 p.println(" {\\DroppedCaps");
120 p.println(" {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
121 p.println("\\starttext");
122 p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
123 p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
124 p.println("\\blank[1cm,force]");
125 //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
126 //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
127 //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
128 //p.println("\\startmypa");
129 //p.println("\\switchtobodyfont[sma");
132 public static void suffix(PrintWriter p) {
133 p.println("\\stoptext");
135 static String[] packages = new String[] { "supp-fun", "bib", "href" };
141 import Data.Array.IArray
149 toContex ll = prefix ++ (concatMap tl ll) ++ suffix
151 packages = [ "[supp-fun]",
154 prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
155 "\\setuppapersize[letter]\n" ++
156 "\\setuppagenumbering[location=]\n" ++
157 "\\setupcolors[state=start]\n" ++
158 --"\\setupinteraction[title={Title},author={Me},"++
159 --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
160 --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
161 "\\setuphead[section][style={\\ss\\bfa},\n" ++
163 " before=\\blank\\hairline\\nowhitespace,\n" ++
165 "\\definelayout[mypage][\n" ++
166 " backspace=1.75in, % the space for margin notes\n" ++
167 " cutspace=1.75in, % the space for right margin notes\n" ++
170 "\\setuplayout[mypage]\n" ++
171 "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
172 "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
173 "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
174 "\\usesymbols[uni]\n" ++
175 "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
176 "\\setupbodyfont[myface, 11pt]\n" ++
177 "\\setupwhitespace[7pt]\n" ++
178 "\\def\\MyDroppedCaps%\n" ++
179 " {\\DroppedCaps\n" ++
180 " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
182 "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
183 "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
184 "\\blank[1cm,force]\n" ++
185 "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
186 "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
187 "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
189 "\\switchtobodyfont[small]\n"
190 suffix = "\n\\stoptext\n"
191 addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
193 escapify ('%':t) = '\\':'%':(escapify t)
194 escapify ('$':t) = '\\':'$':(escapify t)
195 escapify (h:t) = h:(escapify t)
197 tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
198 tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
199 tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
200 tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
201 tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
202 --tl (Special _ Superscript l) =
203 --tl (Special _ Subscript l) =
204 --tl (Special _ (Image u) l) =
205 --tl (Special _ (Cite u) l) =
206 tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
207 (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
208 tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
209 tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
210 tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
211 tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
212 tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
213 tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
214 tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
215 tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
216 tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
217 tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
218 tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
219 tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
220 --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
221 --tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
222 tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
223 tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
224 tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
225 -- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
226 -- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
227 tl (Special _ Abstract l) =
228 "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
229 "\n\n"++(concatMap tl l)++"\\mypar" ++
230 "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
231 tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
232 tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
234 tl (BlankLine _) = "\n\n"
235 tl (Block _ l) = concatMap tl l
236 tl (Letter _ c) = escapify [c]