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

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