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

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