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

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