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

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