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

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