Commits

camlspotter committed 41841ff

added Strfile and Elem to add useful instances

Comments (0)

Files changed (9)

 \.(sp[io]t|annot|o|cm[a-z]+|orig|omc|lock|opt|run)$
 \.omakedb$
 .*\.a
+test/test_expr$
 test/expr$
 test/expr_test.ml$
 test/exprexn$
    mtypes
    position
    result
+   elem
    stream_intf
    stream
    sstring
    pmemo
    pstate
    op_prec
+   strfile
 
 LIB = planck
 
+(** Type of the stream element *)
+module type S = sig
+  type t (** the element of the stream *)
+
+  include Mtypes.Printable  with type t := t
+  include Mtypes.Comparable with type t := t
+end
+
+module Char = struct
+  type t = char
+  let show = Printf.sprintf "%C"
+  let format ppf = Format.fprintf ppf "%C"
+  let equal (x : char) y = x = y
+  let compare (x : char) y = compare x y
+end
+(** Type of the stream element *)
+module type S = sig
+
+    type t
+
+    include Mtypes.Printable  with type t := t
+    include Mtypes.Comparable with type t := t
+    
+end
+
+module Char : S with type t = char
 
 let position_of_buf buf = buf.abs_pos
 
+module type S = sig
+
+  type t (** the type of the stream *)
+
+  val create : buf -> t
+  (** Create a stream from a buffer *)
+
+  val substr : t -> int -> int -> string * t
+  (** [substr t from len] returns a substring of [len] chars from [t]
+      from its absolute char position [from], and a stream which starts at the end of the substring.
+
+      It may raise an exception if [from] and [len] point outside of the stream *)
+
+  val takeWhile : (char -> bool) -> t -> string * t
+  (** [takeWhile p t] returns the longest prefix of [t] all whose characters suffices the predicate [p],
+      and the stream which starts at the end of the prefix.
+  *)
+
+  val bytes : t -> int
+  (** [bytes t] returns the head position of the stream as the nubmer of bytes. *)
+
+  val from_string : filename:string -> string -> t
+  val from_chan : filename:string -> in_channel -> t
+  (** Creation functions of buffered streams *)
+
+  val set_position : t -> Position.File.t -> t
+  (** [set_position t pos] returns the same stream as [t] but with the head position is changed by [pos] *)
+end
+
 module Extend(Base : sig
-  include S
-  with type Elem.t = char
-  and  type Pos.t  = Position.File.t
+
+  include Stream_intf.S with type Elem.t = char
+                        and  type Pos.t  = Position.File.t
   val buf : t -> buf
   val create_attr : buf -> Attr.t
-end) = struct
+
+end) : S with type t := Base.t = struct
 
   open Base
 
+  type t = Base.t
+
   let bytes t = (buf t).buf_pos + (buf t).rel_pos
    
   let advance_char abs_pos = function
     end
 *)
 
-module Extend(Base : sig
-  include S
-  with type Elem.t = char
-  and  type Pos.t  = Position.File.t
-  (** Sbuffer is based on a stream whose element is [char].
-      [attr] is implementation dependent.
-  *)
-  val buf : t -> buf
-  val create_attr : buf -> Attr.t
-end) : sig
 
-  open Base
+module type S = sig
+
+  type t (** the type of the stream *)
 
   val create : buf -> t
   (** Create a stream from a buffer *)
   val set_position : t -> Position.File.t -> t
   (** [set_position t pos] returns the same stream as [t] but with the head position is changed by [pos] *)
 end
+
+module Extend(Base : sig
+
+  include Stream_intf.S with type Elem.t = char
+                        and  type Pos.t  = Position.File.t
+  (** Sbuffer is based on a stream whose element is [char].
+      [attr] is implementation dependent.
+  *)
+  val buf : t -> buf
+  val create_attr : buf -> Attr.t
+
+end) : S with type t := Base.t
 (** Minimum specification to create a stream *)
 module type Base = sig
 
+  module Elem : Elem.S
+  (** Type of the stream element *)
+
   module Pos : Position.S
   (** Position of the stream. 
       Position is a part of the stream attribute defined below. *)
       The position of the stream must be obtained from [Attr.t].
   *)
 
-  module Elem : sig
-    type t
-    (** Type of the stream element *)
-
-    include Mtypes.Printable  with type t := t
-    include Mtypes.Comparable with type t := t
-  end
-
 end
 
 (* Standard interface *)
+(* char stream from a file *)
+
+module Buffered = struct
+
+  module Base = struct
+    
+    module Elem = Elem.Char
+    module Pos  = Position.File
+    
+    module Attr = struct
+      type t = Sbuffer.buf
+      let position attr = Sbuffer.position_of_buf attr
+    end
+    
+  end
+  
+  module NonBuffered = Stream.Make(Base)
+
+  include NonBuffered
+
+  include Sbuffer.Extend(struct
+    include NonBuffered
+    let create_attr buf = buf
+    let buf st = attr st
+  end)
+end
+
+module Buffered : sig
+
+  include Stream_intf.S with type Elem.t = char
+                        and  type Pos.t  = Position.File.t
+
+  include Sbuffer.S with type t := t
+
+end