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

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