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 ]
2
2
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
5
18
6
19
This syntax extension turns
7
20
``` ocaml
8
- match %pcre x with
21
+ function %pcre
9
22
| {|re1|} -> e1
10
23
...
11
24
| {|reN|} -> eN
12
25
| _ -> e0
13
26
```
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:
17
30
18
31
- ` (?<var>...) ` defines a group and binds whatever it matches as ` var ` .
19
32
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
27
40
string. A regular alias is currently not allowed for patterns, since it is
28
41
not obvious whether is should bind the full string or group 0.
29
42
30
- ## Example
43
+ ### Example
31
44
32
45
The following prints out times and hosts for SMTP connections to the Postfix
33
46
daemon:
@@ -51,6 +64,35 @@ let () = Lwt_main.run begin
51
64
end
52
65
```
53
66
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
+
54
96
## Limitations
55
97
56
98
### No Pattern Guards
@@ -72,3 +114,9 @@ ideally produce a counter-example.
72
114
The processor is currently new and not well tested. Please break it and
73
115
file bug reports in the GitHub issue tracker. Any exception raised by
74
116
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
0 commit comments