adam@1592
|
1 (* Copyright (c) 2011, Adam Chlipala
|
adam@1592
|
2 * All rights reserved.
|
adam@1592
|
3 *
|
adam@1592
|
4 * Redistribution and use in source and binary forms, with or without
|
adam@1592
|
5 * modification, are permitted provided that the following conditions are met:
|
adam@1592
|
6 *
|
adam@1592
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adam@1592
|
8 * this list of conditions and the following disclaimer.
|
adam@1592
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adam@1592
|
10 * this list of conditions and the following disclaimer in the documentation
|
adam@1592
|
11 * and/or other materials provided with the distribution.
|
adam@1592
|
12 * - The names of contributors may not be used to endorse or promote products
|
adam@1592
|
13 * derived from this software without specific prior written permission.
|
adam@1592
|
14 *
|
adam@1592
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adam@1592
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adam@1592
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adam@1592
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adam@1592
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adam@1592
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adam@1592
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adam@1592
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adam@1592
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adam@1592
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adam@1592
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adam@1592
|
26 *)
|
adam@1592
|
27
|
adam@1592
|
28 (* Building SML code from XML entity tables *)
|
adam@1592
|
29
|
adam@1592
|
30 fun main () =
|
adam@1592
|
31 let
|
adam@1592
|
32 fun doFile fname =
|
adam@1592
|
33 let
|
adam@1592
|
34 val inf = TextIO.openIn fname
|
adam@1592
|
35
|
adam@1592
|
36 fun loop () =
|
adam@1592
|
37 case TextIO.inputLine inf of
|
adam@1592
|
38 NONE => TextIO.closeIn inf
|
adam@1592
|
39 | SOME line =>
|
adam@1592
|
40 if String.isPrefix "<!ENTITY " line then
|
adam@1592
|
41 case String.tokens (fn ch => Char.isSpace ch orelse ch = #">") line of
|
adam@1592
|
42 "<!ENTITY" :: ent :: exp :: _ =>
|
adam@1592
|
43 let
|
adam@1592
|
44 val exp = if String.isPrefix "\"&#" exp andalso String.isSuffix ";\"" exp then
|
adam@1592
|
45 let
|
adam@1592
|
46 val middle = String.substring (exp, 3, size exp - 5)
|
adam@1592
|
47 in
|
adam@1592
|
48 if CharVector.all Char.isDigit middle then
|
adam@1592
|
49 middle
|
adam@1592
|
50 else if String.isPrefix "38;#" middle then
|
adam@1592
|
51 String.extract (middle, 4, NONE)
|
adam@1592
|
52 else
|
adam@1592
|
53 raise Fail "Bad entity expression [1]"
|
adam@1592
|
54 end
|
adam@1592
|
55 else
|
adam@1592
|
56 raise Fail "Bad entity expansion [2]"
|
adam@1592
|
57 in
|
adam@1592
|
58 print ("\t\t(\"" ^ ent ^ "\", " ^ exp ^ "),\n");
|
adam@1592
|
59 loop ()
|
adam@1592
|
60 end
|
adam@1592
|
61 | _ => raise Fail "Bad ENTITY line"
|
adam@1592
|
62 else
|
adam@1592
|
63 loop ()
|
adam@1592
|
64 in
|
adam@1592
|
65 loop ()
|
adam@1592
|
66 end
|
adam@1592
|
67 in
|
adam@1592
|
68 print "structure Entities = struct\n";
|
adam@1592
|
69 print "\tval all = [\n";
|
adam@1592
|
70 doFile "xml/xhtml-lat1.ent";
|
adam@1592
|
71 doFile "xml/xhtml-special.ent";
|
adam@1592
|
72 doFile "xml/xhtml-symbol.ent";
|
adam@1592
|
73 print "\t(\"\", 0)]\n";
|
adam@1592
|
74 print "end\n"
|
adam@1592
|
75 end
|
adam@1592
|
76
|
adam@1592
|
77 val () = main ()
|