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/parse/parse.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/parse/parse.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 666 - (view) (download)

1 : blume 270 (*
2 :     * Parser for CM description files.
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 : blume 310 signature PARSE = sig
9 : blume 666 val parse : { load_plugin: SrcPath.dir -> string -> bool,
10 : blume 537 gr: GroupReg.groupreg,
11 :     param: GeneralParams.param,
12 :     stabflag: bool option,
13 : blume 666 group: SrcPath.file,
14 : blume 537 init_group: CMSemant.group,
15 :     paranoid: bool }
16 :     -> (CMSemant.group * GeneralParams.info) option
17 : blume 367 val reset : unit -> unit
18 : blume 666 val listLibs : unit -> SrcPath.file list
19 : blume 505 val dropPickles : unit -> unit
20 : blume 666 val dismissLib : SrcPath.file -> unit
21 : blume 268 end
22 :    
23 : blume 362 functor ParseFn (val pending : unit -> DependencyGraph.impexp SymbolMap.map
24 : blume 537 val evictStale : unit -> unit
25 : blume 588 structure StabModmap : STAB_MODMAP
26 :     structure Stabilize : STABILIZE) :> PARSE = struct
27 : blume 268
28 : blume 569 structure VerifyStable = VerStabFn (structure Stabilize = Stabilize)
29 :    
30 : blume 268 val lookAhead = 30
31 :    
32 :     structure S = GenericVC.Source
33 :     structure EM = GenericVC.ErrorMsg
34 :     structure SM = GenericVC.SourceMap
35 : blume 348 structure GG = GroupGraph
36 : blume 569 structure DG = DependencyGraph
37 : blume 268
38 :     structure CMLrVals = CMLrValsFun (structure Token = LrParser.Token)
39 :     structure CMLex = CMLexFun (structure Tokens = CMLrVals.Tokens)
40 :     structure CMParse =
41 :     JoinWithArg (structure ParserData = CMLrVals.ParserData
42 :     structure Lex = CMLex
43 :     structure LrParser = LrParser)
44 :    
45 : blume 367 (* the "stable group cache" *)
46 :     val sgc = ref (SrcPathMap.empty: CMSemant.group SrcPathMap.map)
47 :     fun reset () = sgc := SrcPathMap.empty
48 :    
49 : blume 569 fun majorGC () = SMLofNJ.Internals.GC.doGC 7
50 :    
51 : blume 537 fun cachedStable (p, ig as GG.GROUP { grouppath, ... }) =
52 :     if SrcPath.compare (p, grouppath) = EQUAL then SOME ig
53 :     else SrcPathMap.find (!sgc, p)
54 : blume 587 | cachedStable (_, GG.ERRORGROUP) = NONE
55 : blume 514
56 : blume 573 (* When an entry A vanishes from the stable cache (this only happens in
57 :     * paranoid mode), then all the other ones that refer to A must
58 :     * vasish, too. They might still be valid themselves, but if they
59 :     * had been unpickled before A became invalid they will point to
60 :     * invalid data. By removing them from the cache we force them to
61 :     * be re-read and re-unpickled. This restores sanity. *)
62 : blume 587 fun delCachedStable (p, GG.GROUP { grouppath = igp, ... }) =
63 :     let val changed = ref true
64 :     fun canStay GG.ERRORGROUP = true (* doesn't matter *)
65 :     | canStay (GG.GROUP { sublibs, ... }) = let
66 : blume 666 fun goodSublib (p, gth, _) =
67 : blume 652 case gth () of
68 :     GG.GROUP { kind = GG.LIB { kind = GG.STABLE _,
69 :     ... }, ... } =>
70 :     SrcPath.compare (p, igp) = EQUAL orelse
71 :     SrcPathMap.inDomain (!sgc, p)
72 :     | _ => true
73 : blume 632 val cs = List.all goodSublib sublibs
74 : blume 587 in
75 :     if cs then () else changed := true;
76 :     cs
77 :     end
78 : blume 573 in
79 : blume 587 (sgc := #1 (SrcPathMap.remove (!sgc, p)))
80 :     handle LibBase.NotFound => ();
81 :     while !changed do
82 :     (changed := false; sgc := SrcPathMap.filter canStay (!sgc))
83 : blume 573 end
84 : blume 587 | delCachedStable (_, GG.ERRORGROUP) = ()
85 : blume 573
86 : blume 479 fun listLibs () = map #1 (SrcPathMap.listItemsi (!sgc))
87 : blume 404
88 : blume 505 fun dropPickles () = let
89 : blume 632 fun drop (GG.GROUP { kind = GG.LIB { kind = GG.STABLE dropper,
90 :     ... }, ... }) = dropper ()
91 : blume 505 | drop _ = ()
92 :     in
93 : blume 507 SrcPathMap.app drop (!sgc)
94 : blume 505 end
95 :    
96 : blume 404 fun dismissLib l =
97 : blume 588 (StabModmap.reset ();
98 :     sgc := #1 (SrcPathMap.remove (!sgc, l)))
99 : blume 404 handle LibBase.NotFound => ()
100 :    
101 : blume 537 fun parse args = let
102 : blume 569 val { load_plugin, gr, param, stabflag, group,
103 :     init_group, paranoid } = args
104 : blume 270
105 : blume 587 val { grouppath = init_gname, ... } =
106 :     case init_group of
107 :     GG.GROUP x => x
108 :     | GG.ERRORGROUP =>
109 :     EM.impossible "parse.sml: parse: bad init group"
110 : blume 537
111 : blume 311 val stabthis = isSome stabflag
112 :     val staball = stabflag = SOME true
113 : blume 310
114 : blume 537 val groupreg = gr
115 : blume 309 val errcons = EM.defaultConsumer ()
116 : blume 666 val ginfo0 = { param = param, groupreg = groupreg, errcons = errcons }
117 : blume 587 val keep_going = #keep_going param
118 : blume 268
119 : blume 301 (* The "group cache" -- we store "group options"; having
120 :     * NONE registered for a group means that a previous attempt
121 : blume 537 * to parse it had failed.
122 :     * This registry is primed with the "init" group because it is
123 :     * "special" and cannot be parsed directly. *)
124 :     val gc = ref (SrcPathMap.singleton (init_gname, SOME init_group))
125 : blume 273
126 : blume 569 val em = ref StableMap.empty
127 :    
128 : blume 587 fun update_em (GG.GROUP ns_g, GG.GROUP s_g) =
129 :     let val s_e = #exports s_g
130 : blume 652 fun add (sy, (snth, _, _)) =
131 :     case snth () of
132 :     (_ , DG.SB_SNODE (DG.SNODE sn)) =>
133 :     (case SymbolMap.find (s_e, sy) of
134 :     NONE => ()
135 :     | SOME (bnth, _, _) =>
136 :     (case bnth () of
137 :     (_, DG.SB_BNODE (DG.BNODE bn, _)) =>
138 :     em := StableMap.insert (!em, #bininfo bn,
139 :     #smlinfo sn)
140 :     | _ => ()))
141 :     | _ => ()
142 : blume 587 in
143 :     SymbolMap.appi add (#exports ns_g)
144 :     end
145 :     | update_em _ = ()
146 : blume 569
147 :     fun registerNewStable (p, g) =
148 :     (sgc := SrcPathMap.insert (!sgc, p, g);
149 :     SrcPathSet.app (SmlInfo.cleanGroup true) (Reachable.groupsOf g);
150 :     evictStale ();
151 :     (gc := #1 (SrcPathMap.remove (!gc, p));
152 :     (* ... and for good measure, do a major GC... *)
153 :     majorGC ())
154 :     handle LibBase.NotFound => ())
155 :    
156 : blume 435 fun hasCycle (group, groupstack) = let
157 : blume 286 (* checking for cycles among groups and printing them nicely *)
158 : blume 294 fun findCycle ([], _) = []
159 :     | findCycle ((h as (g, (s, p1, p2))) :: t, cyc) =
160 : blume 354 if SrcPath.compare (g, group) = EQUAL then rev (h :: cyc)
161 : blume 294 else findCycle (t, h :: cyc)
162 :     fun report ((g, (s, p1, p2)), hist) = let
163 :     fun pphist pps = let
164 :     fun loop (_, []) = ()
165 :     | loop (g0, (g, (s, p1, p2)) :: t) = let
166 :     val s = EM.matchErrorString s (p1, p2)
167 :     in
168 : blume 435 PrettyPrint.add_newline pps;
169 : blume 294 PrettyPrint.add_string pps s;
170 :     PrettyPrint.add_string pps ": importing ";
171 : blume 666 PrettyPrint.add_string pps (SrcPath.descr g0);
172 : blume 294 loop (g, t)
173 :     end
174 : blume 273 in
175 : blume 294 loop (g, hist)
176 : blume 273 end
177 :     in
178 : blume 294 EM.error s (p1, p2) EM.COMPLAIN
179 :     ("group hierarchy forms a cycle with " ^
180 : blume 666 SrcPath.descr group)
181 : blume 294 pphist
182 : blume 273 end
183 : blume 435 in
184 :     case findCycle (groupstack, []) of
185 :     h :: t => (report (h, t); true)
186 :     | [] => false
187 :     end
188 : blume 273
189 : blume 666 fun mparse args = let
190 :     val (group, vers, groupstack, pErrFlag, stabthis, curlib,
191 :     ginfo, rb) = args
192 :     fun getStable stablestack (ginfo, gpath, vers, rb) = let
193 : blume 435 (* This is a separate "findCycle" routine that detects
194 :     * cycles among stable libraries. These cycles should
195 :     * never occur unless someone purposefully renames
196 :     * stable library files in a bad way. *)
197 :     fun findCycle ([], _) = NONE
198 :     | findCycle (h :: t, cyc) =
199 :     if SrcPath.compare (h, gpath) = EQUAL then SOME (h :: cyc)
200 :     else findCycle (t, h :: cyc)
201 :     fun report cyc = let
202 :     fun pphist pps = let
203 :     fun loop [] = ()
204 :     | loop (h :: t) =
205 :     (PrettyPrint.add_newline pps;
206 :     PrettyPrint.add_string pps (SrcPath.descr h);
207 :     loop t)
208 :     in
209 :     loop (rev cyc)
210 :     end
211 :     in
212 :     EM.errorNoFile (errcons, pErrFlag) SM.nullRegion
213 :     EM.COMPLAIN
214 :     ("stable libraries form a cycle with " ^
215 :     SrcPath.descr gpath)
216 :     pphist
217 :     end
218 :     fun load () = let
219 : blume 666 val go = Stabilize.loadStable
220 : blume 435 { getGroup = getStable (gpath :: stablestack),
221 :     anyerrors = pErrFlag }
222 : blume 666 (ginfo, gpath, vers, rb)
223 : blume 435 in
224 :     case go of
225 :     NONE => NONE
226 :     | SOME g =>
227 : blume 514 (registerNewStable (gpath, g);
228 : blume 435 Say.vsay ["[library ", SrcPath.descr gpath,
229 :     " is stable]\n"];
230 :     SOME g)
231 :     end
232 : blume 310 in
233 : blume 435 case findCycle (stablestack, []) of
234 : blume 537 NONE => (case cachedStable (gpath, init_group) of
235 : blume 435 SOME g => SOME g
236 :     | NONE => load ())
237 :     | SOME cyc => (report cyc; NONE)
238 : blume 310 end
239 : blume 537
240 :     fun stabilize NONE = NONE
241 :     | stabilize (SOME g) =
242 :     (case g of
243 : blume 587 GG.ERRORGROUP => NONE
244 :     | GG.GROUP { kind = GG.LIB _, ... } => let
245 : blume 537 val go = Stabilize.stabilize ginfo
246 :     { group = g, anyerrors = pErrFlag }
247 :     in
248 :     case go of
249 :     NONE => NONE
250 : blume 569 | SOME g' =>
251 :     (registerNewStable (group, g'); SOME g')
252 : blume 537 end
253 :     | _ => SOME g)
254 : blume 435 in
255 : blume 537 case SrcPathMap.find (!gc, group) of
256 :     SOME gopt => gopt
257 :     | NONE => let
258 : blume 666 fun try_s () = getStable [] (ginfo, group, vers, rb)
259 :     fun try_n () =
260 :     parse' (group, groupstack, pErrFlag, curlib, ginfo, rb)
261 : blume 537 fun reg gopt =
262 :     (gc := SrcPathMap.insert (!gc, group, gopt); gopt)
263 :     fun proc_n gopt =
264 :     reg (if stabthis then stabilize gopt
265 :     else (SmlInfo.cleanGroup false group; gopt))
266 :     in
267 :     if paranoid then
268 :     case try_n () of
269 :     NONE => reg NONE
270 : blume 569 | SOME g => let
271 :     val gopt' =
272 :     if VerifyStable.verify ginfo (!em) g then
273 :     reg (case try_s () of
274 :     NONE => SOME g
275 :     | SOME g' => SOME g')
276 : blume 573 else (delCachedStable (group, init_group);
277 :     proc_n (SOME g))
278 : blume 569 in
279 :     case gopt' of
280 :     NONE => NONE
281 :     | SOME g' => (update_em (g, g'); SOME g')
282 :     end
283 : blume 537 else case try_s () of
284 :     SOME g => reg (SOME g)
285 :     | NONE => proc_n (try_n ())
286 :     end
287 : blume 435 end
288 : blume 310
289 : blume 537 (* Parse' is used when we are sure that we don't want to load
290 :     * a stable library. *)
291 : blume 666 and parse' (group, groupstack, pErrFlag, curlib, ginfo, rb) = let
292 : blume 435
293 : blume 666 val ginfo = GeneralParams.bind ginfo rb
294 :    
295 : blume 294 (* normal processing -- used when there is no cycle to report *)
296 :     fun normal_processing () = let
297 : blume 435 val _ = Say.vsay ["[scanning ", SrcPath.descr group, "]\n"]
298 :    
299 : blume 666 val context = SrcPath.dir group
300 : blume 294
301 : blume 345 fun work stream = let
302 :     val source =
303 : blume 354 S.newSource (SrcPath.osstring group,
304 :     1, stream, false, errcons)
305 : blume 345 val sourceMap = #sourceMap source
306 :     val _ = GroupReg.register groupreg (group, source)
307 : blume 297
308 : blume 345 (* We can hard-wire the source into this
309 :     * error function because the function is only for
310 :     * immediate use and doesn't get stored into persistent
311 :     * data structures. *)
312 :     fun error r m =
313 :     EM.error source r EM.COMPLAIN m EM.nullErrorBody
314 : blume 397 fun obsolete r =
315 : blume 433 if #get StdConfig.warn_obsolete () then
316 : blume 397 EM.error source r EM.WARN
317 :     "old-style operator (obsolete)" EM.nullErrorBody
318 :     else ()
319 : blume 294
320 : blume 345 (* recParse returns a group (not an option).
321 : blume 380 * This function is used to parse sub-groups.
322 : blume 345 * Errors are propagated by explicitly setting the
323 :     * "anyErrors" flag of the parent group. *)
324 : blume 666 fun recParse (p1, p2) curlib (p, v, rb) = let
325 : blume 367 val gs' = (group, (source, p1, p2)) :: groupstack
326 : blume 587 (* my error flag *)
327 :     val mef = #anyErrors source
328 : blume 294 in
329 : blume 587 (* unless we are in keep-going mode we do no further
330 :     * recursive traversals once there was an error on
331 :     * this group. *)
332 :     if !mef andalso not keep_going then GG.ERRORGROUP
333 : blume 666 else case mparse (p, v, gs', mef, staball,
334 :     curlib, ginfo, rb) of
335 : blume 587 NONE => (mef := true; GG.ERRORGROUP)
336 :     | SOME res => res
337 : blume 294 end
338 : blume 345 handle exn as IO.Io _ =>
339 :     (error (p1, p2) (General.exnMessage exn);
340 : blume 587 GG.ERRORGROUP)
341 : blume 345
342 : blume 587 fun doMember ({ name, mkpath }, p1, p2, c, oto) =
343 : blume 632 CMSemant.member { gp = ginfo,
344 :     rparse = recParse (p1, p2),
345 :     load_plugin = load_plugin }
346 :     { name = name, mkpath = mkpath,
347 :     class = c, tooloptions = oto,
348 :     group = (group, (p1, p2)),
349 :     context = context }
350 : blume 345
351 :     (* Build the argument for the lexer; the lexer's local
352 :     * state is encapsulated here to make sure the parser
353 :     * is re-entrant. *)
354 :     val lexarg = let
355 :     (* local state *)
356 :     val depth = ref 0
357 :     val curstring = ref []
358 :     val startpos = ref 0
359 :     val instring = ref false
360 :     (* handling comments *)
361 :     fun enterC () = depth := !depth + 1
362 :     fun leaveC () = let
363 :     val d = !depth - 1
364 :     in
365 :     depth := d;
366 :     d = 0
367 :     end
368 :     (* handling strings *)
369 :     fun newS pos =
370 :     (instring := true;
371 :     curstring := [];
372 :     startpos := pos)
373 :     fun addS c = curstring := c :: !curstring
374 :     fun addSC (s, offs) =
375 :     addS (chr (ord (String.sub (s, 2)) - offs))
376 :     fun addSN (s, pos) = let
377 :     val ns = substring (s, 1, 3)
378 :     val n = Int.fromString ns
379 :     in
380 :     addS (chr (valOf n))
381 :     handle _ =>
382 :     error (pos, pos + size s)
383 :     ("illegal decimal char spec: " ^ ns)
384 :     end
385 :     fun getS (pos, tok) =
386 :     (instring := false;
387 :     tok (implode (rev (!curstring)), !startpos, pos))
388 :     (* handling EOF *)
389 :     fun handleEof () = let
390 :     val pos = SM.lastChange sourceMap
391 :     in
392 :     if !depth > 0 then
393 :     error (pos, pos)
394 :     "unexpected end of input in comment"
395 :     else if !instring then
396 :     error (pos, pos)
397 :     "unexpected end of input in string"
398 :     else ();
399 :     pos
400 :     end
401 :     (* handling line breaks *)
402 :     fun newline pos = SM.newline sourceMap pos
403 :     (* handling #line directives *)
404 :     fun sync (p, t) = let
405 :     fun sep c = c = #"#" orelse Char.isSpace c
406 :     fun cvt s = getOpt (Int.fromString s, 0)
407 :     fun r (line, col, file) = SM.resynch sourceMap
408 :     (p, { fileName = file,
409 :     line = line, column = col })
410 :     in
411 :     case String.tokens sep t of
412 :     [_, line] =>
413 :     r (cvt line, NONE, NONE)
414 :     | [_, line, file] =>
415 :     r (cvt line, NONE, SOME file)
416 :     | [_, line, col, file] =>
417 :     r (cvt line, SOME (cvt col), SOME file)
418 :     | _ => error (p, p + size t)
419 :     "illegal #line directive"
420 :     end
421 : blume 294 in
422 : blume 345 { enterC = enterC,
423 :     leaveC = leaveC,
424 :     newS = newS,
425 :     addS = addS,
426 :     addSC = addSC,
427 :     addSN = addSN,
428 :     getS = getS,
429 :     handleEof = handleEof,
430 :     newline = newline,
431 : blume 397 obsolete = obsolete,
432 : blume 345 error = error,
433 :     sync = sync}
434 : blume 294 end
435 : blume 345
436 :     fun inputc k = TextIO.input stream
437 :    
438 : blume 595 val tokenStream = CMParse.makeLexer inputc lexarg
439 : blume 345 val (parseResult, _) =
440 :     CMParse.parse (lookAhead, tokenStream,
441 :     fn (s,p1,p2) => error (p1, p2) s,
442 : blume 397 (group, context, obsolete, error,
443 : blume 537 doMember, curlib, ginfo, init_group))
444 : blume 294 in
445 : blume 345 if !(#anyErrors source) then NONE
446 :     else SOME parseResult
447 : blume 294 end
448 : blume 364 fun openIt () = TextIO.openIn (SrcPath.osstring group)
449 : blume 268 in
450 : blume 537 SafeIO.perform { openIt = openIt,
451 :     closeIt = TextIO.closeIn,
452 :     work = work,
453 :     cleanup = fn _ => () }
454 : blume 268 end
455 : blume 294 handle LrParser.ParseError => NONE
456 : blume 268 in
457 : blume 435 if hasCycle (group, groupstack) then NONE
458 :     else normal_processing ()
459 : blume 268 end
460 :     in
461 : blume 487 SmlInfo.newGeneration ();
462 : blume 666 case mparse (group, NONE, [], ref false, stabthis, NONE, ginfo0, []) of
463 : blume 303 NONE => NONE
464 : blume 666 | SOME g => SOME (g, ginfo0)
465 : blume 274 end
466 : blume 268 end

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