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 977 - (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 :     in
146 :     case f of
147 :     "syms" => let
148 :     val (l, s) = varlist s
149 :     in
150 :     def (P.SYMS l, s)
151 :     end
152 :     | "import" => let
153 :     val (l, s) = ident s
154 :     val (ss, s) = ident s
155 :     in
156 :     def (P.IMPORT { lib = l, syms = ss }, s)
157 :     end
158 :     | "compile" => comp false
159 :     | "ncompile" => comp true
160 :     | "merge" => let
161 :     val (l, s) = varlist s
162 :     in
163 :     def (P.MERGE l, s)
164 :     end
165 :     | "filter" => let
166 :     val (e, s) = ident s
167 :     val (ss, s) = ident s
168 :     in
169 :     def (P.FILTER { env = e, syms = ss }, s)
170 :     end
171 :     | "sym" => let
172 :     val (ns, s) = string s
173 :     val (n, s) = string s
174 :     in
175 :     def (P.SYM (ns, n), s)
176 :     end
177 :     | x => raise ParseError ("unknown function: " ^ x)
178 :     end
179 :     | _ => NONE
180 : blume 975
181 : blume 977 fun deflist s = let
182 :     fun loop (s, a) =
183 :     case def s of
184 :     SOME (d, s') => loop (s', d :: a)
185 :     | NONE => (rev a, s)
186 :     in
187 :     loop (s, [])
188 :     end
189 : blume 975
190 : blume 977 fun graph s = let
191 :     val s = allof [#2 o S.inputLine, expectId "fn"] s
192 :     val (imports, s) = varlist s
193 :     val s = allof [expect #"=", expect #">", expectId "let"] s
194 :     val (defs, s) = deflist s
195 :     val s = allof [expectId "in", expectId "export", expectId "c"] s
196 :     val (export, s) = ident s
197 :     (* gobble up remaining boilerplate... *)
198 :     val s = allof [expectId "end", expect #"|", expect #"_",
199 :     expect #"=", expect #">", expectId "raise",
200 :     expectId "Fail", #2 o string, expect #")",
201 :     #2 o S.inputLine]
202 :     s
203 :     in
204 :     TextIO.setInstream (ins, s);
205 :     P.GRAPH { imports = imports, defs = defs, export = export }
206 :     end
207 : blume 975 in
208 : blume 977 graph s
209 : blume 975 end
210 :     end

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