From 6bda945b3b80953a7450e40631be1b5f517224f8 Mon Sep 17 00:00:00 2001 From: Emile Trotignon Date: Fri, 8 Nov 2024 22:57:49 +0100 Subject: [PATCH 1/2] Add attach function --- src/tyre.ml | 3 +++ src/tyre.mli | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/tyre.ml b/src/tyre.ml index 64f9447..9e54de8 100644 --- a/src/tyre.ml +++ b/src/tyre.ml @@ -78,6 +78,9 @@ let pcre s = regex @@ Re.Pcre.re s let conv to_ from_ x : _ t = Conv (x, {to_; from_}) +let attach v x = + conv (fun () -> v) (fun _ -> ()) x + let seq a b : _ t = Seq (a, b) let alt a b : _ t = Alt (a, b) diff --git a/src/tyre.mli b/src/tyre.mli index 8849793..bd0d70d 100644 --- a/src/tyre.mli +++ b/src/tyre.mli @@ -1,6 +1,6 @@ (** {1 Typed regular expressions} *) -(** +(** Tyre is a set of combinators to build type-safe regular expressions, allowing automatic extraction and modification of matched groups. Tyre is bi-directional: a typed regular expressions can be used both for {{!matching}matching} and {{!eval}evaluation}. Multiple tyregexs can be combined in order to do {{!routing}routing} in similar manner as switches/pattern matching. @@ -40,7 +40,7 @@ type 'a t (** {1 Combinators} *) val pcre : string -> string t -(** [pcre s] is a tyregex that matches the PCRE [s] and return the +(** [pcre s] is a tyregex that matches the PCRE [s] and return the corresponding string. Groups in [s] are ignored. *) @@ -66,6 +66,9 @@ let pos_int = ]} *) +val attach : 'a -> unit t -> 'a t +(** [attach v tyre] matches [tyre] but has value [v]. Is a simplification of [conv] for [unit] regular expressions.*) + val opt : 'a t -> 'a option t (** [opt tyre] matches either [tyre] or the empty string. Similar to {!Re.opt}. *) From 13fdb497a3b65b728cb33a3ebf58cced5da6d031 Mon Sep 17 00:00:00 2001 From: Emile Trotignon Date: Sat, 9 Nov 2024 16:03:30 +0100 Subject: [PATCH 2/2] Rename attach to const --- src/tyre.ml | 2 +- src/tyre.mli | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tyre.ml b/src/tyre.ml index 9e54de8..dc04326 100644 --- a/src/tyre.ml +++ b/src/tyre.ml @@ -78,7 +78,7 @@ let pcre s = regex @@ Re.Pcre.re s let conv to_ from_ x : _ t = Conv (x, {to_; from_}) -let attach v x = +let const v x = conv (fun () -> v) (fun _ -> ()) x let seq a b : _ t = Seq (a, b) diff --git a/src/tyre.mli b/src/tyre.mli index bd0d70d..fcfdb1f 100644 --- a/src/tyre.mli +++ b/src/tyre.mli @@ -66,8 +66,8 @@ let pos_int = ]} *) -val attach : 'a -> unit t -> 'a t -(** [attach v tyre] matches [tyre] but has value [v]. Is a simplification of [conv] for [unit] regular expressions.*) +val const : 'a -> unit t -> 'a t +(** [const v tyre] matches [tyre] but has value [v]. Is a simplification of [conv] for [unit] regular expressions.*) val opt : 'a t -> 'a option t (** [opt tyre] matches either [tyre] or the empty string. Similar to {!Re.opt}. *)