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

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