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

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