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 324 - (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 :     val loadStable =
84 :     Stabilize.loadStable (ginfo, getStable, pErrFlag)
85 :     in
86 :     case AbsPathMap.find (!gc, gpath) of
87 :     SOME (x as SOME _) => x
88 :     | SOME NONE => NONE
89 :     | NONE =>
90 :     (case loadStable gpath of
91 :     NONE => NONE
92 :     | x as SOME _ =>
93 : blume 311 (gc := AbsPathMap.insert (!gc, gpath, x);
94 :     x))
95 : blume 310 end
96 :    
97 :     fun 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 :     val context = AbsPath.relativeContext (AbsPath.dir group)
103 :     val filename = AbsPath.name group
104 : blume 310 val _ = Say.vsay ["[scanning ", filename, "]\n"]
105 : blume 294 val stream = TextIO.openIn filename
106 :     val source = S.newSource (filename, 1, stream, false, errcons)
107 :     val sourceMap = #sourceMap source
108 :     val _ = GroupReg.register groupreg (group, source)
109 :    
110 : blume 297 (* We can hard-wire the source into this
111 :     * error function because the function is only for
112 :     * immediate use and doesn't get stored into persistent
113 :     * data structures. *)
114 :     fun error r m =
115 :     EM.error source r EM.COMPLAIN m EM.nullErrorBody
116 :    
117 : blume 294 (* recParse returns a group (not an option).
118 :     * This function is used to parse aliases and sub-groups.
119 :     * Errors are propagated by explicitly setting the
120 :     * "anyErrors" flag of the parent group. *)
121 :     fun recParse (p1, p2) p = let
122 :     val groupstack' = (group, (source, p1, p2)) :: groupstack
123 : blume 310 val myErrorFlag = #anyErrors source
124 : blume 286 in
125 : blume 310 case mparse (p, groupstack', myErrorFlag, staball) of
126 :     NONE => (myErrorFlag := true;
127 : blume 294 CMSemant.emptyGroup group)
128 :     | SOME res => res
129 : blume 286 end
130 : blume 294 handle exn as IO.Io _ =>
131 :     (error (p1, p2) (General.exnMessage exn);
132 :     CMSemant.emptyGroup group)
133 :    
134 : blume 297 fun doMember (p, p1, p2, c) =
135 : blume 299 CMSemant.member (ginfo, recParse (p1, p2))
136 : blume 297 { sourcepath = p, class = c,
137 :     group = (group, (p1, p2)) }
138 : blume 294
139 : blume 301 (* Build the argument for the lexer; the lexer's local
140 :     * state is encapsulated here to make sure the parser
141 :     * is re-entrant. *)
142 : blume 294 val lexarg = let
143 :     (* local state *)
144 :     val depth = ref 0
145 :     val curstring = ref []
146 :     val startpos = ref 0
147 :     val instring = ref false
148 :     (* handling comments *)
149 :     fun enterC () = depth := !depth + 1
150 :     fun leaveC () = let
151 :     val d = !depth - 1
152 :     in
153 :     depth := d;
154 :     d = 0
155 :     end
156 :     (* handling strings *)
157 :     fun newS pos =
158 :     (instring := true; curstring := []; startpos := pos)
159 :     fun addS c = curstring := c :: !curstring
160 :     fun addSC (s, offs) =
161 :     addS (chr (ord (String.sub (s, 2)) - offs))
162 :     fun addSN (s, pos) = let
163 :     val ns = substring (s, 1, 3)
164 :     val n = Int.fromString ns
165 :     in
166 :     addS (chr (valOf n))
167 :     handle _ =>
168 :     error (pos, pos + size s)
169 :     ("illegal decimal char spec: " ^ ns)
170 :     end
171 :     fun getS (pos, tok) =
172 :     (instring := false;
173 :     tok (implode (rev (!curstring)), !startpos, pos))
174 :     (* handling EOF *)
175 :     fun handleEof () = let
176 :     val pos = SM.lastChange sourceMap
177 :     in
178 :     if !depth > 0 then
179 :     error (pos, pos)
180 :     "unexpected end of input in comment"
181 :     else if !instring then
182 :     error (pos, pos)
183 :     "unexpected end of input in string"
184 :     else ();
185 :     pos
186 :     end
187 :     (* handling line breaks *)
188 :     fun newline pos = SM.newline sourceMap pos
189 : blume 313 (* handling #line directives *)
190 :     fun sync (p, t) = let
191 :     fun sep c = c = #"#" orelse Char.isSpace c
192 :     fun cvt s = getOpt (Int.fromString s, 0)
193 :     fun r (line, col, file) = SM.resynch sourceMap
194 :     (p, { fileName = file, line = line, column = col })
195 :     in
196 :     case String.tokens sep t of
197 :     [_, line] =>
198 :     r (cvt line, NONE, NONE)
199 :     | [_, line, file] =>
200 :     r (cvt line, NONE, SOME file)
201 :     | [_, line, col, file] =>
202 :     r (cvt line, SOME (cvt col), SOME file)
203 :     | _ => error (p, p + size t)
204 :     "illegal #line directive"
205 :     end
206 : blume 294 in
207 :     { enterC = enterC,
208 :     leaveC = leaveC,
209 :     newS = newS,
210 :     addS = addS,
211 :     addSC = addSC,
212 :     addSN = addSN,
213 :     getS = getS,
214 :     handleEof = handleEof,
215 :     newline = newline,
216 : blume 313 error = error,
217 :     sync = sync}
218 : blume 294 end
219 :    
220 :     fun inputc k = TextIO.input stream
221 :    
222 :     val lexer = CMLex.makeLexer inputc lexarg
223 :     val tokenStream = LrParser.Stream.streamify lexer
224 :     val (parseResult, _) =
225 :     CMParse.parse (lookAhead, tokenStream,
226 :     fn (s,p1,p2) => error (p1, p2) s,
227 : blume 297 (group, context, error, recParse,
228 : blume 299 doMember, ginfo))
229 : blume 268 in
230 : blume 294 TextIO.closeIn stream;
231 :     if !(#anyErrors source) then NONE
232 : blume 310 else if stabthis then stabilize parseResult
233 : blume 294 else SOME parseResult
234 : blume 268 end
235 : blume 294 handle LrParser.ParseError => NONE
236 : blume 268 in
237 : blume 294 case findCycle (groupstack, []) of
238 :     h :: t => (report (h, t); NONE)
239 : blume 310 | [] =>
240 :     (case getStable group of
241 :     NONE => normal_processing ()
242 :     | SOME g => SOME g)
243 : blume 268 end
244 :     in
245 : blume 310 case mparse (group, [], ref false, stabthis) of
246 : blume 303 NONE => NONE
247 :     | SOME g =>
248 :     if CheckSharing.check (g, ginfo) then
249 :     (SmlInfo.forgetAllBut (Reachable.reachable g);
250 :     SOME (g, ginfo))
251 :     else NONE
252 : blume 274 end
253 : blume 268 end

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