Skip to content

Commit 1f6ced2

Browse files
committed
Elaborate README; add a section for ppx_tyre.
1 parent 80a895c commit 1f6ced2

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

README.md

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
[![Build Status](https://travis-ci.org/paurkedal/ppx_regexp.svg?branch=master)](https://travis-ci.org/paurkedal/ppx_compose)
1+
[![Build Status][ci-build-status]][ci]
22

3-
`ppx_regexp` - Matching Regular Expressions with OCaml Patterns
4-
---------------------------------------------------------------
3+
# Two PPXes for Working with Regular Expressions
4+
5+
This repo provides two PPXes providing regular expression-based routing:
6+
7+
- `ppx_regexp` maps to [re][] with the conventional last-match extraction
8+
into `string` and `string option`. It works directly on strings.
9+
- `ppx_tyre` maps to [Tyre][tyre] providing typed extraction into options,
10+
lists, tuples, objects, and polymorphic variants. It defines `Tyre.t` and
11+
`Tyre.route` values.
12+
13+
Another difference is that `ppx_regexp` works directly on strings
14+
essentially hiding the library calls, while `ppx_tyre` provides `Tyre.t` and
15+
`Tyre.route` which can be composed an applied using the Tyre library.
16+
17+
## `ppx_regexp` - Regular Expression Matching with OCaml Patterns
518

619
This syntax extension turns
720
```ocaml
8-
match%pcre x with
21+
function%pcre
922
| {|re1|} -> e1
1023
...
1124
| {|reN|} -> eN
1225
| _ -> e0
1326
```
14-
into suitable invocations of the
15-
[Re library](https://github.com/ocaml/ocaml-re). The patterns are plain
16-
strings of the form accepted by `Re_pcre`, with the following additions:
27+
into suitable invocations of the [Re library][re], and similar for
28+
`match%pcre`. The patterns are plain strings of the form accepted by
29+
`Re_pcre`, with the following additions:
1730

1831
- `(?<var>...)` defines a group and binds whatever it matches as `var`.
1932
The type of `var` will be `string` if the match is guaranteed given that
@@ -27,7 +40,7 @@ A variable is allowed for the universal case and is bound to the matched
2740
string. A regular alias is currently not allowed for patterns, since it is
2841
not obvious whether is should bind the full string or group 0.
2942

30-
## Example
43+
### Example
3144

3245
The following prints out times and hosts for SMTP connections to the Postfix
3346
daemon:
@@ -51,6 +64,35 @@ let () = Lwt_main.run begin
5164
end
5265
```
5366

67+
## `ppx_tyre` - Syntax Support for Tyre Routes
68+
69+
This PPX compiles
70+
```ocaml
71+
[%tyre {|re|}]
72+
```
73+
into `'a Tyre.t` and
74+
```ocaml
75+
function%tyre
76+
| {|re1|} as x1 -> e1
77+
...
78+
| {|reN|} as x2 -> eN
79+
```
80+
into `'a Type.route`, where `re`, `re1`, ... are regular expressions
81+
expressed in a slightly extended subset of PCRE. The interpretations are:
82+
83+
- `re?` extracts an option of what `re` extracts.
84+
- `re+`, `re*`, `re{n,m}` extracts a list of what `re` extracts.
85+
- `(?@qname)` refers to any identifier bound to a typed regular expression
86+
of type `'a Tyre.t`.
87+
- One or more `(?<v>re)` at the top level can be used to bind variables
88+
instead of `as ...`.
89+
- One or more `(?<v>re)` in a sequence extracts an object where each method
90+
`v` is bound to what `re` extracts.
91+
- An alternative with one `(?<v>re)` per branch extracts a polymorphic
92+
variant where each constructor `` `v`` receives what `re` extracts as its
93+
argument.
94+
- `(?&v:qname)` is a shortcut for `(?<v>(?&qname))`.
95+
5496
## Limitations
5597

5698
### No Pattern Guards
@@ -72,3 +114,9 @@ ideally produce a counter-example.
72114
The processor is currently new and not well tested. Please break it and
73115
file bug reports in the GitHub issue tracker. Any exception raised by
74116
generated code except for `Match_failure` is a bug.
117+
118+
119+
[ci]: https://travis-ci.org/paurkedal/ppx_compose
120+
[ci-build-status]: https://travis-ci.org/paurkedal/ppx_regexp.svg?branch=master
121+
[re]: https://github.com/ocaml/ocaml-re
122+
[tyre]: https://github.com/Drup/tyre

ppx_tyre.descr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ expressed in a slightly extended subset of PCRE. The interpretations are:
1818
- `re+`, `re*`, `re{n,m}` extracts a list of what `re` extracts.
1919
- `(?@qname)` refers to any identifier bound to a typed regular expression
2020
of type `'a Tyre.t`.
21+
- One or more `(?<v>re)` at the top level can be used to bind variables
22+
instead of `as ...`.
2123
- One or more `(?<v>re)` in a sequence extracts an object where each method
2224
`v` is bound to what `re` extracts.
2325
- An alternative with one `(?<v>re)` per branch extracts a polymorphic

0 commit comments

Comments
 (0)