camlspotter avatar camlspotter committed 4a3ad93

added pfile

Comments (0)

Files changed (12)

    sstring
    smemo
    sbuffer
+   sfile
    planck_intf
    profile
    pbase
    pbuffer
    pmemo
    pstate
+   pfile
    op_prec
-   strfile
 
 LIB = planck
 
 
 module type S = sig
   include Planck_intf.S
+(* CR jfuruse: damn it, we cannot restrict signature by components of itself!
+      with type error = Str.Pos.t * string
+      and  type 'a t = Str.t -> ('a * Str.t, Str.Pos.t * string (* =error *)) Result.t
+*)
   val run : 'a t -> Str.t -> ('a * Str.t, error) Result.t
 end
 
 module Make( Str : Stream_intf.S ) 
   : S with module Str = Str
       and  type error = Str.Pos.t * string
-      and  type 'a t = Str.t 
-                       -> ('a * Str.t, Str.Pos.t * string (* =error *)) Result.t
+      and  type 'a t = Str.t -> ('a * Str.t, Str.Pos.t * string (* =error *)) Result.t
 (** Functor [Make] takes a stream implementation [S] and create a monadic parser module for [S].
     The parse uses the element, position and attribute from [S].
     The monad is a function type [Str.t -> ('a * Str.t, error) Result.t].
 open Spotlib.Spot
 
+module type X = sig
+
+  type +'a t (** parser monad type *)
+
+  val prefix : int -> string t
+  (** fast retrieval of the prefix string of the given length *)
+
+  val takeWhile : (char -> bool) -> string t
+  (** [takeWhile p] matches the longest prefix string where all the chars hold [p] *)
+
+  val ( ??** ) : (char -> bool) -> string t
+  val ( ??* ) : (char -> bool) -> unit t
+  (** Same as [Planck_intf.( ?** )] and [Planck_intf.( ?* )] but specialized for char stream *)   
+
+  val string : string -> unit t
+  (** Efficient version of [Pchar.string] using Sbuffer. 
+      [string s] succeeds if the prefix of the stream is [s]. *)
+
+  val chars_to_string : char list t -> string t
+  (** Type conversion from [char list t] to [string t].
+      For efficiency reason, users is not encouraged to use this function. Use [matched] instead.
+  *)
+
+  val matched : unit t -> string t
+  (** [matched t] returns the matched string part of [t].
+      It is far more efficient than manual concatenation of chars using [chars_to_string].
+  *)
+
+  val with_matched : 'a t -> ('a * string) t
+  (** [with_matched t] returns the matched string part of [t] 
+      along with the original result of [t]. *)
+
+  val ( </> ) : 'a t -> 'a t -> 'a t
+  (** Longest match. [a </> b] succeeds if either [a] or [b] succeeds.
+      If the both of [a] and [b] succeeds, the longer match is used as the result of [a </> b].
+
+      Note: [a </> b] runs both [a] and [b]. Too many uses of [</>] affects the parsing performance badly.
+  *)
+end
+
 module Extend(Sbuffer : sig
-  include Stream_intf.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 substr : t -> int -> int -> string * t
   val takeWhile : (char -> bool) -> t -> string * t
   val bytes : t -> int
 end)(Base : Planck_intf.S
-     with type Str.Elem.t = char
+     with type Str.desc   = Sbuffer.desc (* I want to say Str.t = Sbuffer.t but not possible *)
+     and  type Str.Elem.t = char
      and  type Str.Pos.t  = Position.File.t
-     and  type Str.desc = Sbuffer.desc
+     and  type Str.Attr.t = Sbuffer.Attr.t
     ) = struct
 
   open Base
             error (err1 ^ " or " ^ err2) <?@> pos0)
 
 end
+
+module Make(Sbuffer : sig
+  include Stream_intf.S
+  with type Elem.t = char
+  and  type Pos.t  = Position.File.t
+  val substr : t -> int -> int -> string * t
+  val takeWhile : (char -> bool) -> t -> string * t
+  val bytes : t -> int
+end) = struct
+  module Base = Pbase.Make(Sbuffer)
+  include Base
+  include Extend(Sbuffer)(Base)
+end
+
     See a use example in test/expr.ml
 *)
 
