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

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