module Tyre:sig..end
Typed regular expressions.
type 'a t
The type variable is the type of the returned value when the typed regular expression (tyregex) is executed. tyregexs are bi-directional and can be used both for matching and evaluation. Multiple tyregexs can be combined in order to do routing in similar manner as switches/pattern matching.
Typed regular expressions are strictly as expressive as regular expressions from re (and are, as such, regular expressions, not PCREs). Performances should be exactly the same.
For example tyre : int t can be used to return an int. In the rest of the documentation, we will use tyre to designate a value of type Tyre.t.
val regex : Re.t -> string tregex re is a tyregex that matches re and return the corresponding string.
Groups inside re are erased.val conv : name:string -> ('a -> 'b option) -> ('b -> 'a) -> 'a t -> 'b tconv ~name to_ from_ tyre matches the same text as tyre, but converts back and forth to a different data type. For example, this is the implementation of Tyre.pos_int:
let pos_int = Tyre.conv (try_ int_of_string) string_of_int (Tyre.regex (Re.rep1 Re.digit))
The to_ part of the converter is allowed to fail, by returning None. If it does, Tyre.exec will return `ConverterFailure (name, s) with s the substring on which the converter was called.
val opt : 'a t -> 'a option topt tyre matches either tyre or the empty string. Similar to Re.opt.val alt : 'a t -> 'b t -> [ `Left of 'a | `Right of 'b ] talt tyreL tyreR matches either tyreL (and will then return `Left v) or tyreR (and will then return `Right v).type'agen =unit -> 'a option
val rep : 'a t -> 'a gen trep tyre matches tyre zero or more times. Similar to Re.rep.
For matching, rep tyre will matches the string a first time, then tyre will be used to walk the matched part to extract values.
val rep1 : 'a t -> ('a * 'a gen) trep1 tyre is seq tyre (rep tyre). Similar to Re.rep1.val seq : 'a t -> 'b t -> ('a * 'b) tseq tyre1 tyre2 matches tyre1 then tyre2 and return both values.val prefix : 'b t * 'b -> 'a t -> 'a tprefix (tyre_i, s) tyre matches tyre_i, ignores the result, and then matches tyre and returns its result.
s is the witness used for evaluation. It is assumed (but not checked) that tyre_i matches s.
val prefixstr : string -> 'a t -> 'a tprefixstr s tyre matches s then matches tyre and return its value.
It is equal to prefix (regex (Re.str s), s) tyre.
val suffix : 'a t -> 'b t * 'b -> 'a tprefix, but reversed.val suffixstr : 'a t -> string -> 'a tprefixstr, but reversed.
The tyregexs are on sides with an arrow.
val (<?>) : 'a t -> 'b t -> [ `Left of 'a | `Right of 'b ] tt <?> t' is alt t t'.val (<*>) : 'a t -> 'b t -> ('a * 'b) tt <*> t' is seq t t'.val ( *> ) : string -> 'a t -> 'a ts *> t is prefixstr s t.val ( <* ) : 'a t -> string -> 'a tt <* s is suffixstr s t.val ( **> ) : 'b t * 'b -> 'a t -> 'a t (ti,s) **> t is prefix (ti,s) t.val ( <** ) : 'a t -> 'b t * 'b -> 'a t t <** (ti,s) is suffix t (ti,s).val int : int tint matches -?[0-9]+ and returns the matched integer.
Integers that do not fit in an int will fail.
val pos_int : int tpos_int matches [0-9]+ and returns the matched positive integer.
Integers that do not fit in an int will fail.
val float : float tfloat matches -?[0-9]+( .[0-9]* )? and returns the matched floating point number.
Floating point numbers that do not fit in a float returns infinity or neg_infinity.
val bool : bool tbool matches true|false and returns the matched boolean.val list : 'a t -> 'a list tlist e is similar to rep e, but returns a list.val terminated_list : sep:string -> 'a t -> 'a list tterminated_list ~sep tyre is list (tyre <* sep) .val separated_list : sep:string -> 'a t -> 'a list tseparated_list ~sep tyre is equivalent to opt (e <*> list (sep *> e)).
See Re for details on the semantics of those combinators.
val word : 'a t -> 'a t
val whole_string : 'a t -> 'a t
val longest : 'a t -> 'a t
val shortest : 'a t -> 'a t
val first : 'a t -> 'a t
val greedy : 'a t -> 'a t
val non_greedy : 'a t -> 'a t
val nest : 'a t -> 'a ttype 'a re
val compile : ?whole:bool -> 'a t -> 'a recompile tyre is the compiled tyregex representing tyre.
if whole is true (the default), the whole string is matched.type'aerror =[ `ConverterFailure of string * string | `NoMatch of 'a re * string ]
val exec : ?pos:int ->
?len:int -> 'a re -> string -> ('a, 'a error) Result.resultexec ctyre s matches the string s using
the compiled tyregex ctyre and returns the extracted value.
Returns Error (`NoMatch (tyre, s) if tyre doesn't match s.
Returns Error (`ConverterFailure (name, s)) if the converter named name failed while trying to convert the substring s.
pos : optional beginning of the string (default 0)len : length of the substring of str that can be matched (default to the end of the string)type 'a route =
| |
Route : |
(* |
A route is a pair of a tyregex and a handler.
When the tyregex is matched, the function is called with the
result of the matching.
| *) |
val (-->) : 'x t -> ('x -> 'a) -> 'a routetyre --> f is Route (tyre, f).val route : ?whole:bool -> 'a route list -> 'a reroute [ tyre1 --> f1 ; tyre2 --> f2 ] produces a compiled
tyregex such that, if tyre1 matches, f1 is called, and so on.
if whole is true (the default), the whole string is matched.
The compiled tyregex shoud be used with Tyre.exec.
val eval : 'a t -> 'a -> stringeval tyre v returns a string s such that exec (compile tyre) s = v.
Note that such string s is not unique. eval will usually returns a very simple witness.
val evalpp : 'a t -> Format.formatter -> 'a -> unitevalpp tyre ppf v is equivalent to Format.fprintf ppf "%s" (eval tyre v), but more efficient.
Is is generally used with "%a":
let my_pp = Tyre.evalpp tyre in
Format.printf "%a@." my_pp v
val pp : Format.formatter -> 'a t -> unit
val pp_re : Format.formatter -> 'a re -> unit
module Internal:sig..end