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

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