-module Extend(Sbuffer : sig
-  include Stream_intf.S
-  with type Elem.t = char
-  and  type Pos.t = Position.File.t
-  val substr : t -> int -> int -> string * t
-  val takeWhile : (char -> bool) -> t -> string * t
-  val bytes : t -> int
-end)(Base : Planck_intf.S 
-     with type Str.Elem.t = char
-     and  type Str.Pos.t  = Position.File.t
-     and  type Str.desc   = Sbuffer.desc) : sig
+module type X = sig
 
-  (* CR jfuruse: we cannot write Base : Planck_intf.S with module Str = Sbuffer ! *)
-
-  open Base
+  type +'a t (** stream type *)
 
   val prefix : int -> string t
   (** fast retrieval of the prefix string of the given length *)
       Note: [a </> b] runs both [a] and [b]. Too many uses of [</>] affects the parsing performance badly.
   *)
 end
+
+
+module Extend(Sbuffer : sig
+  include Stream_intf.S with type Elem.t = char
+                        and  type Pos.t = Position.File.t
+  val substr : t -> int -> int -> string * t
+  val takeWhile : (char -> bool) -> t -> string * t
+  val bytes : t -> int
+end)(Base : Planck_intf.S 
+     with type Str.desc   = Sbuffer.desc (* I want to say Str.t = Sbuffer.t but not possible *)
+     and  type Str.Elem.t = char
+     and  type Str.Pos.t  = Position.File.t
+     and  type Str.Attr.t = Sbuffer.Attr.t )
+  : X with type 'a t := 'a Base.t
+
+
+module Make(Sbuffer : sig
+  include Stream_intf.S with type Elem.t = char
+                        and  type Pos.t = Position.File.t
+  val substr : t -> int -> int -> string * t
+  val takeWhile : (char -> bool) -> t -> string * t
+  val bytes : t -> int
+end) : sig
+  include Pbase.S 
+    with type Str.desc   = Sbuffer.desc (* I want to say Str.t = Sbuffer.t but not possible *)
+    and  type Str.Elem.t = char
+    and  type Str.Pos.t  = Position.File.t
+    and  type Str.Attr.t = Sbuffer.Attr.t
+    and  type error = Sbuffer.Pos.t * string
+    and  type 'a t = Sbuffer.t -> ('a * Sbuffer.t, Sbuffer.Pos.t * string (* =error *)) Result.t
+  include X with type 'a t := 'a Pbase.Make(Sbuffer).t
+end
+module Stream = Sfile
+type 'a t = Stream.t -> ('a * Stream.t, Stream.Pos.t * string (* error *)) Result.t
+module Parser = Pbuffer.Make(Stream)
+(* Stream from Sfile *)
+module Stream : Sfile.S
+
+type 'a t = Stream.t -> ('a * Stream.t, Stream.Pos.t * string (* error *)) Result.t
+
+(* Parser from Pbuffer *)
+module Parser : sig
+  include Pbase.S with type Str.desc   = Stream.desc
+                  and  type Str.Elem.t = char
+                  and  type Str.Pos.t  = Position.File.t
+                  and  type Str.desc   = Stream.desc
+                  and  type Str.Attr.t = Stream.Attr.t
+                  and  type error = Stream.Pos.t * string
+                  and  type 'a t = 'a t
+  include Pbuffer.X with type 'a t := 'a Pbase.Make(Stream).t
+end
 
 let position_of_buf buf = buf.abs_pos
 
-module type S = sig
+module type X = sig
 
   type t (** the type of the stream *)
 
   val buf : t -> buf
   val create_attr : buf -> Attr.t
 
-end) : S with type t := Base.t = struct
+end) = struct
 
   open Base
 
 *)
 
 
-module type S = sig
+module type X = sig
 
   type t (** the type of the stream *)
 
   val buf : t -> buf
   val create_attr : buf -> Attr.t
 
-end) : S with type t := Base.t
+end) : X with type t := Base.t
+(* char stream from a file *)
+
+module type S = sig
+  include Stream_intf.S with type Elem.t = char
+                        and  type Pos.t  = Position.File.t
+                        and  type Attr.t = Sbuffer.buf
+
+  include Sbuffer.X with type t := t
+end
+
+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)
+
+module type S = sig
+  include Stream_intf.S with type Elem.t = char
+                        and  type Pos.t  = Position.File.t
+                        and  type Attr.t = Sbuffer.buf
+
+  include Sbuffer.X with type t := t
+end
+
+include S

lib/strfile.ml

-(* 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
-

lib/strfile.mli

-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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.