Page
Library
Module
Module type
Parameter
Class
Class type
Source
TyreTyre
Typed regular expressions.
A typed regular expression.
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 t.
regex re is a tyregex that matches re and return the corresponding string. Groups inside re are erased.
conv ~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 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, exec will return `ConverterFailure (name, s) with s the substring on which the converter was called.
alt tyreL tyreR matches either tyreL (and will then return `Left v) or tyreR (and will then return `Right v).
A generator g will return a new value each time it's called, until it returns None. See gen.
seq tyre1 tyre2 matches tyre1 then tyre2 and return both values.
prefix (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.
prefixstr s tyre matches s then matches tyre and return its value.
It is equal to prefix (regex (Re.str s), s) tyre.
The tyregexs are on sides with an arrow.
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.
terminated_list ~sep tyre is list (tyre <* sep) .
separated_list ~sep tyre is equivalent to opt (e <*> list (sep *> e)).
See Re for details on the semantics of those combinators.
compile tyre is the compiled tyregex representing tyre. if whole is true (the default), the whole string is matched.
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.
val execp : ?pos:int -> ?len:int -> 'a re -> string -> boolexecp ctyre s returns true if ctyre matches s.
route [ 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 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 vval pp : Format.formatter -> 'a t -> unitval pp_re : Format.formatter -> 'a re -> unitmodule Internal : sig ... endInternal types