1995-11-09 03:04:31 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-11-09 03:04:31 -08:00
|
|
|
(* *)
|
|
|
|
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-11-09 03:04:31 -08:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-11-13 05:29:51 -08:00
|
|
|
(* The fields of type t are not mutable to preserve polymorphism of
|
|
|
|
the empty stream. This is type safe because the empty stream is never
|
|
|
|
patched. *)
|
|
|
|
|
|
|
|
type 'a t = {(*mutable*) count : int; (*mutable*) data : 'a data}
|
1995-11-09 03:04:31 -08:00
|
|
|
and 'a data =
|
|
|
|
Sempty
|
|
|
|
| Scons of 'a * 'a data
|
|
|
|
| Sapp of 'a data * 'a data
|
1995-12-07 11:44:19 -08:00
|
|
|
| Slazy of (unit -> 'a data)
|
|
|
|
| Sgen of 'a gen
|
1995-11-09 03:04:31 -08:00
|
|
|
| Sbuffio of buffio
|
1995-12-07 11:44:19 -08:00
|
|
|
and 'a gen =
|
|
|
|
{mutable curr : 'a option option; func : int -> 'a option}
|
1995-11-09 03:04:31 -08:00
|
|
|
and buffio =
|
1995-11-15 05:50:40 -08:00
|
|
|
{ic : in_channel; buff : string; mutable len : int; mutable ind : int}
|
1995-11-09 03:04:31 -08:00
|
|
|
exception Parse_failure
|
|
|
|
exception Parse_error of string
|
|
|
|
|
|
|
|
let count s = s.count
|
|
|
|
|
|
|
|
let fill_buff b =
|
1995-11-15 05:50:40 -08:00
|
|
|
b.len <- input b.ic b.buff 0 (String.length b.buff);
|
|
|
|
b.ind <- 0
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-12-07 11:44:19 -08:00
|
|
|
let rec get_data =
|
1995-11-09 03:04:31 -08:00
|
|
|
function
|
|
|
|
Sempty -> None
|
|
|
|
| Scons (a, d) -> Some (a, d)
|
|
|
|
| Sapp (d1, d2) ->
|
1995-12-07 11:44:19 -08:00
|
|
|
begin match get_data d1 with
|
1995-11-09 03:04:31 -08:00
|
|
|
Some (a, d) -> Some (a, Sapp (d, d2))
|
1995-12-07 11:44:19 -08:00
|
|
|
| None -> get_data d2
|
1995-11-09 03:04:31 -08:00
|
|
|
end
|
1995-12-07 11:44:19 -08:00
|
|
|
| Slazy f -> get_data (f ())
|
|
|
|
| _ -> failwith "illegal stream concatenation"
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let rec peek s =
|
|
|
|
match s.data with
|
|
|
|
Sempty -> None
|
|
|
|
| Scons (a, _) -> Some a
|
|
|
|
| Sapp (d1, d2) ->
|
1995-12-07 11:44:19 -08:00
|
|
|
begin match get_data d1 with
|
1995-11-13 05:29:51 -08:00
|
|
|
Some (a, d) ->
|
|
|
|
Obj.set_field (Obj.repr s) 1 (Obj.repr (Scons (a, Sapp (d, d2))));
|
|
|
|
Some a
|
|
|
|
| None ->
|
|
|
|
Obj.set_field (Obj.repr s) 1 (Obj.repr d2);
|
|
|
|
peek s
|
1995-11-09 03:04:31 -08:00
|
|
|
end
|
1995-12-07 11:44:19 -08:00
|
|
|
| Slazy f ->
|
|
|
|
begin match f () with
|
|
|
|
Sgen _ | Sbuffio _ -> failwith "illegal stream concatenation"
|
|
|
|
| x -> Obj.set_field (Obj.repr s) 1 (Obj.repr x); peek s
|
|
|
|
end
|
|
|
|
| Sgen {curr = Some a} -> a
|
|
|
|
| Sgen g -> let x = g.func s.count in g.curr <- Some x; x
|
1995-11-09 03:04:31 -08:00
|
|
|
| Sbuffio b ->
|
|
|
|
if b.ind >= b.len then fill_buff b;
|
1995-11-13 05:29:51 -08:00
|
|
|
if b.len == 0 then begin
|
|
|
|
Obj.set_field (Obj.repr s) 1 (Obj.repr Sempty); None
|
|
|
|
end
|
1995-11-09 03:04:31 -08:00
|
|
|
else Some (Obj.magic b.buff.[b.ind])
|
|
|
|
|
|
|
|
let rec junk s =
|
|
|
|
match s.data with
|
1995-11-13 05:29:51 -08:00
|
|
|
Scons (_, s') ->
|
|
|
|
Obj.set_field (Obj.repr s) 0 (Obj.repr (succ s.count));
|
|
|
|
Obj.set_field (Obj.repr s) 1 (Obj.repr s')
|
1995-12-07 11:44:19 -08:00
|
|
|
| Sgen {curr=Some None} -> ()
|
|
|
|
| Sgen ({curr=Some _} as g) ->
|
|
|
|
Obj.set_field (Obj.repr s) 0 (Obj.repr (succ s.count)); g.curr <- None
|
1995-11-13 05:29:51 -08:00
|
|
|
| Sbuffio b ->
|
|
|
|
Obj.set_field (Obj.repr s) 0 (Obj.repr (succ s.count));
|
1995-11-15 05:50:40 -08:00
|
|
|
b.ind <- succ b.ind
|
1995-11-13 05:29:51 -08:00
|
|
|
| _ -> match peek s with None -> () | Some _ -> junk s
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let next s =
|
|
|
|
match peek s with
|
|
|
|
Some a -> junk s; a
|
|
|
|
| None -> raise Parse_failure
|
|
|
|
|
|
|
|
let empty s =
|
|
|
|
match peek s with
|
|
|
|
Some _ -> raise Parse_failure
|
|
|
|
| None -> ()
|
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
let iter f strm =
|
|
|
|
let rec do_rec () =
|
|
|
|
match peek strm with
|
|
|
|
Some a -> junk strm; f a; do_rec ()
|
|
|
|
| None -> ()
|
|
|
|
in
|
|
|
|
do_rec ()
|
|
|
|
|
|
|
|
(* Stream building functions *)
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-12-07 11:44:19 -08:00
|
|
|
let from f = {count = 0; data = Sgen {curr = None; func = f}}
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let of_list l =
|
|
|
|
{count = 0; data = List.fold_right (fun x l -> Scons (x, l)) l Sempty}
|
|
|
|
|
|
|
|
let of_string s =
|
|
|
|
from (fun c -> if c < String.length s then Some s.[c] else None)
|
|
|
|
|
|
|
|
let of_channel ic =
|
|
|
|
{count = 0;
|
|
|
|
data = Sbuffio {ic = ic; buff = String.create 4096; len = 0; ind = 0}}
|
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
(* Stream expressions builders *)
|
|
|
|
|
1995-11-09 03:04:31 -08:00
|
|
|
let sempty = {count = 0; data = Sempty}
|
1995-12-07 11:44:19 -08:00
|
|
|
let scons f s = {count = 0; data = Slazy (fun _ -> Scons (f (), s.data))}
|
1995-11-09 03:04:31 -08:00
|
|
|
let sapp f s =
|
|
|
|
match s.data with
|
1995-12-07 11:44:19 -08:00
|
|
|
Sempty -> {count = 0; data = Slazy (fun _ -> (f ()).data)}
|
|
|
|
| d -> {count = 0; data = Slazy (fun _-> Sapp ((f ()).data, d))}
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
(* For debugging use *)
|
|
|
|
|
1995-11-09 03:04:31 -08:00
|
|
|
let rec dump f s =
|
|
|
|
print_string "{count = "; print_int s.count; print_string "; data = ";
|
|
|
|
dump_data f s.data; print_string "}"; print_newline ()
|
|
|
|
and dump_data f =
|
|
|
|
function
|
|
|
|
Sempty -> print_string "Sempty"
|
|
|
|
| Scons (a, d) ->
|
|
|
|
print_string "Scons ("; f a; print_string ", "; dump_data f d;
|
|
|
|
print_string ")"
|
|
|
|
| Sapp (d1, d2) ->
|
|
|
|
print_string "Sapp ("; dump_data f d1; print_string ", ";
|
|
|
|
dump_data f d2; print_string ")"
|
1995-12-07 11:44:19 -08:00
|
|
|
| Slazy f -> print_string "Slazy"
|
|
|
|
| Sgen _ -> print_string "Sgen"
|
1995-11-09 03:04:31 -08:00
|
|
|
| Sbuffio b -> print_string "Sbuffio"
|