Source file mpp_charstream.ml
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
let debug = ref false
type charstream = {
take : unit -> char option;
push : char -> unit;
insert : charstream -> unit;
where : unit -> location;
}
and filename = string
and location = filename*line*column
and line = int
and column = int
type t = charstream
let parse_error : ?start:location -> ?msg:string -> location -> unit =
fun ?start ?msg location ->
let f, l, c = location in
match start with
| None ->
begin match msg with
| None ->
Printf.eprintf
"Error in %s:%d:%d.\n%!"
f l c
| Some m ->
Printf.eprintf
"Error: %s\nIn %s:%d:%d.\n%!"
m f l c
end
| Some(filename,line,column) ->
begin match msg with
| None ->
if l <> 0 && c <> 0 then
Printf.eprintf
"Error from %s:%d:%d to %s:%d:%d.\n%!"
filename line column f l c
else
Printf.eprintf
"Error from %s:%d:%d to end of file.\n%!"
filename line column
| Some m ->
if l <> 0 && c <> 0 then
Printf.eprintf
"Error: %s\nFrom %s:%d:%d to %s:%d:%d.\n%!"
m filename line column f l c
else
Printf.eprintf
"Error: %s\nFrom %s:%d:%d to end of file.\n%!"
m filename line column
end
let output_charstream out c =
let rec loop () =
match c.take () with
| None -> ()
| Some c -> output_char out c; loop()
in loop()
module Mpp_charset = Mpp_charset
let charstream_take_n n charstream =
let b = Buffer.create n in
for _ = 1 to n do
match charstream.take() with
| None -> ()
| Some c -> Buffer.add_char b c
done;
Buffer.contents b
let charstream_peek ?(n=1) charstream =
let limit = ref n in
let b = Buffer.create n in
for i = 1 to n do
match charstream.take() with
| Some c -> Buffer.add_char b c
| None -> limit := min !limit (pred i)
done;
let res = Buffer.contents b in
for i = !limit - 1 downto 0 do
let x = res.[i] in
charstream.push x
done;
res
let null_charstream =
let c = ref [] in
let cs = ref [] in
let insert e = cs := e :: !cs in
let rec take () = match !cs with
| x::tl ->
begin match x.take() with
| Some _ as r -> r
| None -> cs := tl; take ()
end
| [] ->
match !c with
| [] -> None
| e :: tl -> c := tl; Some e
in
let push e = match !cs with
| [] -> c := e :: !c
| x :: _ -> x.push e
in
let where () = match !cs with
| [] -> "", -1, -1
| x :: _ -> x.where ()
in
{ take = take;
push = push;
insert = insert;
where = where;
}
let charstream_of_inchannel filename ?(line=1) ?(column=0) inchan =
let buffer : char list ref = ref [] in
let line = ref line in
let column = ref [column] in
let incr_column () =
(match !column with
| [] -> assert false;
| x :: tl -> column := (x+1) :: tl
)
in
let where () =
match !column with
| [] -> filename, !line, 0
| c::_ -> filename, !line, c
in
let take () =
match !buffer with
| [] ->
begin
try match input_char inchan with
| '\n' | '\r' as c ->
incr line;
column := 0 :: !column;
Some c
| c ->
incr_column();
Some c
with End_of_file -> None
end
| c::tl ->
match c with
| '\n' | '\r' ->
incr line;
column := 0 :: !column;
buffer := tl;
Some c
| c ->
incr_column();
buffer := tl;
Some c
in
let push c =
buffer := c :: !buffer
in
let csl = ref [{take;push;where;insert=fun _ -> assert false}] in
let insert cs =
csl := cs :: !csl
in
let where () =
match !csl with
| [] ->
filename, !line, (match !column with x::_ -> x| _ -> 0)
| e::_ -> e.where()
in
let rec take () =
match !csl with
| [] -> None
| e::tl ->
match e.take() with
| None -> csl := tl; take()
| Some _ as res -> res
in
let rec push c =
match !csl with
| [] ->
csl := [null_charstream];
push c
| e::_ ->
begin
match c with
| '\n' | '\r' ->
begin
decr line;
match !column with
| [] ->
column := [0]
| _::tl -> column := tl
end
| _ ->
match !column with
| cl::cls -> column := pred cl::cls
| [] -> column := [-1]
end;
e.push c
in
{ take = take;
push = push;
insert = insert;
where = where;
}
let charstream_of_string ?(location:location=("<anon-string>",0,0)) (s:string) : charstream =
let i = ref 0 in
let location = ref location in
let t = ref
(fun() ->
if !i >= String.length s then None
else
begin match s.[!i] with
| '\n' as c ->
location :=
(match !location with
| fn, line, _ -> fn, line+1, 0);
incr i;
Some c
| c ->
location :=
(match !location with
| fn, line, col -> fn, line, col+1);
incr i;
Some c
end)
in
let streams = ref [] in
let stack = ref [] in
let push c = match !streams with
| [] -> stack := c :: !stack
| s::_ -> s.push c
in
let rec take () = match !streams with
| s::tl ->
begin match s.take() with
| None -> streams := tl; take()
| Some _ as r -> r
end
| [] ->
begin match !stack with
| [] ->
(match !t() with None -> t := (fun () -> None); None | r -> r)
| c::tl ->
stack := tl;
Some c
end
in
let insert cs = streams := cs :: !streams in
let where () = match !streams with
| [] -> !location
| cs::_ -> cs.where()
in
{ take = take;
push = push;
insert = insert;
where = where;
}
let match_token token charstream =
if !debug then
(let _filename, _line, _col = charstream.where () in
Printf.eprintf "<token=%s@%d-%d----'%s'>\n%!" token _line _col (String.escaped(charstream_peek ~n:20 charstream)));
let res =
token <> ""
&&
let rec loop i taken =
if i >= String.length token then
true, []
else
match charstream.take() with
| None ->
false, taken
| Some c ->
if token.[i] = c then
loop (succ i) (c::taken)
else
false, (c::taken)
in match loop 0 [] with
| true, _ ->
true
| false, taken ->
List.iter charstream.push taken;
false
in
res
let rec eat (cs:Mpp_charset.t) charstream =
match charstream.take() with
| None ->
()
| Some c ->
if Mpp_charset.mem c cs then
eat cs charstream
else
charstream.push c
let [@warning "-27"] read_until ?(caller="") ?(failsafe=false) c charstream : string =
if !debug then Printf.eprintf "read_until '%s'\n%!" (Char.escaped c);
let b = Buffer.create 128 in
let rec loop () =
match charstream.take() with
| Some z ->
let () =
if !debug then
Printf.eprintf "Peek<%s>\n%!"
(String.escaped (charstream_peek ~n:20 charstream))
in
if c = z then
begin
charstream.push z;
Buffer.contents b
end
else
begin
Buffer.add_char b z;
loop ()
end
| None ->
if failsafe then
Buffer.contents b
else
begin
parse_error
~msg:(Printf.sprintf "Couldn't read far enough. I could read <%s>." (Buffer.contents b))
(charstream.where());
exit 1
end
in loop ()
let read_until_one_of ?(caller="") ?(failsafe=false) ?(push_back=false) (cs:Mpp_charset.t) ?(exclude=Mpp_charset.empty) ?(expect:string option) charstream =
if !debug then Printf.eprintf "read_until_one_of [%s]\n%!" (Mpp_charset.fold (fun c r -> Printf.sprintf "%s%s" r (Char.escaped c)) cs "");
let b = Buffer.create 128 in
let rec loop () =
match charstream.take() with
| Some z ->
if Mpp_charset.mem z cs then
begin
if push_back then charstream.push z;
Buffer.contents b
end
else if Mpp_charset.mem z exclude then
begin
match expect with
| None ->
parse_error ~msg:(Printf.sprintf "Forbidden character: <%s>" (Char.escaped z)) (charstream.where());
exit 1
| Some m ->
parse_error ~msg:(Printf.sprintf "Forbidden character: <%s>. Expected: <%s>" (Char.escaped z) m) (charstream.where());
exit 1
end
else
begin
Buffer.add_char b z;
loop ()
end
| None ->
if failsafe then
Buffer.contents b
else
begin
parse_error
~msg:(Printf.sprintf "Couldn't read far enough. I could read <%s>. I was looking for [%s].%s" (Buffer.contents b) (Mpp_charset.to_escaped_string cs) (if caller <> "" then Printf.sprintf " Caller: %s." caller else ""))
(charstream.where());
exit 1
end
in loop ()
let parse_int charstream =
if !debug then Printf.eprintf "parse_int\n%!";
let res = Buffer.create 42 in
let rec loop () =
match charstream.take() with
| Some ('0' .. '9' as c) -> Buffer.add_char res c; loop()
| Some '_' -> loop()
| Some c -> charstream.push c
| None -> ()
in
loop ();
int_of_string (Buffer.contents res)
let read_until_word ?(failsafe=false) ?(success=ref true) charstream word =
assert(word<>"");
let start_location = charstream.where() in
let res = Buffer.create (String.length word * 8) in
let buf = Buffer.create (String.length word) in
let rec loop i =
if i >= String.length word then
begin
success := true;
Buffer.contents res
end
else
match charstream.take() with
| None ->
success := false;
if failsafe then
Buffer.contents res
else
begin
parse_error
~start:start_location
~msg:(Printf.sprintf "Could not reach the end of block ending with \"%s\"." (String.escaped word))
(charstream.where());
exit 1
end
| Some c ->
if c = word.[i] then
begin
Buffer.add_char buf c;
loop (succ i)
end
else
begin
Buffer.add_buffer res buf;
Buffer.clear buf;
Buffer.add_char res c;
loop 0
end
in
loop 0
let split_on_char ?(keep_empty_strings=false) c s =
let buf = Buffer.create 42 in
let sl = String.length s in
let make_res res =
match Buffer.contents buf with
| "" -> if keep_empty_strings then "" :: res else res
| b -> b :: res
in
let rec loop index res =
if index >= sl then
make_res res
else if s.[index] = c then
let l = make_res res in
Buffer.clear buf;
loop (index+1) l
else
begin
Buffer.add_char buf s.[index];
loop (index+1) res
end
in List.rev (loop 0 [])
let parse_a_string cs =
let location = cs.where() in
let b = Buffer.create 10 in
let rec loop() =
match cs.take() with
| None -> Buffer.contents b
| Some '"' -> Buffer.contents b
| Some '\\' ->
begin match cs.take() with
| Some ('\\' as c) -> Buffer.add_char b c
| Some 'n' -> Buffer.add_char b '\n'
| Some 'b' -> Buffer.add_char b '\b'
| Some 'r' -> Buffer.add_char b '\r'
| Some 't' -> Buffer.add_char b '\t'
| Some '\n' -> ()
| Some (('x'|'X'| '0' .. '2') as c0) ->
begin
match cs.take() with
| Some ('A' .. 'F' | 'a' .. 'f' | '0' .. '9' as c1) ->
begin match cs.take() with
| Some ('A' .. 'F' | 'a' .. 'f' | '0' .. '9' as c2) ->
let s =
String.init 3
(function
| 0 -> c0
| 1 -> c1
| 2 -> c2
| _ -> assert false)
in
Buffer.add_char b (char_of_int(int_of_string s))
| Some _ | None -> parse_error ~msg:"Error when parsing a string." location
end
| Some _ | None -> parse_error ~msg:"Error when parsing a string." location
end
| Some c -> Buffer.add_char b c
| None -> ()
end;
loop()
| Some c ->
Buffer.add_char b c;
loop()
in
loop()
let stream_of_charstream (cs:charstream) : char Stream.t =
Stream.from (fun _ -> cs.take())
let append cs1 cs2 =
let current_is_cs1 = ref true in
{
take =
(fun () ->
if !current_is_cs1 then
match cs1.take() with
| (Some _) as c ->
c
| None ->
current_is_cs1 := false;
cs2.take()
else
cs2.take()
);
push = (fun c -> if !current_is_cs1 then cs1.push c else cs2.push c);
insert = (fun c -> if !current_is_cs1 then cs1.insert c else cs2.insert c);
where = (fun () -> if !current_is_cs1 then cs1.where() else cs2.where())
}
let delete_trailing_spaces s =
if s = "" then
s
else
let l = ref (String.length s) in
while
(match s.[!l - 1] with
| '\n' | '\t' | ' ' | '\r' -> true
| _ -> false)
do
decr l
done;
if !l = String.length s then
s
else
String.sub s 0 !l
let string_of_charstream ?(keepcs=false) c =
let x = c.where() in
let b = Buffer.create 42 in
let rec loop () =
match c.take () with
| None -> Buffer.contents b
| Some c -> Buffer.add_char b c; loop()
in
let res = loop() in
match keepcs with
| true ->
c.insert (charstream_of_string ~location:x res);
res
| false ->
res