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/tools/main/private-tools.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/tools/main/private-tools.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 873 - (view) (download)

1 : blume 742 (*
2 :     * This is the implementation of the private interface to CM's tools
3 :     * mechanism. It lacks certain public features implemented by ToolsFn
4 :     * but provides other, non-public routines such as "expand".
5 :     *
6 :     * (C) 2000 Lucent Technologies, Bell Laboratories
7 :     *
8 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
9 :     *)
10 :     structure PrivateTools : PRIVATETOOLS = struct
11 :    
12 :     type class = string
13 :    
14 :     type srcpath = SrcPath.file
15 :     type presrcpath = SrcPath.prefile
16 :     type rebindings = SrcPath.rebindings
17 :    
18 :     val nativeSpec = SrcPath.osstring_relative
19 : blume 756 val nativePreSpec = SrcPath.osstring_prefile_relative
20 : blume 742
21 :     val srcpath = SrcPath.file
22 :    
23 : blume 756 val augment = SrcPath.extend
24 :    
25 : blume 742 exception ToolError of { tool: string, msg: string }
26 :    
27 : blume 756 type pathmaker = unit -> presrcpath
28 : blume 742
29 : blume 756 type fnspec = { name: string, mkpath: pathmaker }
30 :    
31 : blume 742 datatype toolopt =
32 : blume 756 STRING of fnspec
33 : blume 742 | SUBOPTS of { name: string, opts: toolopts }
34 :     withtype toolopts = toolopt list
35 :    
36 :     type tooloptcvt = toolopts option -> toolopts option
37 :    
38 :     type spec = { name: string,
39 :     mkpath: pathmaker,
40 :     class: class option,
41 :     opts: toolopts option,
42 :     derived: bool }
43 :    
44 :     type setup = string option * string option
45 :    
46 : blume 818 type splitting = int option option
47 :    
48 : blume 742 type expansion =
49 : blume 818 { smlfiles: (srcpath * Sharing.request * setup * splitting) list,
50 : blume 742 cmfiles: (srcpath * Version.t option * rebindings) list,
51 :     sources: (srcpath * { class: string, derived: bool}) list }
52 :    
53 :     type partial_expansion = expansion * spec list
54 :    
55 :     type rulefn = unit -> partial_expansion
56 :     type rulecontext = rulefn -> partial_expansion
57 : blume 756 type rule = { spec: spec,
58 :     native2pathmaker: string -> pathmaker,
59 :     context: rulecontext,
60 : blume 873 defaultClassOf: fnspec -> class option,
61 :     sysinfo: { symval: string -> int option,
62 :     archos: string } } ->
63 : blume 756 partial_expansion
64 : blume 742
65 : blume 756 type gcarg = { name: string, mkfname: unit -> string }
66 :    
67 : blume 742 type registry = { classes : rule StringMap.map ref,
68 :     sfx_classifiers : (string -> class option) ref,
69 : blume 756 gen_classifiers : (gcarg -> class option) ref }
70 : blume 742
71 :     fun layer (look1, look2) s = case look1 s of NONE => look2 s | x => x
72 :    
73 :     fun newRegistry () = { classes = ref StringMap.empty,
74 :     sfx_classifiers = ref (fn _ => NONE),
75 :     gen_classifiers = ref (fn _ => NONE) } : registry
76 :    
77 :     (* Three registries:
78 :     * 1. global: where globally available tools are registered and found.
79 :     * 2. local: where locally available tools are found;
80 :     * the local registry is being set anew every time "expand"
81 :     * is being called; each instance of a local registry belongs
82 :     * to one description file that is being processed.
83 :     * 3. plugin registries: mapping from tool implementations (indexed
84 :     * by their respective description files) to that tool's
85 :     * registry; here is where local tools register themselves;
86 :     * the rule for the "tool" class causes the tool to register
87 :     * itself if it has not already done so and then merges
88 :     * the contents of the tool's registry into the current
89 :     * local registry.
90 :     * These complications exist because tools register themselves via
91 :     * side-effects. *)
92 :    
93 :     val global_registry = newRegistry ()
94 :    
95 :     val local_registry : registry ref = ref (newRegistry ())
96 :    
97 :     val plugin_registries : registry SrcPathMap.map ref = ref SrcPathMap.empty
98 :    
99 :     val current_plugin : SrcPath.file option ref = ref NONE
100 :    
101 :     local
102 :     fun registry sel cvt s = let
103 :     val get = cvt o ! o sel
104 :     in
105 :     layer (get (!local_registry), get global_registry) s
106 :     end
107 :     fun curry f x y = f (x, y)
108 :     in
109 :     val classes = registry #classes (curry StringMap.find)
110 :     val sfx_classifiers = registry #sfx_classifiers (fn x => x)
111 :     val gen_classifiers = registry #gen_classifiers (fn x => x)
112 :     end
113 :    
114 :     datatype classifier =
115 :     SFX_CLASSIFIER of string -> class option
116 : blume 756 | GEN_CLASSIFIER of gcarg -> class option
117 : blume 742
118 :     fun stdSfxClassifier { sfx, class } =
119 :     SFX_CLASSIFIER (fn e => if sfx = e then SOME class else NONE)
120 :    
121 :     local
122 :     fun upd sel augment = let
123 :     val rf =
124 :     sel (case !current_plugin of
125 :     NONE => global_registry
126 :     | SOME p =>
127 :     (case SrcPathMap.find (!plugin_registries, p) of
128 :     SOME r => r
129 :     | NONE => let
130 :     val r = newRegistry ()
131 :     in
132 :     plugin_registries :=
133 :     SrcPathMap.insert (!plugin_registries, p, r);
134 :     r
135 :     end))
136 :     in
137 :     rf := augment (!rf)
138 :     end
139 :     in
140 :     fun registerClass (class, rule) =
141 :     upd #classes (fn m => StringMap.insert (m, class, rule))
142 :     fun registerClassifier (SFX_CLASSIFIER c) =
143 :     upd #sfx_classifiers (fn c' => layer (c, c'))
144 :     | registerClassifier (GEN_CLASSIFIER c) =
145 :     upd #gen_classifiers (fn c' => layer (c, c'))
146 :    
147 :     fun transfer_local p = let
148 :     val lr = !local_registry
149 :     in
150 :     case SrcPathMap.find (!plugin_registries, p) of
151 :     NONE => ()
152 :     | SOME pr => let
153 :     fun upd sel join = sel lr := join (! (sel pr), ! (sel lr))
154 :     in
155 :     upd #classes (StringMap.unionWith #1);
156 :     upd #sfx_classifiers layer;
157 :     upd #gen_classifiers layer
158 :     end
159 :     end
160 :    
161 :     fun withPlugin p thunk =
162 :     SafeIO.perform { openIt = fn () => !current_plugin before
163 :     current_plugin := SOME p,
164 :     closeIt = fn prev => (transfer_local p;
165 :     current_plugin := prev),
166 :     work = fn _ => thunk (),
167 :     cleanup = fn _ => () }
168 :     end
169 :    
170 :     datatype extensionStyle =
171 :     EXTEND of (string * class option * tooloptcvt) list
172 :     | REPLACE of string list * (string * class option * tooloptcvt) list
173 :    
174 :     fun extend (EXTEND l) (f, too) =
175 :     map (fn (s, co, toc) => (concat [f, ".", s], co, toc too)) l
176 :     | extend (REPLACE (ol, nl)) (f, too) = let
177 :     val { base, ext } = OS.Path.splitBaseExt f
178 :     fun join b (e, co, toc) =
179 :     (OS.Path.joinBaseExt { base = b, ext = SOME e }, co, toc too)
180 :     fun gen b = map (join b) nl
181 :     fun sameExt (e1: string) (e2: string) = e1 = e2
182 :     in
183 :     case ext of
184 :     NONE => gen base
185 :     | SOME e =>
186 :     if List.exists (sameExt e) ol then gen base else gen f
187 :     end
188 :    
189 :     local
190 :     fun timex f =
191 :     (OS.FileSys.modTime f, true)
192 :     handle _ => (Time.zeroTime, false)
193 :     val op < = Time.<
194 :     fun olderThan t f = OS.FileSys.modTime f < t
195 :     fun cannotAccess tool f =
196 :     raise ToolError { tool = tool, msg = "cannot access " ^ f }
197 :     in
198 :     fun outdated tool (l, f) = let
199 :     val (ftime, fexists) = timex f
200 :     in
201 :     (List.exists (olderThan ftime) l)
202 :     handle _ => if fexists then true else cannotAccess tool f
203 :     end
204 :    
205 :     fun outdated' tool { src, wtn, tgt } = let
206 :     val (st, se) = timex src
207 :     val (tt, te) = timex tgt
208 :     in
209 :     if not se then
210 :     if te then false else cannotAccess tool src
211 :     else if te then
212 :     let val (wt, we) = timex wtn
213 :     in
214 :     if we then wt < st else tt < st
215 :     end
216 :     else true
217 :     end
218 :     end
219 :    
220 :     val openTextOut = AutoDir.openTextOut
221 :     val makeDirs = AutoDir.makeDirs
222 :    
223 :     fun globally lp arg =
224 :     SafeIO.perform { openIt = fn () => !current_plugin before
225 :     current_plugin := NONE,
226 :     closeIt = fn prev => current_plugin := prev,
227 :     work = fn _ => lp arg,
228 :     cleanup = fn _ => () }
229 :    
230 :     (* query default class *)
231 : blume 756 fun defaultClassOf load_plugin (s: fnspec) = let
232 :     val p = #name s
233 :     val mkfname = SrcPath.osstring_prefile o #mkpath s
234 :     val gcarg = { name = p, mkfname = mkfname }
235 : blume 742 fun sfx_gen_check e =
236 :     case sfx_classifiers e of
237 :     SOME c => SOME c
238 : blume 756 | NONE => gen_classifiers gcarg
239 : blume 742 in
240 :     case OS.Path.ext p of
241 :     SOME e =>
242 :     (case sfx_gen_check e of
243 :     SOME c => SOME c
244 :     | NONE => let
245 :     val plugin = concat ["$/", e, "-ext.cm"]
246 :     in
247 :     if globally load_plugin plugin then sfx_gen_check e
248 :     else NONE
249 :     end)
250 : blume 756 | NONE => gen_classifiers gcarg
251 : blume 742 end
252 :    
253 :     fun parseOptions { tool, keywords, options } = let
254 :     fun err m = raise ToolError { tool = tool, msg = m }
255 :     fun isKW kw = List.exists (fn kw' => kw = kw') keywords
256 :     fun loop ([], m, ro) = { matches = fn kw => StringMap.find (m, kw),
257 :     restoptions = rev ro }
258 :     | loop (STRING { name, ... } :: t, m, ro) = loop (t, m, name :: ro)
259 :     | loop (SUBOPTS { name, opts } :: t, m, ro) =
260 :     if not (isKW name) then
261 :     raise err (concat ["keyword option `", name,
262 :     "' not recognized"])
263 :     else (case StringMap.find (m, name) of
264 :     SOME _ => err (concat ["keyword option `", name,
265 :     "' specified more than once"])
266 :     | NONE => loop (t, StringMap.insert (m, name, opts), ro))
267 :     in
268 :     loop (options, StringMap.empty, [])
269 :     end
270 :    
271 : blume 873 fun smlrule { spec, context, native2pathmaker, defaultClassOf, sysinfo } = let
272 : blume 742 val { name, mkpath, opts = oto, derived, ... } : spec = spec
273 :     val tool = "sml"
274 :     fun err s = raise ToolError { tool = tool, msg = s }
275 :     val kw_setup = "setup"
276 : blume 818 val kw_lambdasplit = "lambdasplit"
277 :     val UseDefault = NONE
278 :     val Suggest = SOME
279 :     val (srq, setup, splitting) =
280 : blume 742 case oto of
281 : blume 818 NONE => (Sharing.DONTCARE, (NONE, NONE), UseDefault)
282 : blume 742 | SOME to => let
283 :     val { matches, restoptions } =
284 :     parseOptions { tool = tool,
285 : blume 818 keywords = [kw_setup, kw_lambdasplit],
286 : blume 742 options = to }
287 :     val srq =
288 :     case restoptions of
289 :     [] => Sharing.DONTCARE
290 :     | ["shared"] => Sharing.SHARED
291 :     | ["private"] => Sharing.PRIVATE
292 :     | _ => err "invalid option(s)"
293 :     val setup =
294 :     case matches kw_setup of
295 :     NONE => (NONE, NONE)
296 :     | SOME [] => (NONE, NONE)
297 :     | SOME [STRING s] => (SOME (#name s), NONE)
298 :     | SOME [SUBOPTS { name = "pre",
299 :     opts = [STRING pre] }] =>
300 :     (SOME (#name pre), NONE)
301 :     | SOME [SUBOPTS { name = "post",
302 :     opts = [STRING post] }] =>
303 :     (NONE, SOME (#name post))
304 :     | (SOME [SUBOPTS { name = "pre",
305 :     opts = [STRING pre] },
306 :     SUBOPTS { name = "post",
307 :     opts = [STRING post] }] |
308 :     SOME [SUBOPTS { name = "post",
309 :     opts = [STRING post] },
310 :     SUBOPTS { name = "pre",
311 :     opts = [STRING pre] }]) =>
312 :     (SOME (#name pre), SOME (#name post))
313 :     | _ => err "invalid setup spec"
314 : blume 818 val splitting = let
315 :     fun invalid () = err "invalid lambdasplit spec"
316 :     fun spec (s: fnspec) =
317 :     case #name s of
318 :     "default" => UseDefault
319 :     | "on" => Suggest (SOME 0)
320 :     | "off" => Suggest NONE
321 :     | "infinity" => Suggest (SOME 100000000)
322 :     | n =>
323 :     (case Int.fromString n of
324 :     SOME i => Suggest (SOME i)
325 :     | NONE => invalid ())
326 :     in
327 :     case matches kw_lambdasplit of
328 :     NONE => UseDefault
329 :     | SOME [] => Suggest (SOME 0)(* == "on" *)
330 :     | SOME [STRING x] => spec x
331 :     | _ => err "invalid lambdasplit spec"
332 :     end
333 : blume 742 in
334 : blume 818 (srq, setup, splitting)
335 : blume 742 end
336 : blume 756 val p = srcpath (mkpath ())
337 : blume 742 in
338 : blume 818 ({ smlfiles = [(p, srq, setup, splitting)],
339 : blume 742 sources = [(p, { class = "sml", derived = derived })],
340 :     cmfiles = [] },
341 :     [])
342 :     end
343 : blume 873 fun cmrule { spec, context, native2pathmaker, defaultClassOf, sysinfo } = let
344 : blume 742 val { name, mkpath, opts = oto, derived, ... } : spec = spec
345 :     fun err m = raise ToolError { tool = "cm", msg = m }
346 :     fun proc_opts (rb, vrq, []) = (rb, vrq)
347 :     | proc_opts (_, _, STRING _ :: _) = err "ill-formed option"
348 :     | proc_opts (rb, vrq, SUBOPTS { name = "version", opts } :: r) =
349 :     let fun ill () = err "ill-formed version specification"
350 :     in
351 :     case (vrq, opts) of
352 :     (SOME _, _) =>
353 :     err "version cannot be specified more than once"
354 :     | (NONE, [STRING { name, ... }]) =>
355 :     (case Version.fromString name of
356 :     NONE => ill ()
357 :     | SOME v => proc_opts (rb, SOME v, r))
358 :     | _ => ill ()
359 :     end
360 :     | proc_opts (rb, vrq, SUBOPTS { name = "bind", opts } :: r) =
361 :     (case opts of
362 :     [SUBOPTS { name = "anchor", opts = [STRING { name, ... }] },
363 :     SUBOPTS { name = "value", opts = [STRING v] }] =>
364 : blume 756 proc_opts ({ anchor = name, value = #mkpath v () }
365 : blume 742 :: rb,
366 :     vrq, r)
367 :     | _ => err "ill-formed bind specification")
368 :     | proc_opts (_, _, SUBOPTS { name, ... } :: _) =
369 :     err ("unknown option: " ^ name)
370 :     val (rb, vrq) = case oto of
371 :     NONE => ([], NONE)
372 :     | SOME l => proc_opts ([], NONE, l)
373 : blume 756 val p = srcpath (mkpath ())
374 : blume 742 in
375 :     ({ smlfiles = [],
376 :     sources = [(p, { class = "cm", derived = derived })],
377 :     cmfiles = [(p, vrq, rev rb)] },
378 :     [])
379 :     end
380 :    
381 : blume 873 fun expand { error, local_registry = lr, spec, context, load_plugin, sysinfo } = let
382 : blume 756 val dummy = ({ smlfiles = [], cmfiles = [], sources = [] }, [])
383 :     fun norule _ = dummy
384 :     fun native2pathmaker s () =
385 : blume 742 SrcPath.native { err = error } { context = context, spec = s }
386 :     fun class2rule class =
387 :     case classes class of
388 :     SOME rule => rule
389 :     | NONE => let
390 :     val base = concat ["$/", class, "-tool"]
391 :     val plugin = OS.Path.joinBaseExt { base = base,
392 :     ext = SOME "cm" }
393 :     fun complain () =
394 : blume 756 (error (concat ["unknown class: ", class]);
395 :     norule)
396 : blume 742 in
397 :     if globally (load_plugin context) plugin then
398 :     case classes class of
399 :     SOME rule => rule
400 :     | NONE => complain ()
401 :     else complain ()
402 :     end
403 :    
404 : blume 756 fun expand1 (spec as { name, mkpath, class = co, ... }) = let
405 :     val fns = { name = name, mkpath = mkpath }
406 : blume 742 val rule =
407 :     case co of
408 :     SOME c0 => class2rule (String.map Char.toLower c0)
409 :     | NONE =>
410 : blume 756 (case defaultClassOf (load_plugin context) fns of
411 : blume 742 SOME c => class2rule c
412 : blume 756 | NONE =>
413 :     (error (concat ["unable to classify: ", name]);
414 :     norule))
415 : blume 742 fun rcontext rf = let
416 :     val dir = SrcPath.osstring_dir context
417 :     val cwd = OS.FileSys.getDir ()
418 :     in
419 :     SafeIO.perform { openIt = fn () => OS.FileSys.chDir dir,
420 :     closeIt = fn () => OS.FileSys.chDir cwd,
421 :     work = rf,
422 :     cleanup = fn _ => () }
423 :     end
424 :     in
425 :     rule { spec = spec, context = rcontext,
426 : blume 756 native2pathmaker = native2pathmaker,
427 : blume 873 defaultClassOf = defaultClassOf (load_plugin context),
428 :     sysinfo = sysinfo }
429 : blume 742 handle ToolError { tool, msg } =>
430 :     (error (concat ["tool \"", tool, "\" failed: ", msg]);
431 : blume 756 dummy)
432 : blume 742 end
433 :     fun loop (expansion, []) = expansion
434 :     | loop ({ smlfiles, cmfiles, sources }, item :: items) = let
435 :     val ({ smlfiles = sfl, cmfiles = cfl, sources = sl }, il) =
436 :     expand1 item
437 :     in
438 :     loop ({ smlfiles = smlfiles @ sfl,
439 :     cmfiles = cmfiles @ cfl,
440 :     sources = sources @ sl },
441 :     il @ items)
442 :     end
443 :     in
444 :     SafeIO.perform { openIt = fn () => !local_registry
445 :     before local_registry := lr,
446 :     closeIt = fn prev => local_registry := prev,
447 :     work = fn _ => loop ({ smlfiles = [], cmfiles = [],
448 :     sources = [] },
449 :     [spec]),
450 :     cleanup = fn _ => () }
451 :     end
452 :    
453 :     local
454 :     fun sfx (s, c) =
455 :     registerClassifier (stdSfxClassifier { sfx = s, class = c })
456 :     in
457 :     val _ = registerClass ("sml", smlrule)
458 :     val _ = registerClass ("cm", cmrule)
459 :    
460 :     val _ = sfx ("sml", "sml")
461 :     val _ = sfx ("sig", "sml")
462 :     val _ = sfx ("fun", "sml")
463 :     val _ = sfx ("cm", "cm")
464 :     end
465 :     end

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