package ppx_regexp
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=318e6dcfd9b7faa75d5b54f91a44d7a976c95122a0e35a0497db5d790479e806
md5=e98e79d9e72803f69c81282cceabdaaa
Description
This syntax extension turns
match%pcre x with
| {|re1|} -> e1
...
| {|reN|} -> eN
| _ -> e0
into suitable invocations to the ocaml-re library. The patterns are plain
strings of the form accepted by Re_pcre
, except groups can be bound to
variables using the syntax (?<var>...)
. The type of var
will be
string
if a match is of the groups is guaranteed given a match of the
whole pattern, and string option
if the variable is bound to or nested
below an optionally matched group.
Published: 05 Mar 2018
README
ppx_regexp
- Matching Regular Expressions with OCaml Patterns
This syntax extension turns
match%pcre x with
| {|re1|} -> e1
...
| {|reN|} -> eN
| _ -> e0
into suitable invocations of the Re library. The patterns are plain strings of the form accepted by Re_pcre
, with the following additions:
(?<var>...)
defines a group and binds whatever it matches asvar
. The type ofvar
will bestring
if the match is guaranteed given that the whole pattern matches, andstring option
if the variable is bound to or nested below an optionally matched group.?<var>
at the start of a pattern binds group 0 asvar : string
. This may not be the full string if the pattern is unanchored.
A variable is allowed for the universal case and is bound to the matched string. A regular alias is currently not allowed for patterns, since it is not obvious whether is should bind the full string or group 0.
Example
The following prints out times and hosts for SMTP connections to the Postfix daemon:
(* Link with re, re.pcre, lwt, lwt.unix.
Preprocess with ppx_regexp.
Adjust to your OS. *)
open Lwt.Infix
let check_line =
(function%pcre
| {|(?<t>.*:\d\d) .* postfix/smtpd\[[0-9]+\]: connect from (?<host>[a-z0-9.-]+)|} ->
Lwt_io.printlf "%s %s" t host
| _ ->
Lwt.return_unit)
let () = Lwt_main.run begin
Lwt_io.printl "SMTP connections from:" >>= fun () ->
Lwt_stream.iter_s check_line (Lwt_io.lines_of_file "/var/log/syslog")
end
Limitations
No Pattern Guards
Pattern guards are not supported. This is due to the fact that all match cases are combined into a single regular expression, so if one of the patterns succeed, the match is committed before we can check the guard condition.
No Exhaustiveness Check
The syntax extension will always warn if no catch-all case is provided. No exhaustiveness check is attempted. Doing it right would require reimplementing full regular expression parsing and an algorithm which would ideally produce a counter-example.
Bug Reports
The processor is currently new and not well tested. Please break it and file bug reports in the GitHub issue tracker. Any exception raised by generated code except for Match_failure
is a bug.
Dependencies (5)
- ppx_tools_versioned
-
re
>= "1.7.2"
-
ocaml-migrate-parsetree
< "2.0.0"
- jbuilder
-
ocaml
>= "4.02.3"
Dev Dependencies
None
Used by
None
Conflicts
None