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

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