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 505 - (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 301 val parse :
10 : blume 355 GroupReg.groupreg option ->
11 : blume 310 GeneralParams.param -> bool option ->
12 : blume 354 SrcPath.t -> (CMSemant.group * GeneralParams.info) option
13 : blume 367 val reset : unit -> unit
14 : blume 479 val listLibs : unit -> SrcPath.t list
15 : blume 505 val dropPickles : unit -> unit
16 : blume 404 val dismissLib : SrcPath.t -> unit
17 : blume 268 end
18 :    
19 : blume 362 functor ParseFn (val pending : unit -> DependencyGraph.impexp SymbolMap.map
20 :     structure Stabilize: STABILIZE) :> PARSE = struct
21 : blume 268
22 :     val lookAhead = 30
23 :    
24 :     structure S = GenericVC.Source
25 :     structure EM = GenericVC.ErrorMsg
26 :     structure SM = GenericVC.SourceMap
27 : blume 348 structure GG = GroupGraph
28 : blume 268
29 :     structure CMLrVals = CMLrValsFun (structure Token = LrParser.Token)
30 :     structure CMLex = CMLexFun (structure Tokens = CMLrVals.Tokens)
31 :     structure CMParse =
32 :     JoinWithArg (structure ParserData = CMLrVals.ParserData
33 :     structure Lex = CMLex
34 :     structure LrParser = LrParser)
35 :    
36 : blume 367 (* the "stable group cache" *)
37 :     val sgc = ref (SrcPathMap.empty: CMSemant.group SrcPathMap.map)
38 :     fun reset () = sgc := SrcPathMap.empty
39 :    
40 : blume 479 fun listLibs () = map #1 (SrcPathMap.listItemsi (!sgc))
41 : blume 404
42 : blume 505 fun dropPickles () = let
43 :     fun drop (GG.GROUP { kind = GG.STABLELIB dropper, ... }) = dropper ()
44 :     | drop _ = ()
45 :     in
46 :     if #get (StdConfig.conserve_memory) () then
47 :     SrcPathMap.app drop (!sgc)
48 :     else ()
49 :     end
50 :    
51 : blume 404 fun dismissLib l =
52 :     (sgc := #1 (SrcPathMap.remove (!sgc, l)))
53 :     handle LibBase.NotFound => ()
54 :    
55 : blume 355 fun parse gropt param stabflag group = let
56 : blume 270
57 : blume 311 val stabthis = isSome stabflag
58 :     val staball = stabflag = SOME true
59 : blume 310
60 : blume 355 val groupreg =
61 :     case gropt of
62 :     SOME r => r
63 :     | NONE => GroupReg.new ()
64 : blume 309 val errcons = EM.defaultConsumer ()
65 :     val ginfo = { param = param, groupreg = groupreg, errcons = errcons }
66 : blume 268
67 : blume 301 (* The "group cache" -- we store "group options"; having
68 :     * NONE registered for a group means that a previous attempt
69 :     * to parse it had failed. *)
70 : blume 354 val gc = ref (SrcPathMap.empty: CMSemant.group option SrcPathMap.map)
71 : blume 273
72 : blume 435 fun hasCycle (group, groupstack) = let
73 : blume 286 (* checking for cycles among groups and printing them nicely *)
74 : blume 294 fun findCycle ([], _) = []
75 :     | findCycle ((h as (g, (s, p1, p2))) :: t, cyc) =
76 : blume 354 if SrcPath.compare (g, group) = EQUAL then rev (h :: cyc)
77 : blume 294 else findCycle (t, h :: cyc)
78 :     fun report ((g, (s, p1, p2)), hist) = let
79 :     fun pphist pps = let
80 :     fun loop (_, []) = ()
81 :     | loop (g0, (g, (s, p1, p2)) :: t) = let
82 :     val s = EM.matchErrorString s (p1, p2)
83 :     in
84 : blume 435 PrettyPrint.add_newline pps;
85 : blume 294 PrettyPrint.add_string pps s;
86 :     PrettyPrint.add_string pps ": importing ";
87 : blume 354 PrettyPrint.add_string pps (SrcPath.specOf g0);
88 : blume 294 loop (g, t)
89 :     end
90 : blume 273 in
91 : blume 294 loop (g, hist)
92 : blume 273 end
93 :     in
94 : blume 294 EM.error s (p1, p2) EM.COMPLAIN
95 :     ("group hierarchy forms a cycle with " ^
96 : blume 354 SrcPath.specOf group)
97 : blume 294 pphist
98 : blume 273 end
99 : blume 435 in
100 :     case findCycle (groupstack, []) of
101 :     h :: t => (report (h, t); true)
102 :     | [] => false
103 :     end
104 : blume 273
105 : blume 435 fun mparse (group, groupstack, pErrFlag, stabthis, curlib) = let
106 :     fun getStable stablestack gpath = let
107 :     (* This is a separate "findCycle" routine that detects
108 :     * cycles among stable libraries. These cycles should
109 :     * never occur unless someone purposefully renames
110 :     * stable library files in a bad way. *)
111 :     fun findCycle ([], _) = NONE
112 :     | findCycle (h :: t, cyc) =
113 :     if SrcPath.compare (h, gpath) = EQUAL then SOME (h :: cyc)
114 :     else findCycle (t, h :: cyc)
115 :     fun report cyc = let
116 :     fun pphist pps = let
117 :     fun loop [] = ()
118 :     | loop (h :: t) =
119 :     (PrettyPrint.add_newline pps;
120 :     PrettyPrint.add_string pps (SrcPath.descr h);
121 :     loop t)
122 :     in
123 :     loop (rev cyc)
124 :     end
125 :     in
126 :     EM.errorNoFile (errcons, pErrFlag) SM.nullRegion
127 :     EM.COMPLAIN
128 :     ("stable libraries form a cycle with " ^
129 :     SrcPath.descr gpath)
130 :     pphist
131 :     end
132 :     fun load () = let
133 :     val go = Stabilize.loadStable ginfo
134 :     { getGroup = getStable (gpath :: stablestack),
135 :     anyerrors = pErrFlag }
136 :     gpath
137 :     in
138 :     case go of
139 :     NONE => NONE
140 :     | SOME g =>
141 :     (sgc := SrcPathMap.insert (!sgc, gpath, g);
142 :     Say.vsay ["[library ", SrcPath.descr gpath,
143 :     " is stable]\n"];
144 : blume 492 SrcPathSet.app SmlInfo.cleanGroup
145 :     (Reachable.groupsOf g);
146 : blume 435 SOME g)
147 :     end
148 : blume 310 in
149 : blume 435 case findCycle (stablestack, []) of
150 :     NONE => (case SrcPathMap.find (!sgc, gpath) of
151 :     SOME g => SOME g
152 :     | NONE => load ())
153 :     | SOME cyc => (report cyc; NONE)
154 : blume 310 end
155 : blume 435 in
156 :     case getStable [] group of
157 :     SOME g => SOME g
158 :     | NONE =>
159 :     (case SrcPathMap.find (!gc, group) of
160 :     SOME gopt => gopt
161 :     | NONE => let
162 :     val pres =
163 :     parse' (group, groupstack, pErrFlag,
164 :     stabthis, curlib)
165 :     in
166 :     gc := SrcPathMap.insert (!gc, group, pres);
167 :     pres
168 :     end)
169 :     end
170 : blume 310
171 : blume 435 and parse' (group, groupstack, pErrFlag, stabthis, curlib) = let
172 :    
173 : blume 340 (* We stabilize libraries only because a stable library will
174 :     * encompass the contents of its sub-groups
175 :     * (but not sub-libraries!). *)
176 : blume 348 fun stabilize (g as GG.GROUP { kind = GG.NOLIB, ... }) = SOME g
177 : blume 340 | stabilize g =
178 : blume 311 Stabilize.stabilize ginfo { group = g, anyerrors = pErrFlag }
179 : blume 310
180 : blume 294 (* normal processing -- used when there is no cycle to report *)
181 :     fun normal_processing () = let
182 : blume 435 val _ = Say.vsay ["[scanning ", SrcPath.descr group, "]\n"]
183 :    
184 : blume 354 val context = SrcPath.sameDirContext group
185 : blume 294
186 : blume 345 fun work stream = let
187 :     val source =
188 : blume 354 S.newSource (SrcPath.osstring group,
189 :     1, stream, false, errcons)
190 : blume 345 val sourceMap = #sourceMap source
191 :     val _ = GroupReg.register groupreg (group, source)
192 : blume 297
193 : blume 345 (* We can hard-wire the source into this
194 :     * error function because the function is only for
195 :     * immediate use and doesn't get stored into persistent
196 :     * data structures. *)
197 :     fun error r m =
198 :     EM.error source r EM.COMPLAIN m EM.nullErrorBody
199 : blume 397 fun obsolete r =
200 : blume 433 if #get StdConfig.warn_obsolete () then
201 : blume 397 EM.error source r EM.WARN
202 :     "old-style operator (obsolete)" EM.nullErrorBody
203 :     else ()
204 : blume 294
205 : blume 345 (* recParse returns a group (not an option).
206 : blume 380 * This function is used to parse sub-groups.
207 : blume 345 * Errors are propagated by explicitly setting the
208 :     * "anyErrors" flag of the parent group. *)
209 : blume 367 fun recParse (p1, p2) curlib p = let
210 :     val gs' = (group, (source, p1, p2)) :: groupstack
211 : blume 345 val myErrorFlag = #anyErrors source
212 : blume 294 in
213 : blume 367 case mparse (p, gs', myErrorFlag, staball, curlib) of
214 : blume 345 NONE => (myErrorFlag := true;
215 :     CMSemant.emptyGroup group)
216 :     | SOME res => res
217 : blume 294 end
218 : blume 345 handle exn as IO.Io _ =>
219 :     (error (p1, p2) (General.exnMessage exn);
220 :     CMSemant.emptyGroup group)
221 :    
222 : blume 493 fun doMember ({ name, mkpath }, p1, p2, c) =
223 : blume 345 CMSemant.member (ginfo, recParse (p1, p2))
224 : blume 493 { name = name, mkpath = mkpath,
225 :     class = c, group = (group, (p1, p2)),
226 :     context = context }
227 : blume 345
228 :     (* Build the argument for the lexer; the lexer's local
229 :     * state is encapsulated here to make sure the parser
230 :     * is re-entrant. *)
231 :     val lexarg = let
232 :     (* local state *)
233 :     val depth = ref 0
234 :     val curstring = ref []
235 :     val startpos = ref 0
236 :     val instring = ref false
237 :     (* handling comments *)
238 :     fun enterC () = depth := !depth + 1
239 :     fun leaveC () = let
240 :     val d = !depth - 1
241 :     in
242 :     depth := d;
243 :     d = 0
244 :     end
245 :     (* handling strings *)
246 :     fun newS pos =
247 :     (instring := true;
248 :     curstring := [];
249 :     startpos := pos)
250 :     fun addS c = curstring := c :: !curstring
251 :     fun addSC (s, offs) =
252 :     addS (chr (ord (String.sub (s, 2)) - offs))
253 :     fun addSN (s, pos) = let
254 :     val ns = substring (s, 1, 3)
255 :     val n = Int.fromString ns
256 :     in
257 :     addS (chr (valOf n))
258 :     handle _ =>
259 :     error (pos, pos + size s)
260 :     ("illegal decimal char spec: " ^ ns)
261 :     end
262 :     fun getS (pos, tok) =
263 :     (instring := false;
264 :     tok (implode (rev (!curstring)), !startpos, pos))
265 :     (* handling EOF *)
266 :     fun handleEof () = let
267 :     val pos = SM.lastChange sourceMap
268 :     in
269 :     if !depth > 0 then
270 :     error (pos, pos)
271 :     "unexpected end of input in comment"
272 :     else if !instring then
273 :     error (pos, pos)
274 :     "unexpected end of input in string"
275 :     else ();
276 :     pos
277 :     end
278 :     (* handling line breaks *)
279 :     fun newline pos = SM.newline sourceMap pos
280 :     (* handling #line directives *)
281 :     fun sync (p, t) = let
282 :     fun sep c = c = #"#" orelse Char.isSpace c
283 :     fun cvt s = getOpt (Int.fromString s, 0)
284 :     fun r (line, col, file) = SM.resynch sourceMap
285 :     (p, { fileName = file,
286 :     line = line, column = col })
287 :     in
288 :     case String.tokens sep t of
289 :     [_, line] =>
290 :     r (cvt line, NONE, NONE)
291 :     | [_, line, file] =>
292 :     r (cvt line, NONE, SOME file)
293 :     | [_, line, col, file] =>
294 :     r (cvt line, SOME (cvt col), SOME file)
295 :     | _ => error (p, p + size t)
296 :     "illegal #line directive"
297 :     end
298 : blume 294 in
299 : blume 345 { enterC = enterC,
300 :     leaveC = leaveC,
301 :     newS = newS,
302 :     addS = addS,
303 :     addSC = addSC,
304 :     addSN = addSN,
305 :     getS = getS,
306 :     handleEof = handleEof,
307 :     newline = newline,
308 : blume 397 obsolete = obsolete,
309 : blume 345 error = error,
310 :     sync = sync}
311 : blume 294 end
312 : blume 345
313 :     fun inputc k = TextIO.input stream
314 :    
315 :     val lexer = CMLex.makeLexer inputc lexarg
316 :     val tokenStream = LrParser.Stream.streamify lexer
317 :     val (parseResult, _) =
318 :     CMParse.parse (lookAhead, tokenStream,
319 :     fn (s,p1,p2) => error (p1, p2) s,
320 : blume 397 (group, context, obsolete, error,
321 : blume 367 doMember, curlib, ginfo))
322 : blume 294 in
323 : blume 345 if !(#anyErrors source) then NONE
324 :     else SOME parseResult
325 : blume 294 end
326 : blume 364 fun openIt () = TextIO.openIn (SrcPath.osstring group)
327 : blume 345 val pro =
328 : blume 364 SafeIO.perform { openIt = openIt,
329 : blume 345 closeIt = TextIO.closeIn,
330 :     work = work,
331 : blume 459 cleanup = fn _ => () }
332 : blume 268 in
333 : blume 345 case pro of
334 :     NONE => NONE
335 :     | SOME pr =>
336 : blume 487 (SmlInfo.cleanGroup group;
337 :     if stabthis then stabilize pr
338 :     else SOME pr)
339 : blume 268 end
340 : blume 294 handle LrParser.ParseError => NONE
341 : blume 268 in
342 : blume 435 if hasCycle (group, groupstack) then NONE
343 :     else normal_processing ()
344 : blume 268 end
345 :     in
346 : blume 487 SmlInfo.newGeneration ();
347 : blume 367 case mparse (group, [], ref false, stabthis, NONE) of
348 : blume 303 NONE => NONE
349 : blume 487 | SOME g => SOME (g, ginfo)
350 : blume 274 end
351 : blume 268 end

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