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

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