Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cm/smlfile/skel-io.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/smlfile/skel-io.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 297 - (view) (download)

1 : blume 275 (*
2 :     * Reading and writing skeletons to skeleton files.
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 :     signature SKELIO = sig
9 :     exception InternalError
10 :     val read : AbsPath.t * TStamp.t -> Skeleton.decl option
11 :     val write : AbsPath.t * Skeleton.decl -> unit
12 :     end
13 :    
14 :     structure SkelIO :> SKELIO = struct
15 :    
16 :     structure SK = Skeleton
17 :     structure SS = SymbolSet
18 : blume 278 structure S = Symbol
19 : blume 275 structure SP = GenericVC.SymPath
20 :    
21 :     exception InternalError
22 :     exception FormatError
23 :    
24 :     val s2b = Byte.stringToBytes
25 :     val b2c = Byte.byteToChar
26 :    
27 : blume 286 val version = "Skeleton 1\n"
28 : blume 275
29 :     fun makeset l = SS.addList (SS.empty, l)
30 :    
31 :     fun inputLine s = let
32 :     fun finish acc = String.implode (rev acc)
33 :     fun loop acc =
34 :     case Option.map b2c (BinIO.input1 s) of
35 :     NONE => finish (#"\n" :: acc)
36 :     | SOME #"\n" => finish (#"\n" :: acc)
37 :     | SOME c => loop (c :: acc)
38 :     in
39 :     loop []
40 :     end
41 :    
42 :     fun write_decl (s, d) = let
43 :    
44 :     (* We are consing up the whole output as a list of strings
45 :     * before concatenating it to form the final result and
46 :     * wrinting it out using one single `output' call. *)
47 : blume 294 fun w_name (n, r) =
48 :     (case S.nameSpace n of
49 :     S.SIGspace => "'" (* only tyvars could start like that *)
50 :     | S.FCTspace => "(" (* no sym can start like that *)
51 :     | S.FSIGspace => ")" (* no sym can start like that *)
52 :     | S.STRspace => "" (* this should be safe now *)
53 :     | _ => raise InternalError)
54 :     :: S.name n :: "." :: r
55 : blume 275
56 : blume 294 (* foldl means that last element appears first in output! *)
57 :     fun w_list w (l, r) = foldl w (";" :: r) l
58 : blume 275
59 :     fun w_path (SP.SPATH p, r) = w_list w_name (p, r)
60 :    
61 : blume 286 fun w_decl (SK.Bind (name, def), r) =
62 :     "b" :: w_name (name, w_modExp (def, r))
63 :     | w_decl (SK.Local (x, y), r) = "l" :: w_decl (x, w_decl (y, r))
64 :     | w_decl (SK.Par l, r) = "p" :: w_list w_decl (l, r)
65 :     | w_decl (SK.Seq l, r) = "q" :: w_list w_decl (l, r)
66 :     | w_decl (SK.Open d, r) = "o" :: w_modExp (d, r)
67 :     | w_decl (SK.Ref s, r) = "r" :: w_list w_name (SS.listItems s, r)
68 : blume 275
69 : blume 286 and w_modExp (SK.Var p, r) = "v" :: w_path (p, r)
70 : blume 293 | w_modExp (SK.Decl d, r) = "d" :: w_list w_decl (d, r)
71 :     | w_modExp (SK.Let (d, m), r) =
72 :     "l" :: w_list w_decl (d, w_modExp (m, r))
73 : blume 291 | w_modExp (SK.Ign1 (m1, m2), r) =
74 :     "i" :: w_modExp (m1, w_modExp (m2, r))
75 : blume 275 in
76 :     BinIO.output (s, s2b (concat (version :: w_decl (d, ["\n"]))))
77 :     end
78 :    
79 :     fun read_decl s = let
80 :    
81 :     fun rd () = Option.map b2c (BinIO.input1 s)
82 :    
83 :     local
84 :     fun get (ns, first) = let
85 :     fun loop (accu, NONE) = raise FormatError
86 :     | loop ([], SOME #".") = raise FormatError
87 :     | loop (accu, SOME #".") = ns (String.implode (rev accu))
88 :     | loop (accu, SOME s) = loop (s :: accu, rd ())
89 :     in
90 :     loop ([], first)
91 :     end
92 :     in
93 : blume 294 fun r_name (SOME #"'") = get (S.sigSymbol, rd ())
94 : blume 293 | r_name (SOME #"(") = get (S.fctSymbol, rd ())
95 :     | r_name (SOME #")") = get (S.fsigSymbol, rd ())
96 :     | r_name first = get (S.strSymbol, first)
97 : blume 275 end
98 :    
99 : blume 294 (* lists are written in reverse order, so a tail-recursive
100 :     * reader is exactly right because it undoes the reversal *)
101 :     fun r_list r first = let
102 :     (* argument order important: side effects in arguments! *)
103 :     fun rl (l, SOME #";") = l
104 :     | rl (l, first) = rl (r first :: l, rd ())
105 : blume 275 in
106 : blume 294 rl ([], first)
107 : blume 275 end
108 :    
109 :     fun r_path first = SP.SPATH (r_list r_name first)
110 :    
111 : blume 286 fun r_decl (SOME #"b") = SK.Bind (r_name (rd ()), r_modExp (rd ()))
112 :     | r_decl (SOME #"l") = SK.Local (r_decl (rd ()), r_decl (rd ()))
113 :     | r_decl (SOME #"p") = SK.Par (r_list r_decl (rd ()))
114 :     | r_decl (SOME #"q") = SK.Seq (r_list r_decl (rd ()))
115 :     | r_decl (SOME #"o") = SK.Open (r_modExp (rd ()))
116 :     | r_decl (SOME #"r") = SK.Ref (makeset (r_list r_name (rd ())))
117 : blume 275 | r_decl _ = raise FormatError
118 :    
119 : blume 286 and r_modExp (SOME #"v") = SK.Var (r_path (rd ()))
120 : blume 293 | r_modExp (SOME #"d") = SK.Decl (r_list r_decl (rd ()))
121 :     | r_modExp (SOME #"l") =
122 :     SK.Let (r_list r_decl (rd ()), r_modExp (rd ()))
123 : blume 291 | r_modExp (SOME #"i") = SK.Ign1 (r_modExp (rd ()), r_modExp (rd ()))
124 : blume 286 | r_modExp _ = raise FormatError
125 : blume 275
126 :     val firstline = inputLine s
127 :     val r = if firstline = version then r_decl (rd ())
128 :     else raise FormatError
129 :     val nl = rd ()
130 :     in
131 :     if nl = SOME #"\n" then r else raise FormatError
132 :     end
133 :    
134 :     fun read (ap, ts) =
135 :     if TStamp.earlier (AbsPath.tstamp ap, ts) then NONE
136 :     else let
137 :     val s = AbsPath.openBinIn ap
138 :     val r = read_decl s
139 :     handle exn => (BinIO.closeIn s; raise exn)
140 :     in
141 :     BinIO.closeIn s; SOME r
142 :     end handle _ => NONE
143 :    
144 :     fun write (ap, sk) = let
145 : blume 297 val s = AbsPath.openBinOut ap
146 : blume 275 in
147 :     (Interrupt.guarded (fn () => write_decl (s, sk));
148 :     BinIO.closeOut s)
149 :     handle exn => let
150 :     val p = AbsPath.name ap
151 :     in
152 :     BinIO.closeOut s;
153 :     OS.FileSys.remove p handle _ => ();
154 :     Say.say (concat ["[writing ", p, " failed]\n"]);
155 :     raise exn
156 :     end
157 :     end handle Interrupt.Interrupt => raise Interrupt.Interrupt
158 :     | InternalError => raise InternalError
159 :     | _ => ()
160 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0