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 286 - (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 286 fun parse' (group, groupstack) = let
43 : blume 273
44 : blume 286 val currentDir = AbsPath.dir group
45 :     val context = AbsPath.relativeContext (AbsPath.dir group)
46 :     val filename = AbsPath.name group
47 :     val _ = Say.vsay (concat ["[scanning ", filename, "]\n"])
48 :     val stream = TextIO.openIn filename
49 :     val errcons =
50 :     { linewidth = !P.linewidth, flush = P.flush, consumer = P.say }
51 :     val source = S.newSource (filename, 1, stream, false, errcons)
52 :     val sourceMap = #sourceMap source
53 :     val _ = GroupReg.register groupreg (group, source)
54 :     fun error' region m b = let
55 :     val src = GroupReg.lookup groupreg group
56 :     in
57 :     EM.error src region EM.COMPLAIN m b
58 :     end
59 :     fun error region m = error' region m EM.nullErrorBody
60 : blume 273
61 : blume 286 (* recParse returns a group (not an option)
62 :     * and re-raises LrParser.ParseError.
63 :     * This exception will be handled by the surrounding
64 :     * call to parse.
65 :     * This function is used to parse aliases and sub-groups. *)
66 :     fun recParse (p1, p2) p =
67 :     (case parse' (p, (group, (source, p1, p2)) :: groupstack) of
68 :     NONE => (#anyErrors source := true;
69 :     CMSemant.emptyGroup group)
70 :     | SOME res => res)
71 :     handle exn as IO.Io _ =>
72 :     (error (p1, p2) (General.exnMessage exn);
73 :     CMSemant.emptyGroup group)
74 :    
75 :     fun doMember (p, p1, p2, c, e) =
76 :     CMSemant.member (params, recParse (p1, p2))
77 :     { sourcepath = p,
78 :     group = group,
79 :     class = c,
80 :     error = e }
81 :    
82 :     (* checking for cycles among groups and printing them nicely *)
83 :     val _ = let
84 :     fun findCycle ([], _) = []
85 :     | findCycle ((h as (g, (s, p1, p2))) :: t, cyc) =
86 :     if AbsPath.compare (g, group) = EQUAL then rev (h :: cyc)
87 :     else findCycle (t, h :: cyc)
88 :     fun report ((g, (s, p1, p2)), hist) = let
89 :     fun pphist pps = let
90 :     fun loop (_, []) = ()
91 :     | loop (g0, (g, (s, p1, p2)) :: t) = let
92 :     val s = EM.matchErrorString s (p1, p2)
93 :     in
94 :     PrettyPrint.add_string pps s;
95 :     PrettyPrint.add_string pps ": importing ";
96 :     PrettyPrint.add_string pps (AbsPath.spec g0);
97 :     PrettyPrint.add_newline pps;
98 :     loop (g, t)
99 :     end
100 :     in
101 :     PrettyPrint.add_newline pps;
102 :     loop (g, hist)
103 :     end
104 : blume 273 in
105 : blume 286 EM.error s (p1, p2) EM.COMPLAIN
106 :     ("group hierarchy forms a cycle with " ^
107 :     AbsPath.spec group)
108 :     pphist;
109 :     raise LrParser.ParseError
110 : blume 273 end
111 :     in
112 : blume 286 case findCycle (groupstack, []) of
113 :     [] => ()
114 :     | h :: t => report (h, t)
115 : blume 273 end
116 :    
117 : blume 286 val lexarg = let
118 :     (* local state *)
119 :     val depth = ref 0
120 :     val curstring = ref []
121 :     val startpos = ref 0
122 :     val instring = ref NONE
123 :     (* handling comments *)
124 :     fun enterC () = depth := !depth + 1
125 :     fun leaveC () = let val d = !depth - 1 in depth := d; d = 0 end
126 :     (* handling strings *)
127 :     fun newS (pos, kind) =
128 :     (instring := SOME kind;
129 :     curstring := [];
130 :     startpos := pos)
131 :     fun addS c = curstring := c :: !curstring
132 :     fun addSC (s, offs) =
133 :     addS (chr (ord (String.sub (s, 2)) - offs))
134 :     fun addSN (s, pos) = let
135 :     val ns = substring (s, 1, 3)
136 :     val n = Int.fromString ns
137 :     in
138 :     addS (chr (valOf n))
139 :     handle _ =>
140 :     error (pos, pos + size s)
141 :     ("illegal decimal char spec: " ^ ns)
142 :     end
143 :     fun getS (pos, tok) =
144 :     (instring := NONE;
145 :     tok (implode (rev (!curstring)), !startpos, pos))
146 :     (* handling EOF *)
147 :     fun handleEof () = let
148 :     val pos = SM.lastChange sourceMap
149 :     in
150 :     if !depth > 0 then
151 :     error (pos, pos) "unexpected end of input in comment"
152 :     else if isSome (!instring) then
153 :     error (pos, pos)
154 :     ("unexpected end of input in " ^ valOf (!instring))
155 :     else ();
156 :     pos
157 :     end
158 :     (* handling line breaks *)
159 :     fun newline pos = SM.newline sourceMap pos
160 : blume 268 in
161 : blume 286 { enterC = enterC,
162 :     leaveC = leaveC,
163 :     newS = newS,
164 :     addS = addS,
165 :     addSC = addSC,
166 :     addSN = addSN,
167 :     getS = getS,
168 :     handleEof = handleEof,
169 :     newline = newline,
170 :     error = error }
171 : blume 268 end
172 : blume 286
173 :     fun inputc k = TextIO.input stream
174 :    
175 :     val lexer = CMLex.makeLexer inputc lexarg
176 :     val tokenStream = LrParser.Stream.streamify lexer
177 :     val (parseResult, _) =
178 :     CMParse.parse (lookAhead, tokenStream,
179 :     fn (s,p1,p2) => error (p1, p2) s,
180 :     (group, context, error', error, recParse,
181 :     doMember))
182 : blume 268 in
183 : blume 286 TextIO.closeIn stream;
184 :     if !(#anyErrors source) then NONE
185 :     else SOME parseResult
186 : blume 268 end
187 : blume 286 handle LrParser.ParseError => NONE
188 : blume 268 in
189 : blume 286 parse' (group, [])
190 : blume 274 end
191 : blume 268 end

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