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

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