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

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