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