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

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