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

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