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 /pgraph/releases/release-110.64/scan.sml
ViewVC logotype

Annotation of /pgraph/releases/release-110.64/scan.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1011 - (view) (download)
Original Path: sml/trunk/src/cm/pgraph/scan.sml

1 : blume 975 (* scan.sml
2 :     *
3 :     * (C) 2001 Lucent Technologies, Bell Labs
4 :     *
5 :     * Read the output of format.sml and reconstruct the original
6 :     * PortableGraph.graph.
7 :     *
8 :     * author: Matthias Blume (blume@research.bell-labs.com)
9 :     *)
10 :     structure ScanPortable : sig
11 :     exception ParseError of string
12 :     val input : TextIO.instream -> PortableGraph.graph
13 :     end = struct
14 :    
15 :     structure P = PortableGraph
16 :     structure S = TextIO.StreamIO
17 :    
18 :     exception ParseError of string
19 :    
20 :     fun input ins = let
21 : blume 977 val s = TextIO.getInstream ins
22 : blume 975
23 : blume 977 fun allof l s = foldl (fn (f, s) => f s) s l
24 : blume 975
25 : blume 977 fun skipWS s =
26 :     case S.input1 s of
27 :     NONE => s
28 :     | SOME (c, s') => if Char.isSpace c then skipWS s' else s
29 : blume 975
30 : blume 977 fun maybeident s = let
31 :     val s = skipWS s
32 :     val finish = String.implode o rev
33 :     fun loop (s, a) =
34 :     case S.input1 s of
35 :     NONE => SOME (finish a, s)
36 :     | SOME (c, s') => if Char.isAlphaNum c then loop (s', c :: a)
37 :     else SOME (finish a, s)
38 :     in
39 :     case S.input1 s of
40 :     NONE => NONE
41 :     | SOME (c, s') => if Char.isAlpha c then loop (s', [c])
42 :     else NONE
43 :     end
44 : blume 975
45 : blume 977 fun ident s =
46 :     case maybeident s of
47 :     NONE => raise ParseError "expected: identifier"
48 :     | SOME (i, s') => (i, s')
49 : blume 975
50 : blume 977 fun maybestring s = let
51 :     val s = skipWS s
52 :     fun eof () = raise ParseError "unexpected EOF in string"
53 :     fun loop (s, a) =
54 :     case S.input1 s of
55 :     NONE => eof ()
56 :     | SOME (#"\"", s') =>
57 :     (case String.fromString (String.implode (rev a)) of
58 :     SOME x => SOME (x, s')
59 :     | NONE => raise ParseError "illegal string syntax")
60 :     | SOME (#"\\", s') =>
61 :     (case S.input1 s' of
62 :     NONE => eof ()
63 :     | SOME (c, s'') => loop (s'', c :: #"\\" :: a))
64 :     | SOME (c, s') => loop (s', c :: a)
65 :     in
66 :     case S.input1 s of
67 :     SOME (#"\"", s') => loop (s', [])
68 :     | _ => raise ParseError "expected: string"
69 :     end
70 : blume 975
71 : blume 977 fun string s =
72 :     case maybestring s of
73 :     NONE => raise ParseError "expected: string"
74 :     | SOME (x, s') => (x, s')
75 : blume 975
76 : blume 977 fun expect c s = let
77 :     val s = skipWS s
78 :     fun notc what =
79 :     raise ParseError (concat ["expected: ", Char.toString c,
80 :     ", found: ", what])
81 :     in
82 :     case S.input1 s of
83 :     NONE => notc "EOF"
84 :     | SOME (c', s') => if c = c' then s' else notc (Char.toString c')
85 :     end
86 : blume 975
87 : blume 977 fun expectId i s = let
88 :     val (i', s') = ident s
89 :     in
90 :     if i = i' then s'
91 :     else raise ParseError (concat ["expected: ", i, ", found: ", i'])
92 :     end
93 : blume 975
94 : blume 977 fun varlist s = let
95 :     fun eof () = raise ParseError "unexpected EOF in varlist"
96 :     val s = allof [expect #"[", skipWS] s
97 :     fun rest s = let
98 :     val s = skipWS s
99 :     in
100 :     case S.input1 s of
101 :     NONE => eof ()
102 :     | SOME (#"]", s') => ([], s')
103 :     | SOME (#",", s') => let
104 :     val (h, s'') = ident s'
105 :     val (t, s''') = rest s''
106 :     in
107 :     (h :: t, s''')
108 :     end
109 :     | SOME (c, _) =>
110 :     raise ParseError
111 :     (concat ["expected , or ], found: ",
112 :     Char.toString c])
113 :     end
114 :     in
115 :     case S.input1 s of
116 :     NONE => eof ()
117 :     | SOME (#"]", s') => ([], s')
118 :     | SOME _ => let
119 :     val (h, s') = ident s
120 :     val (t, s'') = rest s'
121 :     in
122 :     (h :: t, s'')
123 :     end
124 :     end
125 : blume 975
126 : blume 977 fun def s =
127 :     case maybeident s of
128 :     SOME ("val", s) =>
129 :     let val s = allof [expect #"(", expectId "c", expect #","] s
130 :     val (lhs, s) = ident s
131 :     val s = allof [expect #")", expect #"="] s
132 :     val (f, s) = ident s
133 :     val s = expectId "c" s
134 :     fun def (rhs, s) =
135 :     SOME (P.DEF { lhs = lhs, rhs = rhs }, s)
136 :     fun comp native = let
137 :     val (r, s) = string s
138 :     val (e, s) = ident s
139 :     val (ss, s) = ident s
140 :     in
141 :     def (P.COMPILE { src = (r, native),
142 :     env = e, syms = ss },
143 :     s)
144 :     end
145 : blume 1011 fun sym ns = let
146 :     val (n, s) = string s
147 :     in
148 :     def (P.SYM (ns, n), s)
149 :     end
150 : blume 977 in
151 :     case f of
152 :     "syms" => let
153 :     val (l, s) = varlist s
154 :     in
155 :     def (P.SYMS l, s)
156 :     end
157 :     | "import" => let
158 :     val (l, s) = ident s
159 :     val (ss, s) = ident s
160 :     in
161 :     def (P.IMPORT { lib = l, syms = ss }, s)
162 :     end
163 :     | "compile" => comp false
164 :     | "ncompile" => comp true
165 :     | "merge" => let
166 :     val (l, s) = varlist s
167 :     in
168 :     def (P.MERGE l, s)
169 :     end
170 :     | "filter" => let
171 :     val (e, s) = ident s
172 :     val (ss, s) = ident s
173 :     in
174 :     def (P.FILTER { env = e, syms = ss }, s)
175 :     end
176 : blume 1011 | "sgn" => sym P.SGN
177 :     | "str" => sym P.STR
178 :     | "fct" => sym P.FCT
179 : blume 977 | x => raise ParseError ("unknown function: " ^ x)
180 :     end
181 :     | _ => NONE
182 : blume 975
183 : blume 977 fun deflist s = let
184 :     fun loop (s, a) =
185 :     case def s of
186 :     SOME (d, s') => loop (s', d :: a)
187 :     | NONE => (rev a, s)
188 :     in
189 :     loop (s, [])
190 :     end
191 : blume 975
192 : blume 977 fun graph s = let
193 :     val s = allof [#2 o S.inputLine, expectId "fn"] s
194 :     val (imports, s) = varlist s
195 : blume 1011 val s = allof [expect #"=", expect #">", expectId "let",
196 :     expectId "open", expectId "PGOps"] s
197 : blume 977 val (defs, s) = deflist s
198 :     val s = allof [expectId "in", expectId "export", expectId "c"] s
199 :     val (export, s) = ident s
200 :     (* gobble up remaining boilerplate... *)
201 :     val s = allof [expectId "end", expect #"|", expect #"_",
202 :     expect #"=", expect #">", expectId "raise",
203 :     expectId "Fail", #2 o string, expect #")",
204 :     #2 o S.inputLine]
205 :     s
206 :     in
207 :     TextIO.setInstream (ins, s);
208 :     P.GRAPH { imports = imports, defs = defs, export = export }
209 :     end
210 : blume 975 in
211 : blume 977 graph s
212 : blume 975 end
213 :     end

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