Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-antlr/BackEnds/LaTeX/latex-output.sml

raw
1 (* token.sml
2 *
3 * COPYRIGHT (c) 2006
4 * All rights reserved.
5 *
6 * LaTeX output.
7 * Contributed by Adam Shaw.
8 *)
9
10 (*
11 signature BACK_END =
12 sig
13 val output : (LLKSpec.grammar * TextIO.outstream * string)
14 -> unit
15 end
16 *)
17
18 structure LaTeXOutput (* : BACK_END *) =
19 struct
20
21 structure S = LLKSpec
22
23 (* containsChar : (char list) -> char -> bool *)
24 fun containsChar (cs : char list) (x : char) =
25 (case cs
26 of [] => false
27 | (ch :: chs) => (ch = x) orelse (containsChar chs x)
28 (* end case *))
29
30 (* removeChar : (char list) -> char -> (char list) *)
31 fun removeChar (cs : char list) (x : char) =
32 (case cs
33 of [] => []
34 | (ch :: chs) => if (ch = x) then chs else (removeChar chs x)
35 (* end case *))
36
37 (* escape : char -> string -> string *)
38 fun escape c = let
39 fun backslash [] = []
40 | backslash (x::xs) =
41 if x = c
42 then #"\\" :: x :: (backslash xs)
43 else x :: (backslash xs)
44 in
45 (implode o backslash o explode)
46 end
47
48 (* backslashFirst : (char list) -> (char list) *)
49 (* put backslash first if present, otherwise do nothing *)
50 fun backslashFirst cs =
51 if containsChar cs #"\\"
52 then #"\\" :: (removeChar cs #"\\")
53 else cs
54
55 (* escapeL : (char list) -> string -> string *)
56 (* escape the characters in cs by prepending backslashes *)
57 (* escape backslash first to avoid double-escaping *)
58 fun escapeL cs s = let
59 val cs' = backslashFirst cs
60 fun escl ([], s) = s
61 | escl (ch :: chs, s) = escl (chs, escape ch s)
62 in
63 escl (cs', s)
64 end
65
66 fun curry f = (fn a => fn b => f (a, b))
67 fun flip f = (fn (a, b) => f (b, a))
68 val drop = List.drop
69 fun clipby n = implode o rev o (((curry o flip) drop) n) o rev o explode
70
71 fun wrap d s = concat [d, s, d]
72
73 fun maybewrap (d, t) = fn s => if s = t then wrap d t else s
74
75 fun mathif t = maybewrap ("$", t)
76
77 fun stripquotes s =
78 if String.isPrefix "\"" s
79 then stripquotes (String.extract (s, 1, NONE))
80 else if String.isSuffix "\"" s
81 then stripquotes (clipby 1 s)
82 else s
83
84 (* mathescape : string -> string -> string *)
85 (* to surround all occurences of s with $ . $ *)
86 fun mathescape s s' =
87 if (size s) > (size s')
88 then s'
89 else if String.isPrefix s s'
90 then concat [wrap "$" s,
91 mathescape s (String.extract (s', String.size s, NONE))]
92 else concat [String.substring (s', 0, 1),
93 mathescape s (String.extract (s', 1, NONE))]
94
95 (* mathescapeL : (string list) -> string -> string *)
96 fun mathescapeL ss s' =
97 (case ss
98 of [] => s'
99 | (st :: sts) => mathescapeL sts (mathescape st s')
100 (* end case *))
101
102 (* isID: string -> bool *)
103 (* returns true if a string starts with _ or an alphabetic character *)
104 (* TODO implement this less cheesily *)
105 fun isID s = let
106 fun f [] = false
107 | f (p :: ps) = (String.isPrefix (Char.toString p) s)
108 orelse (f ps)
109 in
110 f (explode "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
111 end
112
113 fun ttif s s' = if s = s' then concat ["{\tt ", s', "}"] else s'
114
115 val cat = String.concat
116 val catw = String.concatWith
117
118 fun prods nt = let
119 val (S.NT {prods, ...}) = nt
120 in
121 !prods
122 end
123
124 val lhs = Prod.lhs
125 val rhs = Prod.items
126
127 val nt2s = Nonterm.toString
128 val i2s = Item.toString
129
130 val spacer = " $\\;$ "
131 val spacedPipe = " $\\;|\\;$ "
132
133 fun tt s = concat ["{\\tt ", s, "}"]
134 fun bf s = concat ["{\\bf ", s, "}"]
135
136 (* some grammar.sty functions *)
137 structure GrammarSty = struct
138 fun tag t = (fn s => concat ["\\", t, "{", s, "}"])
139 val RHS = tag "RHS"
140 val GRP = tag "GRP"
141 val OPT = tag "OPT"
142 val OPTGRP = tag "OPTGRP"
143 val LIST = tag "LIST"
144 val LISTGRP = tag "LISTGRP"
145 val LISTONE = tag "LISTONE"
146 val LISTONEGRP = tag "LISTONEGRP"
147 val ITEM = tag "ITEM"
148 val kw = tag "kw"
149 val nt = tag "nt"
150 val term = tag "term"
151 val sym = tag "sym"
152 fun Grammar s = concat ["\\begin{Grammar}\n", s, "\n\n\\end{Grammar}\n"]
153 fun Rules (nt, rhss) = concat (["\n\\begin{Rules}{", nt, "}\n"] @
154 [catw "\n" rhss] @
155 ["\n\\end{Rules}"])
156 end
157
158 structure G = GrammarSty
159
160 fun prod isSubrule = (if isSubrule then (fn x => x) else G.RHS) o
161 (catw " ") o (map (item isSubrule)) o rhs
162
163 and nonterm isSubrule nt = let
164 val ps = prods nt
165 in
166 if isSubrule
167 then catw spacedPipe (map (prod true) ps)
168 else let val nt' = nt2s nt
169 in G.Rules (nt', map (prod isSubrule) ps)
170 end
171 end
172
173 and tok t = let
174 val abbrev = Token.toString t
175 val scrub = (escapeL (explode "_{}")) o
176 (mathescapeL ["<", ">", "-", "|"]) o
177 (stripquotes)
178 in
179 if isID abbrev
180 then (G.kw o (* G.ITEM o *) scrub) abbrev
181 else (G.sym o (* G.ITEM o *) scrub) abbrev
182 end
183
184 and item isSubrule i =
185 (case Item.sym i
186 of S.TOK t => tok t
187 | S.NONTERM (nt, _) => if Nonterm.isSubrule nt
188 then (G.nt o G.GRP o (nonterm true)) nt
189 else (G.nt o i2s) i
190 | S.CLOS nt => (G.LISTGRP o (nonterm true)) nt
191 | S.POSCLOS nt => (G.LISTONEGRP o (nonterm true)) nt
192 | S.OPT nt => (G.OPTGRP o (nonterm true)) nt
193 (* end case *))
194
195 fun nonterms [] = "the null grammar"
196 | nonterms ns = G.Grammar (catw "\n" (map (nonterm false) ns))
197
198 (* output grammar *)
199 fun grammarHook spec strm = let
200 val (S.Grammar {sortedTops, ...}, _) = spec
201 val nts = List.concat sortedTops
202 val g = nonterms nts
203 in
204 TextIO.output (strm, g)
205 end
206
207 fun output (grm, pm, fname) = (print (" writing " ^ fname ^ ".tex\n");
208 ExpandFile.expandTemplate {
209 src = LaTeXTemplate.template,
210 dst = fname ^ ".tex",
211 hooks = [("grammar", grammarHook (grm, pm))]
212 })
213
214 end