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 291 - (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 :     fun w_name (n, r) = let
48 :     val ns = S.nameSpace n
49 :     val prefix =
50 :     case ns of
51 :     S.STRspace => "#"
52 :     | S.SIGspace => "$"
53 :     | S.FCTspace => "%"
54 :     | S.FSIGspace => "&"
55 :     | _ => raise InternalError
56 :     in
57 :     prefix :: S.name n :: "." :: r
58 :     end
59 :    
60 :     fun w_list w (l, r) = foldr w (";" :: r) l
61 :    
62 :     fun w_path (SP.SPATH p, r) = w_list w_name (p, r)
63 :    
64 : blume 286 fun w_decl (SK.Bind (name, def), r) =
65 :     "b" :: w_name (name, w_modExp (def, r))
66 :     | w_decl (SK.Local (x, y), r) = "l" :: w_decl (x, w_decl (y, r))
67 :     | w_decl (SK.Par l, r) = "p" :: w_list w_decl (l, r)
68 :     | w_decl (SK.Seq l, r) = "q" :: w_list w_decl (l, r)
69 :     | w_decl (SK.Open d, r) = "o" :: w_modExp (d, r)
70 :     | w_decl (SK.Ref s, r) = "r" :: w_list w_name (SS.listItems s, r)
71 : blume 275
72 : blume 286 and w_modExp (SK.Var p, r) = "v" :: w_path (p, r)
73 :     | w_modExp (SK.Decl d, r) = "d" :: w_decl (d, r)
74 :     | w_modExp (SK.Let (d, m), r) = "l" :: w_decl (d, w_modExp (m, r))
75 : blume 291 | w_modExp (SK.Ign1 (m1, m2), r) =
76 :     "i" :: w_modExp (m1, w_modExp (m2, r))
77 : blume 275 in
78 :     BinIO.output (s, s2b (concat (version :: w_decl (d, ["\n"]))))
79 :     end
80 :    
81 :     fun read_decl s = let
82 :    
83 :     fun rd () = Option.map b2c (BinIO.input1 s)
84 :    
85 :     local
86 :     fun get (ns, first) = let
87 :     fun loop (accu, NONE) = raise FormatError
88 :     | loop ([], SOME #".") = raise FormatError
89 :     | loop (accu, SOME #".") = ns (String.implode (rev accu))
90 :     | loop (accu, SOME s) = loop (s :: accu, rd ())
91 :     in
92 :     loop ([], first)
93 :     end
94 :     in
95 :     fun r_name (SOME #"#") = get (S.strSymbol, rd ())
96 :     | r_name (SOME #"$") = get (S.sigSymbol, rd ())
97 :     | r_name (SOME #"%") = get (S.fctSymbol, rd ())
98 :     | r_name (SOME #"&") = get (S.fsigSymbol, rd ())
99 :     | r_name _ = raise FormatError
100 :     end
101 :    
102 :     fun r_list r = let
103 :     fun loop (accu, NONE) = raise FormatError
104 :     | loop (accu, SOME #";") = rev accu
105 :     | loop (accu, cur) = loop ((r cur) :: accu, rd ())
106 :     in
107 :     fn first => loop ([], first)
108 :     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 :     | r_modExp (SOME #"d") = SK.Decl (r_decl (rd ()))
122 :     | r_modExp (SOME #"l") = SK.Let (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 :     val s = AbsPath.openBinOut Say.vsay ap
146 :     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