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 294 - (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 268 signature CMPARSE = sig
9 : blume 286 val parse : Primitive.configuration -> AbsPath.t -> CMSemant.group option
10 : blume 268 end
11 :    
12 :     structure CMParse :> CMPARSE = struct
13 :    
14 :     val lookAhead = 30
15 :    
16 :     structure S = GenericVC.Source
17 :     structure EM = GenericVC.ErrorMsg
18 :     structure SM = GenericVC.SourceMap
19 :     structure P = GenericVC.Control.Print
20 :    
21 :     structure CMLrVals = CMLrValsFun (structure Token = LrParser.Token)
22 :     structure CMLex = CMLexFun (structure Tokens = CMLrVals.Tokens)
23 :     structure CMParse =
24 :     JoinWithArg (structure ParserData = CMLrVals.ParserData
25 :     structure Lex = CMLex
26 :     structure LrParser = LrParser)
27 :    
28 : blume 281 (* The error function must look for the source using the GroupReg
29 :     * group register because the source must not be hard-wired into it
30 :     * (via closure creation). The reason for this is that the error
31 :     * function will get cached in SmlInfo.info but the source will change
32 :     * when one re-runs the analysis. *)
33 :    
34 : blume 286 fun parse primconf group = let
35 : blume 270
36 : blume 286 val groupreg = GroupReg.new ()
37 :     val fnpolicy = FilenamePolicy.default
38 :     val params = { groupreg = groupreg,
39 :     fnpolicy = fnpolicy,
40 :     primconf = primconf }
41 : blume 268
42 : blume 294 val gc = ref AbsPathMap.empty (* the "group cache" *)
43 : blume 273
44 : blume 294 fun mparse (group, groupstack) =
45 :     case AbsPathMap.find (!gc, group) of
46 :     SOME g => g
47 :     | NONE => let
48 :     val g = parse' (group, groupstack)
49 :     in
50 :     gc := AbsPathMap.insert (!gc, group, g);
51 :     g
52 :     end
53 : blume 273
54 : blume 294 and parse' (group, groupstack) = 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 :     PrettyPrint.add_string pps (AbsPath.spec g0);
69 :     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 :     AbsPath.spec group)
80 :     pphist
81 : blume 273 end
82 :    
83 : blume 294 (* normal processing -- used when there is no cycle to report *)
84 :     fun normal_processing () = let
85 :     val currentDir = AbsPath.dir group
86 :     val context = AbsPath.relativeContext (AbsPath.dir group)
87 :     val filename = AbsPath.name group
88 :     val _ = Say.vsay (concat ["[scanning ", filename, "]\n"])
89 :     val stream = TextIO.openIn filename
90 :     val errcons = { linewidth = !P.linewidth,
91 :     flush = P.flush,
92 :     consumer = P.say }
93 :     val source = S.newSource (filename, 1, stream, false, errcons)
94 :     val sourceMap = #sourceMap source
95 :     val _ = GroupReg.register groupreg (group, source)
96 :     fun error' region sev m b = let
97 :     val src = GroupReg.lookup groupreg group
98 : blume 286 in
99 : blume 294 EM.error src region sev m b
100 : blume 286 end
101 : blume 294 fun error region m =
102 :     error' region EM.COMPLAIN m EM.nullErrorBody
103 :    
104 :     (* recParse returns a group (not an option).
105 :     * This function is used to parse aliases and sub-groups.
106 :     * Errors are propagated by explicitly setting the
107 :     * "anyErrors" flag of the parent group. *)
108 :     fun recParse (p1, p2) p = let
109 :     val groupstack' = (group, (source, p1, p2)) :: groupstack
110 : blume 286 in
111 : blume 294 case mparse (p, groupstack') of
112 :     NONE => (#anyErrors source := true;
113 :     CMSemant.emptyGroup group)
114 :     | SOME res => res
115 : blume 286 end
116 : blume 294 handle exn as IO.Io _ =>
117 :     (error (p1, p2) (General.exnMessage exn);
118 :     CMSemant.emptyGroup group)
119 :    
120 :     fun doMember (p, p1, p2, c, e) =
121 :     CMSemant.member (params, recParse (p1, p2))
122 :     { sourcepath = p, group = group,
123 :     class = c, error = e }
124 :    
125 :     val lexarg = let
126 :     (* local state *)
127 :     val depth = ref 0
128 :     val curstring = ref []
129 :     val startpos = ref 0
130 :     val instring = ref false
131 :     (* handling comments *)
132 :     fun enterC () = depth := !depth + 1
133 :     fun leaveC () = let
134 :     val d = !depth - 1
135 :     in
136 :     depth := d;
137 :     d = 0
138 :     end
139 :     (* handling strings *)
140 :     fun newS pos =
141 :     (instring := true; curstring := []; startpos := pos)
142 :     fun addS c = curstring := c :: !curstring
143 :     fun addSC (s, offs) =
144 :     addS (chr (ord (String.sub (s, 2)) - offs))
145 :     fun addSN (s, pos) = let
146 :     val ns = substring (s, 1, 3)
147 :     val n = Int.fromString ns
148 :     in
149 :     addS (chr (valOf n))
150 :     handle _ =>
151 :     error (pos, pos + size s)
152 :     ("illegal decimal char spec: " ^ ns)
153 :     end
154 :     fun getS (pos, tok) =
155 :     (instring := false;
156 :     tok (implode (rev (!curstring)), !startpos, pos))
157 :     (* handling EOF *)
158 :     fun handleEof () = let
159 :     val pos = SM.lastChange sourceMap
160 :     in
161 :     if !depth > 0 then
162 :     error (pos, pos)
163 :     "unexpected end of input in comment"
164 :     else if !instring then
165 :     error (pos, pos)
166 :     "unexpected end of input in string"
167 :     else ();
168 :     pos
169 :     end
170 :     (* handling line breaks *)
171 :     fun newline pos = SM.newline sourceMap pos
172 :     in
173 :     { enterC = enterC,
174 :     leaveC = leaveC,
175 :     newS = newS,
176 :     addS = addS,
177 :     addSC = addSC,
178 :     addSN = addSN,
179 :     getS = getS,
180 :     handleEof = handleEof,
181 :     newline = newline,
182 :     error = error }
183 :     end
184 :    
185 :     fun inputc k = TextIO.input stream
186 :    
187 :     val lexer = CMLex.makeLexer inputc lexarg
188 :     val tokenStream = LrParser.Stream.streamify lexer
189 :     val (parseResult, _) =
190 :     CMParse.parse (lookAhead, tokenStream,
191 :     fn (s,p1,p2) => error (p1, p2) s,
192 :     (group, context, error', error, recParse,
193 :     doMember))
194 : blume 268 in
195 : blume 294 TextIO.closeIn stream;
196 :     if !(#anyErrors source) then NONE
197 :     else SOME parseResult
198 : blume 268 end
199 : blume 294 handle LrParser.ParseError => NONE
200 : blume 268 in
201 : blume 294 case findCycle (groupstack, []) of
202 :     h :: t => (report (h, t); NONE)
203 :     | [] => normal_processing ()
204 : blume 268 end
205 : blume 294
206 : blume 268 in
207 : blume 294 mparse (group, [])
208 : blume 274 end
209 : blume 268 end

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