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

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