1995-11-09 03:04:31 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1997-06-16 08:33:44 -07:00
|
|
|
(* Ocaml *)
|
1995-11-09 03:04:31 -08:00
|
|
|
(* *)
|
|
|
|
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1997-06-16 08:33:44 -07:00
|
|
|
(* Copyright 1997 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the GNU Library General Public License. *)
|
1995-11-09 03:04:31 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
(* Module [Stream]: streams and parsers *)
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
type 'a t
|
1999-11-29 11:04:56 -08:00
|
|
|
(* The type of streams holding values of type ['a]. *)
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
exception Failure;;
|
1999-11-29 11:04:56 -08:00
|
|
|
(* Raised by parsers when none of the first components of the stream
|
1995-11-09 07:08:44 -08:00
|
|
|
patterns is accepted. *)
|
1997-06-16 08:33:44 -07:00
|
|
|
exception Error of string;;
|
1999-11-29 11:04:56 -08:00
|
|
|
(* Raised by parsers when the first component of a stream pattern is
|
|
|
|
accepted, but one of the following components is rejected. *)
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
(** Stream builders *)
|
1995-12-07 11:44:19 -08:00
|
|
|
(* Warning: these functions create streams with fast access; it is illegal
|
1995-12-29 05:15:44 -08:00
|
|
|
to mix them with streams built with [[< >]]; would raise [Failure]
|
1995-12-07 11:44:19 -08:00
|
|
|
when accessing such mixed streams. *)
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
val from : (int -> 'a option) -> 'a t;;
|
1999-11-29 11:04:56 -08:00
|
|
|
(* [Stream.from f] returns a stream built from the function [f].
|
1995-11-09 07:08:44 -08:00
|
|
|
To create a new stream element, the function [f] is called with
|
|
|
|
the current stream count. The user function [f] must return either
|
|
|
|
[Some <value>] for a value or [None] to specify the end of the
|
|
|
|
stream. *)
|
1997-06-16 08:33:44 -07:00
|
|
|
val of_list : 'a list -> 'a t;;
|
1995-11-13 05:29:51 -08:00
|
|
|
(* Return the stream holding the elements of the list in the same
|
1995-11-09 03:04:31 -08:00
|
|
|
order. *)
|
1997-06-16 08:33:44 -07:00
|
|
|
val of_string : string -> char t;;
|
1995-11-13 05:29:51 -08:00
|
|
|
(* Return the stream of the characters of the string parameter. *)
|
1997-06-16 08:33:44 -07:00
|
|
|
val of_channel : in_channel -> char t;;
|
1999-11-29 11:04:56 -08:00
|
|
|
(* Return the stream of the characters read from the input channel. *)
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
(** Stream iterator *)
|
|
|
|
|
1997-10-31 04:59:29 -08:00
|
|
|
val iter : ('a -> unit) -> 'a t -> unit;;
|
1995-11-10 07:15:12 -08:00
|
|
|
(* [Stream.iter f s] scans the whole stream s, applying function [f]
|
|
|
|
in turn to each stream element encountered. *)
|
|
|
|
|
|
|
|
(** Predefined parsers *)
|
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
val next : 'a t -> 'a;;
|
1999-11-29 11:04:56 -08:00
|
|
|
(* Return the first element of the stream and remove it from the
|
1997-06-16 08:33:44 -07:00
|
|
|
stream. Raise [Stream.Failure] if the stream is empty. *)
|
|
|
|
val empty : 'a t -> unit;;
|
1999-11-29 11:04:56 -08:00
|
|
|
(* Return [()] if the stream is empty, else raise [Stream.Failure]. *)
|
1995-11-10 07:15:12 -08:00
|
|
|
|
1995-12-06 07:46:34 -08:00
|
|
|
(** Useful functions *)
|
1995-11-10 07:15:12 -08:00
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
val peek : 'a t -> 'a option;;
|
|
|
|
(* Return [Some] of "the first element" of the stream, or [None] if
|
|
|
|
the stream is empty. *)
|
|
|
|
val junk : 'a t -> unit;;
|
1995-11-13 05:29:51 -08:00
|
|
|
(* Remove the first element of the stream, possibly unfreezing
|
|
|
|
it before. *)
|
1999-10-14 06:40:47 -07:00
|
|
|
val count : 'a t -> int;;
|
1999-11-29 11:04:56 -08:00
|
|
|
(* Return the current count of the stream elements, i.e. the number
|
1995-11-10 07:15:12 -08:00
|
|
|
of the stream elements discarded. *)
|
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
val npeek : int -> 'a t -> 'a list;;
|
|
|
|
(* [npeek n] returns the list of the [n] first elements of
|
|
|
|
the stream, or all its remaining elements if less than [n]
|
|
|
|
elements are available. *)
|
|
|
|
|
1995-11-09 03:04:31 -08:00
|
|
|
(*--*)
|
|
|
|
|
|
|
|
(*** For system use only, not for the casual user *)
|
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
val iapp : 'a t -> 'a t -> 'a t;;
|
|
|
|
val icons : 'a -> 'a t -> 'a t;;
|
|
|
|
val ising : 'a -> 'a t;;
|
|
|
|
|
|
|
|
val lapp : (unit -> 'a t) -> 'a t -> 'a t;;
|
|
|
|
val lcons : (unit -> 'a) -> 'a t -> 'a t;;
|
|
|
|
val lsing : (unit -> 'a) -> 'a t;;
|
|
|
|
|
|
|
|
val sempty : 'a t;;
|
1997-10-14 06:17:48 -07:00
|
|
|
val slazy : (unit -> 'a t) -> 'a t;;
|
1997-06-16 08:33:44 -07:00
|
|
|
|
|
|
|
val dump : ('a -> unit) -> 'a t -> unit;;
|