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 487 - (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 :     SOME g)
135 :     end
136 : blume 310 in
137 : blume 435 case findCycle (stablestack, []) of
138 :     NONE => (case SrcPathMap.find (!sgc, gpath) of
139 :     SOME g => SOME g
140 :     | NONE => load ())
141 :     | SOME cyc => (report cyc; NONE)
142 : blume 310 end
143 : blume 435 in
144 :     case getStable [] group of
145 :     SOME g => SOME g
146 :     | NONE =>
147 :     (case SrcPathMap.find (!gc, group) of
148 :     SOME gopt => gopt
149 :     | NONE => let
150 :     val pres =
151 :     parse' (group, groupstack, pErrFlag,
152 :     stabthis, curlib)
153 :     in
154 :     gc := SrcPathMap.insert (!gc, group, pres);
155 :     pres
156 :     end)
157 :     end
158 : blume 310
159 : blume 435 and parse' (group, groupstack, pErrFlag, stabthis, curlib) = let
160 :    
161 : blume 340 (* We stabilize libraries only because a stable library will
162 :     * encompass the contents of its sub-groups
163 :     * (but not sub-libraries!). *)
164 : blume 348 fun stabilize (g as GG.GROUP { kind = GG.NOLIB, ... }) = SOME g
165 : blume 340 | stabilize g =
166 : blume 311 Stabilize.stabilize ginfo { group = g, anyerrors = pErrFlag }
167 : blume 310
168 : blume 294 (* normal processing -- used when there is no cycle to report *)
169 :     fun normal_processing () = let
170 : blume 435 val _ = Say.vsay ["[scanning ", SrcPath.descr group, "]\n"]
171 :    
172 : blume 354 val context = SrcPath.sameDirContext group
173 : blume 294
174 : blume 345 fun work stream = let
175 :     val source =
176 : blume 354 S.newSource (SrcPath.osstring group,
177 :     1, stream, false, errcons)
178 : blume 345 val sourceMap = #sourceMap source
179 :     val _ = GroupReg.register groupreg (group, source)
180 : blume 297
181 : blume 345 (* We can hard-wire the source into this
182 :     * error function because the function is only for
183 :     * immediate use and doesn't get stored into persistent
184 :     * data structures. *)
185 :     fun error r m =
186 :     EM.error source r EM.COMPLAIN m EM.nullErrorBody
187 : blume 397 fun obsolete r =
188 : blume 433 if #get StdConfig.warn_obsolete () then
189 : blume 397 EM.error source r EM.WARN
190 :     "old-style operator (obsolete)" EM.nullErrorBody
191 :     else ()
192 : blume 294
193 : blume 345 (* recParse returns a group (not an option).
194 : blume 380 * This function is used to parse sub-groups.
195 : blume 345 * Errors are propagated by explicitly setting the
196 :     * "anyErrors" flag of the parent group. *)
197 : blume 367 fun recParse (p1, p2) curlib p = let
198 :     val gs' = (group, (source, p1, p2)) :: groupstack
199 : blume 345 val myErrorFlag = #anyErrors source
200 : blume 294 in
201 : blume 367 case mparse (p, gs', myErrorFlag, staball, curlib) of
202 : blume 345 NONE => (myErrorFlag := true;
203 :     CMSemant.emptyGroup group)
204 :     | SOME res => res
205 : blume 294 end
206 : blume 345 handle exn as IO.Io _ =>
207 :     (error (p1, p2) (General.exnMessage exn);
208 :     CMSemant.emptyGroup group)
209 :    
210 :     fun doMember (p, p1, p2, c) =
211 :     CMSemant.member (ginfo, recParse (p1, p2))
212 :     { sourcepath = p, class = c,
213 :     group = (group, (p1, p2)) }
214 :    
215 :     (* Build the argument for the lexer; the lexer's local
216 :     * state is encapsulated here to make sure the parser
217 :     * is re-entrant. *)
218 :     val lexarg = let
219 :     (* local state *)
220 :     val depth = ref 0
221 :     val curstring = ref []
222 :     val startpos = ref 0
223 :     val instring = ref false
224 :     (* handling comments *)
225 :     fun enterC () = depth := !depth + 1
226 :     fun leaveC () = let
227 :     val d = !depth - 1
228 :     in
229 :     depth := d;
230 :     d = 0
231 :     end
232 :     (* handling strings *)
233 :     fun newS pos =
234 :     (instring := true;
235 :     curstring := [];
236 :     startpos := pos)
237 :     fun addS c = curstring := c :: !curstring
238 :     fun addSC (s, offs) =
239 :     addS (chr (ord (String.sub (s, 2)) - offs))
240 :     fun addSN (s, pos) = let
241 :     val ns = substring (s, 1, 3)
242 :     val n = Int.fromString ns
243 :     in
244 :     addS (chr (valOf n))
245 :     handle _ =>
246 :     error (pos, pos + size s)
247 :     ("illegal decimal char spec: " ^ ns)
248 :     end
249 :     fun getS (pos, tok) =
250 :     (instring := false;
251 :     tok (implode (rev (!curstring)), !startpos, pos))
252 :     (* handling EOF *)
253 :     fun handleEof () = let
254 :     val pos = SM.lastChange sourceMap
255 :     in
256 :     if !depth > 0 then
257 :     error (pos, pos)
258 :     "unexpected end of input in comment"
259 :     else if !instring then
260 :     error (pos, pos)
261 :     "unexpected end of input in string"
262 :     else ();
263 :     pos
264 :     end
265 :     (* handling line breaks *)
266 :     fun newline pos = SM.newline sourceMap pos
267 :     (* handling #line directives *)
268 :     fun sync (p, t) = let
269 :     fun sep c = c = #"#" orelse Char.isSpace c
270 :     fun cvt s = getOpt (Int.fromString s, 0)
271 :     fun r (line, col, file) = SM.resynch sourceMap
272 :     (p, { fileName = file,
273 :     line = line, column = col })
274 :     in
275 :     case String.tokens sep t of
276 :     [_, line] =>
277 :     r (cvt line, NONE, NONE)
278 :     | [_, line, file] =>
279 :     r (cvt line, NONE, SOME file)
280 :     | [_, line, col, file] =>
281 :     r (cvt line, SOME (cvt col), SOME file)
282 :     | _ => error (p, p + size t)
283 :     "illegal #line directive"
284 :     end
285 : blume 294 in
286 : blume 345 { enterC = enterC,
287 :     leaveC = leaveC,
288 :     newS = newS,
289 :     addS = addS,
290 :     addSC = addSC,
291 :     addSN = addSN,
292 :     getS = getS,
293 :     handleEof = handleEof,
294 :     newline = newline,
295 : blume 397 obsolete = obsolete,
296 : blume 345 error = error,
297 :     sync = sync}
298 : blume 294 end
299 : blume 345
300 :     fun inputc k = TextIO.input stream
301 :    
302 :     val lexer = CMLex.makeLexer inputc lexarg
303 :     val tokenStream = LrParser.Stream.streamify lexer
304 :     val (parseResult, _) =
305 :     CMParse.parse (lookAhead, tokenStream,
306 :     fn (s,p1,p2) => error (p1, p2) s,
307 : blume 397 (group, context, obsolete, error,
308 : blume 367 doMember, curlib, ginfo))
309 : blume 294 in
310 : blume 345 if !(#anyErrors source) then NONE
311 :     else SOME parseResult
312 : blume 294 end
313 : blume 364 fun openIt () = TextIO.openIn (SrcPath.osstring group)
314 : blume 345 val pro =
315 : blume 364 SafeIO.perform { openIt = openIt,
316 : blume 345 closeIt = TextIO.closeIn,
317 :     work = work,
318 : blume 459 cleanup = fn _ => () }
319 : blume 268 in
320 : blume 345 case pro of
321 :     NONE => NONE
322 :     | SOME pr =>
323 : blume 487 (SmlInfo.cleanGroup group;
324 :     if stabthis then stabilize pr
325 :     else SOME pr)
326 : blume 268 end
327 : blume 294 handle LrParser.ParseError => NONE
328 : blume 268 in
329 : blume 435 if hasCycle (group, groupstack) then NONE
330 :     else normal_processing ()
331 : blume 268 end
332 :     in
333 : blume 487 SmlInfo.newGeneration ();
334 : blume 367 case mparse (group, [], ref false, stabthis, NONE) of
335 : blume 303 NONE => NONE
336 : blume 487 | SOME g => SOME (g, ginfo)
337 : blume 274 end
338 : blume 268 end

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