Commits

camlspotter  committed 95f4cb3

rework

  • Participants
  • Parent commits bc7cb9a

Comments (0)

Files changed (7)

   val error : string -> 'a t
   val eos : unit t
   val filter_map : string -> (Str.elem -> 'a option) -> 'a t
-  val token : string -> Str.elem -> Str.elem t
+  val token : Str.elem -> Str.elem t
   val tokenp : string -> (Str.elem -> bool) -> Str.elem t
+  val one_of : Str.elem list -> Str.elem t
   val seq' : Str.elem list -> unit t
+  val option : 'a t -> 'a option t
   val star : 'a t -> 'a list t
   val star_ : 'a t -> unit t
   val plus : 'a t -> 'a list t
   let tokenp : string -> (S.elem -> bool) -> S.elem t = fun error p ->
     filter_map error (fun x -> if p x then Some x else None)
   
-  let token : string -> S.elem -> S.elem t = fun error tkn ->
-    filter_map error (fun x -> if tkn = x then Some x else None)
-  
+  let token : S.elem -> S.elem t = fun tkn ->
+    filter_map 
+      (Printf.sprintf "expected %s" (S.show_elem tkn))
+      (fun x -> if tkn = x then Some x else None)
+
+  let one_of : S.elem list -> S.elem t = fun tkns ->
+    tokenp 
+      (Printf.sprintf "expected one of %s"
+         (String.concat " " (List.map S.show_elem tkns)))
+      (fun x -> List.mem x tkns)
+
   let rec seq' : S.elem list -> unit t = fun str ->
     let rec aux = function
       | [] -> return ()
     in
     aux str >>= fun () -> return ()
   
+  let option : 'a t -> 'a option t = fun com s ->
+    match com s with
+    | R.Error _ -> return None s
+    | R.Ok (v, s') -> return (Some v) s'
+
   let star : 'a t -> 'a list t = fun com ->
     let rec aux st = fun s ->
       match com s with
   val error : string -> 'a t
   val eos : unit t
   val filter_map : string -> (Str.elem -> 'a option) -> 'a t
-  val token : string -> Str.elem -> Str.elem t
+  val token : Str.elem -> Str.elem t
   val tokenp : string -> (Str.elem -> bool) -> Str.elem t
+  val one_of : Str.elem list -> Str.elem t
   val seq' : Str.elem list -> unit t
+  val option : 'a t -> 'a option t
   val star : 'a t -> 'a list t
   val star_ : 'a t -> unit t
   val plus : 'a t -> 'a list t
   val string : string -> string t
   val chars_to_string : char list t -> string t
 
+(*
   type mark
   val mark : mark t
   val string_from_mark : mark -> string t
+*)
   val substr : 'a t -> string t
   val with_substr : 'a t -> (string * 'a) t
   val (</>) : 'a t -> 'a t -> 'a t
   val string : string -> string t
   val chars_to_string : char list t -> string t
 
+(*
   type mark
   val mark : mark t
   val string_from_mark : mark -> string t
+
+  use substr or with_substr instead
+*)
   val substr : 'a t -> string t
   val with_substr : 'a t -> (string * 'a) t
   val (</>) : 'a t -> 'a t -> 'a t
 		   and  type pos = Position.None.t
   val from_string : string -> t
 end = struct
-  include Simple(struct type t = string end)(Position.None)
+  include Simple(struct 
+    type t = string 
+    let show x = Printf.sprintf "%S" x
+  end)(Position.None)
   let from_string str = create (fun () -> 
     Position.None.none, 
     `Some (str, fun () -> Position.None.none, `None))
     abs_pos : Pos.t; (* from the first *)
   }
 
+  let show_elem x = Printf.sprintf "%C" x
+
   let empty = 
     { buf = Simple_string.empty;
       rel_pos = 0;
 
   type gen
   val create : gen -> t
+
+  val show_elem : elem -> string
 end
 
 module type S = sig
 
 type ('a, 'pos) generator = unit -> 'pos * [ `None | `Some of 'a * ('a, 'pos) generator ]
 
-module Simple(E : sig type t end)(Pos : Position.S) = Make(struct
+module Simple(E : sig type t val show : t -> string end)(Pos : Position.S) = Make(struct
   type pos = Pos.t
   type elem = E.t
   type gen = (elem, pos) generator
     | Null _pos -> None
     | Cons (e, _pos, t) -> Some (e, t)
     | Lazy _ -> assert false
+
+  let show_elem = E.show
 end)
     (** get the current position of the stream *)
 
   val create : gen -> t
+
+  val show_elem : elem -> string
 end
 
 module type S = sig
 type ('a, 'pos) generator = unit -> 'pos * [ `None 
 					   | `Some of 'a * ('a, 'pos) generator ]
  
-module Simple(E : sig type t end)(Pos : Position.S) : S 
+module Simple(E : sig 
+  type t 
+  val show : t -> string
+end)(Pos : Position.S) : S 
   with type elem = E.t 
   and  type gen = (E.t, Pos.t) generator
   and  type pos = Pos.t