Debian Codegraph @

lib/mllpt-lib/ml-lpt/gen/gen-template-struct.sml

raw
1 (* gen-template-struct.sml
2 *
3 * COPYRIGHT (c) 2009 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *
6 * This is a helper program for converting the template files into SML
7 * structures. The usage is:
8 *
9 * gen-template-struct struct-name template-file output-file
10 *
11 * where struct-name is the name of the structure to be generated,
12 * template-file is the source file for the string, and output-file
13 * is the generated file. The generated structure will have the
14 * following signature:
15 *
16 * structure <struct-name> : sig
17 * val template : ExpandFile.template
18 * end
19 *)
20
21 structure Gen =
22 struct
23
24 structure TIO = TextIO
25
26 fun header {fileName, structName} = String.concat [
27 "(* ", fileName, "\n",
28 " *\n\
29 \ * COPYRIGHT (c) 2009 The Fellowship of SML/NJ (http://www.smlnj.org)\n\
30 \ * All rights reserved.\n\
31 \ *\n\
32 \ * !!! WARNING: this file is generated; do not edit !!!\n\
33 \ *)\n\
34 \\n\
35 \structure ", structName, " : sig val template : ExpandFile.template end =\n\
36 \ struct\n\
37 \\n\
38 \ val template = ExpandFile.mkTemplateFromList ["
39 ]
40
41 val tail = "\
42 \\n\
43 \ ]\n\
44 \\n\
45 \ end\n\
46 \"
47
48 fun gen {outputFile, structName, templateFile} = let
49 val src = TIO.openIn templateFile
50 handle ex => (
51 TIO.output(TIO.stdErr, concat[
52 "Error: unable to open template file \"",
53 templateFile, "\"\n"
54 ]);
55 raise ex)
56 val dst = TIO.openOut outputFile
57 handle ex => (
58 TIO.output(TIO.stdErr, concat[
59 "Error: unable to open output file \"",
60 outputFile, "\"\n"
61 ]);
62 raise ex)
63 fun done () = (TIO.closeIn src; TIO.closeOut dst)
64 fun copy first = (case TIO.inputLine src
65 of NONE => ()
66 | SOME line => (
67 if (not first) then TIO.output (dst, ",") else ();
68 TIO.output (dst, "\n \"");
69 TIO.output (dst, String.toString line);
70 TIO.output (dst, "\"");
71 copy false)
72 (* end case *))
73 in
74 ((* try *)
75 TIO.output (dst, header {
76 fileName=outputFile, structName=structName
77 });
78 copy true;
79 TIO.output (dst, tail)
80 handle ex => (
81 TIO.output(TIO.stdErr, "Error copying file\n");
82 done(); raise ex));
83 done()
84 end
85
86 fun main (_, [structName, templateFile, outputFile]) = (
87 (* try *)
88 (gen {
89 outputFile = outputFile,
90 structName = structName,
91 templateFile = templateFile
92 };
93 OS.Process.success)
94 handle ex => OS.Process.failure)
95 | main (cmd, _) = (
96 TIO.output(TIO.stdErr, concat [
97 "usage: ", cmd, " struct-name template-file output-file\n"
98 ]);
99 OS.Process.failure)
100
101 end
102