Commits

camlspotter committed adc85e2

<||>

Comments (0)

Files changed (3)

 name="planck"
 version="0.0.1"
 description="Parser LANguage Combinator Kit"
-requires=""
+requires="sexplib"
 archive(byte)="planck.cmo"
 archive(native)="planck.cmx"
 linkopts = ""
   val plus_ : 'a t -> unit t
   val surrounded : 'a t -> 'b t -> 'c t -> 'c t
   val ( <|> ) : 'a t -> 'a t -> 'a t
+  val ( <||> ) : 'a option t -> 'a option t -> 'a option t
+  val ( <|>> ) : 'a option t -> 'a t -> 'a t
   val ignore : 'a t -> unit t
-    
+  val catch : 'a t -> 'a Result.t t
+  val catch_opt : 'a t -> 'a option t
+  val begin_with : 'a t -> 'b t -> 'b option t
+
   val stream_gen : 
     'a option t 
     -> (Str.t -> Str.t -> 'b)  (* position calculator *)
       | (R.Ok _ as res) -> res
       | R.Error _ -> c2 st
 
+
+  let (<||>) : 'a option t -> 'a option t -> 'a option t = fun c1 c2 -> 
+    fun st ->
+      let res = c1 st in
+      match res with
+      | R.Ok (None,_) -> c2 st
+      | R.Ok (Some _, _) -> res
+      | R.Error _ -> res
+
+  let (<|>>) : 'a option t -> 'a t -> 'a t = fun c1 c2 -> 
+    fun st ->
+      let res = c1 st in
+      match res with
+      | R.Ok (None,_) -> c2 st
+      | R.Ok (Some v, st) -> R.Ok (v, st)
+      | R.Error e -> R.Error e
+
+  let catch : 'a t -> 'a R.t t = fun c st ->
+    match c st with
+    | R.Ok (res, st) -> R.Ok (R.Ok res, st)
+    | R.Error e -> R.Ok (R.Error e, st)
+
+  let catch_opt : 'a t -> 'a option t = fun c st ->
+    match c st with
+    | R.Ok (res, st) -> R.Ok (Some res, st)
+    | R.Error _e -> R.Ok (None, st)
+
   let ignore : 'a t -> unit t = fun c st ->
     match c st with
     | R.Ok (_,st) -> R.Ok ((), st)
     | R.Error e -> R.Error e
 
+  let begin_with c1 c2 =
+    catch_opt c1 >>= function
+      | None -> return None
+      | Some _ -> 
+	  c2 >>= fun v -> return (Some v)
+
   module Str = S
 
   let stream_gen (c : 'a option t) (pos_conv : Str.t -> Str.t -> 'b) (st : Str.t) =
   val plus_ : 'a t -> unit t
   val surrounded : 'a t -> 'b t -> 'c t -> 'c t
   val ( <|> ) : 'a t -> 'a t -> 'a t
+  val ( <||> ) : 'a option t -> 'a option t -> 'a option t
+  val ( <|>> ) : 'a option t -> 'a t -> 'a t
   val ignore : 'a t -> unit t
+  val catch : 'a t -> 'a Result.t t
+  val catch_opt : 'a t -> 'a option t
+  val begin_with : 'a t -> 'b t -> 'b option t
 
   val stream_gen : 
     'a option t