-
Notifications
You must be signed in to change notification settings - Fork 14
/
command-line.sml
325 lines (232 loc) · 8 KB
/
command-line.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
structure ParseCommandLine :> PARSE_COMMAND_LINE =
struct
type 'a parser = string list -> 'a * string list
exception Error of string
exception Backtrack of int
fun return x l = (x, l)
fun string l =
(case l of
nil => raise (Backtrack 0)
| str :: rest => (str, rest))
fun reject l = raise (Backtrack 0)
fun error p l =
let
val (err, _) = p l
in
raise (Error err)
end
fun seq p q l1 =
let
val (x, l2) = p l1
val (y, l3) = q l2
in
((x, y), l3)
end
fun seq3 p q r l1 =
let
val (x, l2) = p l1
val (y, l3) = q l2
val (z, l4) = r l3
in
((x, y, z), l4)
end
fun seq4 p q r s l1 =
let
val (x, l2) = p l1
val (y, l3) = q l2
val (z, l4) = r l3
val (w, l5) = s l4
in
((x, y, z, w), l5)
end
fun or ps l =
let
fun loop ps =
(case ps of
nil => raise (Backtrack 0)
| p :: rest =>
(p l
handle (Backtrack i) =>
if i = 0 then
loop rest
else
raise (Backtrack (i-1))))
in
loop ps
end
fun map f p l =
let
val (x, l') = p l
in
(f x, l')
end
fun fold f z p l =
(let
val (x, l') = p l
in
fold f (f (x, z)) p l'
end
handle (Backtrack i) =>
if i = 0 then
(z, l)
else
raise (Backtrack (i-1)))
fun test f p l =
(case l of
nil => raise (Backtrack 0)
| str :: rest =>
if f str then
p l
else
raise (Backtrack 0))
fun eof l =
(case l of
nil => ((), nil)
| _ :: _ => raise (Backtrack 0))
fun cut p l =
(p l
handle (Backtrack i) => raise (Backtrack (i+1)))
fun parse p l =
((case p l of
(x, nil) => x
| (_, str :: _) => raise (Error ("unexpected argument " ^ str)))
handle (Backtrack _) => raise (Error "cannot parse command-line arguments"))
fun backtrack () = raise (Backtrack 0)
structure D = RedBlackDict (structure Key = IntOrdered)
type log = exn D.dict
type ('a, 'b) key = ('a -> log -> log) * (log -> 'b)
datatype ('a, 'b) machine =
Machine of { write : 'a -> ('a, 'b) machine,
read : log -> 'b }
val blank = D.empty
fun write (wr, _) = wr
fun read (_, rd) = rd
fun mapKeyIn f (wr, rd) = ((fn x => wr (f x)), rd)
fun mapKeyOut f (wr, rd) = (wr, (fn log => f (rd log)))
val cur = ref 0
fun ('a, 'b) key (m : ('a, 'b) machine) =
let
val i = !cur
val () = cur := i + 1
exception E of ('a, 'b) machine
fun get log =
(case D.find log i of
NONE => m
| SOME (E m') => m'
| SOME _ =>
raise (Fail "impossible"))
fun wr x log =
let
val Machine {write, ...} = get log
in
D.insert log i (E (write x))
end
fun rd log =
let
val Machine {read, ...} = get log
in
read log
end
in
(wr, rd)
end
fun stringToBool str =
(case str of
"true" => true
| "false" => false
| "yes" => true
| "no" => false
| "on" => true
| "off" => false
| _ => raise (Backtrack 0))
fun stringToInt str =
(case FromString.toIntM str of
NONE => raise (Backtrack 0)
| SOME x => x)
fun stringToNonneg str =
let val i = stringToInt str
in
if i < 0 then
raise (Backtrack 0)
else
i
end
fun expects str1 str2 = String.concat [str2, " expects ", str1]
fun unexpected str = "unexpected argument " ^ str
fun unrecognized str = "unrecognized option " ^ str
fun eq (x : string) y = x = y
fun forget p = map (fn _ => ()) p
fun match f = test f (forget string)
fun seqfst p q = map (fn (x, y) => x) (seq p q)
fun seqsnd p q = map (fn (x, y) => y) (seq p q)
val seqf = seqsnd
fun seql ps = List.foldl (fn (p, q) => map (op ::) (seq p q)) (return []) ps
fun trap err p = or [p, error (return err)]
fun mapCompose f p = map (fn g => fn x => f (g x)) p
fun foldCompose p = fold (op o) (fn x => x) p
fun mapw k p = map (write k) p
val bool = map stringToBool string
val int = map stringToInt string
val nonneg = map stringToNonneg string
val nohyph = or [test (fn str => String.size str > 0 andalso String.sub (str, 0) = #"-") (error (map unrecognized string)), string]
fun exactly (str : string) = match (fn str' => str = str')
fun unitOpt str k v = map (fn () => write k v) (exactly str)
fun stringOpt str k = seqf (exactly str) (trap (expects "an argument" str) (map (write k) nohyph))
fun boolOpt str k = seqf (exactly str) (trap (expects "a boolean argument" str) (map (write k) bool))
fun intOpt str k = seqf (exactly str) (trap (expects "a numeric argument" str) (map (write k) int))
fun nonnegOpt str k = seqf (exactly str) (trap (expects "a nonnegative argument" str) (map (write k) nonneg))
fun default x =
Machine { write = default, read = (fn _ => x) }
fun default' f =
Machine { write = default, read = f }
fun optionWrite x =
Machine { write = optionWrite, read = (fn _ => SOME x) }
val option =
Machine { write = optionWrite, read = (fn _ => NONE) }
fun listWrite l x =
Machine { write = listWrite (x :: l),
read = (fn _ => rev (x :: l)) }
val list =
Machine { write = (fn x => listWrite [] x), read = (fn _ => []) }
fun listDefault x =
Machine { write = (fn x => listWrite [] x), read = (fn _ => x) }
fun listDefault' f =
Machine { write = (fn x => listWrite [] x), read = f }
fun revappend l1 l2 = List.foldl (op ::) l2 l1
fun appendWrite l1 l2 =
Machine { write = appendWrite (revappend l2 l1),
read = (fn _ => rev (revappend l2 l1)) }
val append =
Machine { write = (fn l => appendWrite [] l), read = (fn _ => []) }
fun appendDefault x =
Machine { write = (fn l => appendWrite [] l), read = (fn _ => x) }
fun appendDefault' f =
Machine { write = (fn l => appendWrite [] l), read = f }
fun mapIn f (Machine { write, read }) =
let
fun wt x = mapIn f (write (f x))
in
Machine {write=wt, read=read}
end
fun mapOut f (Machine { write, read }) =
let
fun wt x = mapOut f (write x)
fun rd log = f (read log)
in
Machine {write=wt, read=rd}
end
type 'a id_key = ('a, 'a) key
type 'a option_key = ('a, 'a option) key
type 'a list_key = ('a, 'a list) key
fun limit i err (Machine {write, read}) =
let
fun wt x =
if i <= 0 then
raise (Error err)
else
limit (i-1) err (write x)
in
Machine {write=wt, read=read}
end
fun once err = limit 1 err
end