Skip to content

Commit ec834d6

Browse files
committed
%mik -> @mikmatch, %miks -> %miksearch
1 parent aad34bb commit ec834d6

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

MIK.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# `%mik` extension
2+
# `%mikmatch` extension
33

44
Accepts `mikmatch` syntax, along with some nice to haves.
55

@@ -68,34 +68,34 @@ Where `PREDEFINED_CLASS` is one of:
6868
## Semantics and Examples
6969
### Variable substitution
7070
```ocaml
71-
let%mik re1 = {| "hello" |}
72-
let%mik re2 = {| re1 "world" |}
71+
let%mikmatch re1 = {| "hello" |}
72+
let%mikmatch re2 = {| re1 "world" |}
7373
74-
let do_something = function%mik
74+
let do_something = function%mikmatch
7575
| {|/ ... (re2) ... /|} -> ...
7676
| _ -> ...
7777
7878
(* will expand to *)
79-
let do_something = function%mik
79+
let do_something = function%mikmatch
8080
| {|/ ... ("hello" "world") ... /|} -> ...
8181
| _ -> ...
8282
```
8383

8484
### Variable capture
8585
```ocaml
86-
let%mik num = {| digit+ |}
86+
let%mikmatch num = {| digit+ |}
8787
88-
let do_something = function%mik
88+
let do_something = function%mikmatch
8989
| {|/ ... (num as n) ... /|} -> ... (* (n : string) available here *)
9090
| _ -> ...
9191
```
9292

9393
Values are also available at the guard level:
9494

9595
```ocaml
96-
let%mik num = {| digit+ |}
96+
let%mikmatch num = {| digit+ |}
9797
98-
let do_something = function%mik
98+
let do_something = function%mikmatch
9999
| {|/ ... (num as n) ... /|} when n = "123" -> ...
100100
| _ -> ...
101101
```
@@ -104,23 +104,23 @@ let do_something = function%mik
104104
It is possible to convert variables to `int` of `float` on the fly:
105105

106106
```ocaml
107-
let%mik num = {| digit+ |}
107+
let%mikmatch num = {| digit+ |}
108108
109-
let do_something = function%mik
109+
let do_something = function%mikmatch
110110
| {|/ 'd' (num as n : int) ... /|} when n = 123 -> ... (* (n : int) available here *)
111111
| {|/ 'f' (num as n : float) ... /|} -> ... (* (n : float) available here *)
112112
| _ -> ...
113113
```
114114

115115
It is also possible to pass the variables into any `string -> 'a` function:
116116
```ocaml
117-
let%mik ip = {| (digit{1-3} '.'){3} digit{1-3}|}
117+
let%mikmatch ip = {| (digit{1-3} '.'){3} digit{1-3}|}
118118
let parse_ip = String.split_on_char '.'
119-
let get_ip = function%mik
119+
let get_ip = function%mikmatch
120120
| {|/ ... (ip as ip := parse_ip) ... /|} -> ... (* (ip : string list) available here *)
121121
| _ -> ...
122122
123-
let get_upper_name = function%mik
123+
let get_upper_name = function%mikmatch
124124
| {|/ ... (['a'-'z'] as name := String.uppercase) ... /|} -> ... (* (name : string) available here *)
125125
| _ -> ...
126126
```
@@ -141,22 +141,22 @@ let mk_example name num mode = match mode with
141141
| Some 'b' -> { name; num; mode = `B}
142142
| Some _ | None -> { name; num; mode = `Default}
143143
144-
let mk_example_re = function%mik
144+
let mk_example_re = function%mikmatch
145145
| {|/ (['a'-'z'] as name := String.capitalize_ascii) ' ' (digit+ as num : int) ' ' ('a'|'b' as mode)? >>> mk_example as res /|} -> (* (res : example) available here, and all other bound variables *)
146146
| _ -> ...
147147
```
148148

149149
## Case Insensitive Match
150150

151-
You can use `%mik_i`: `match%mik_i` and `function%mik_i`. (not available at the variable level)
151+
You can use `%mikmatch_i`: `match%mikmatch_i` and `function%mikmatch_i`. (not available at the variable level)
152152

153153
## Alternatives
154154
### Defining variables
155155
You have a choice between:
156156
```ocaml
157-
let%mik re = {|some regex|}
157+
let%mikmatch re = {|some regex|}
158158
(* and *)
159-
let re = {%mik|some regex|}
159+
let re = {%mikmatch|some regex|}
160160
```
161161

162162
No `/` delimiters are needed here.
@@ -165,7 +165,7 @@ No `/` delimiters are needed here.
165165
#### `match%mik` and `function%mik`
166166

167167
```ocaml
168-
function%mik
168+
function%mikmatch
169169
| {|/ some regex /|} -> ...
170170
| {|/ another regex /|} -> ...
171171
...
@@ -175,23 +175,23 @@ function%mik
175175
This match expression will compile all of the REs in the branches into one, and use marks to find which branch was executed.
176176
Efficient if you have multiple branches.
177177

178-
#### `match%miks` and `function%miks` (search, not anchored)
178+
#### `match%miksearch` and `function%miksearch` (search, not anchored)
179179

180180
The previous extension was **anchored**, meaning, it will only match at the beginning of the string.
181181

182182
This version is not, meaning, for example:
183183

184184
```ocaml
185-
let mik_test = function%mik
185+
let mik_test = function%mikmatch
186186
| {|/ (digit+ as num) /|} -> ...
187187
...
188188
| _ -> failwith "no match"
189189
190190
let () = mik_test "123" ... (* match *)
191191
let () = mik_test "test123" ... (* ERROR: no match *)
192192
193-
(* but, with %miks... *)
194-
let miks_test = function%miks
193+
(* but, with %miksearch... *)
194+
let miks_test = function%miksearch
195195
| {|/ (digit+ as num) /|} -> ...
196196
...
197197
| _ -> failwith "no match"
@@ -200,23 +200,23 @@ let () = miks_test "123" ... (* match *)
200200
let () = miks_test "test123" ... (* match *)
201201
```
202202

203-
Similar for `%miks_i`, except it is case insensitive.
203+
Similar for `%miksearch_i`, except it is case insensitive.
204204

205205
#### General match/function
206206

207207
```ocaml
208208
function
209209
| "some string" -> ...
210-
| {%mik|/ some regex /|} -> ...
210+
| {%mikmatch|/ some regex /|} -> ...
211211
...
212212
| "another string" -> ...
213-
| {%miks|/ some regex /|} -> ... (* non-anchored *)
213+
| {%miksearch|/ some regex /|} -> ... (* non-anchored *)
214214
...
215215
| "yet another string" -> ...
216-
| {%mik_i|/ another regex /|} -> ... (* case insensitive *)
216+
| {%mikmatch_i|/ another regex /|} -> ... (* case insensitive *)
217217
...
218218
| "would you guess it" -> ...
219-
| {%miks_i|/ another regex /|} -> ... (* non-anchored, case insensitive *)
219+
| {%miksearch_i|/ another regex /|} -> ... (* non-anchored, case insensitive *)
220220
| _ -> ...
221221
```
222222

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ The patterns are plain strings of the form accepted by `Re.Pcre`, with the follo
5858
A variable is allowed for the universal case and is bound to the matched
5959
string.
6060

61-
### `%mik`
61+
### `%mikmatch`
6262

63-
Full [%mik guide](./MIK.md)
63+
Full [%mikmatch guide](./MIK.md)
6464

6565
#### Quick Links
6666
- [Variable capture](./MIK.md#variable-capture)

ppx_regexp/ppx_regexp.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let transformation ctx =
2525
method! structure_item item acc =
2626
match item.pstr_desc with
2727
(* let%mik/%pcre x = {|some regex|}*)
28-
| Pstr_extension (({ txt = ("pcre" | "mik") as ext; _ }, PStr [ { pstr_desc = Pstr_value (rec_flag, vbs); _ } ]), _) ->
28+
| Pstr_extension (({ txt = ("pcre" | "mikmatch") as ext; _ }, PStr [ { pstr_desc = Pstr_value (rec_flag, vbs); _ } ]), _) ->
2929
let mode = if ext = "pcre" then `Pcre else `Mik in
3030
let bindings = Transformations.transform_let ~mode ~ctx vbs in
3131
let new_item = { item with pstr_desc = Pstr_value (rec_flag, bindings) } in
@@ -36,7 +36,7 @@ let transformation ctx =
3636
List.fold_left
3737
(fun (vbs_acc, bindings_acc) vb ->
3838
match vb.pvb_expr.pexp_desc with
39-
| Pexp_extension ({ txt = ("pcre" | "mik") as ext; _ }, PStr [ { pstr_desc = Pstr_eval (expr, _); _ } ])
39+
| Pexp_extension ({ txt = ("pcre" | "mikmatch") as ext; _ }, PStr [ { pstr_desc = Pstr_eval (expr, _); _ } ])
4040
when match expr.pexp_desc with Pexp_constant (Pconst_string _) -> true | _ -> false ->
4141
let mode = if ext = "pcre" then `Pcre else `Mik in
4242
let new_vb = { vb with pvb_expr = expr } in
@@ -68,15 +68,17 @@ let transformation ctx =
6868
in
6969
match e_ext.pexp_desc with
7070
(* match%mik/match%pcre and function%mik/function%pcre, anchored *)
71-
| Pexp_extension ({ txt = ("pcre" | "mik" | "pcre_i" | "mik_i") as ext; _ }, PStr [ { pstr_desc = Pstr_eval (e, _); _ } ]) ->
71+
| Pexp_extension ({ txt = ("pcre" | "mikmatch" | "pcre_i" | "mikmatch_i") as ext; _ }, PStr [ { pstr_desc = Pstr_eval (e, _); _ } ])
72+
->
7273
let mode = if String.starts_with ~prefix:"pcre" ext then `Pcre else `Mik in
7374
let opts =
7475
if String.ends_with ~suffix:"_i" ext then `Caseless :: `Anchored :: Util.default_opts else `Anchored :: Util.default_opts
7576
in
7677
let loc = e.pexp_loc in
7778
make_transformations ~mode ~opts ~loc e.pexp_desc
7879
(* match%miks/match%pcres and function%miks/function%pcres, non anchored (search) *)
79-
| Pexp_extension ({ txt = ("pcres" | "miks" | "pcres_i" | "miks_i") as ext; _ }, PStr [ { pstr_desc = Pstr_eval (e, _); _ } ]) ->
80+
| Pexp_extension
81+
({ txt = ("pcres" | "miksearch" | "pcres_i" | "miksearch_i") as ext; _ }, PStr [ { pstr_desc = Pstr_eval (e, _); _ } ]) ->
8082
let mode = if String.starts_with ~prefix:"pcre" ext then `Pcre else `Mik in
8183
let opts = if String.ends_with ~suffix:"_i" ext then `Caseless :: Util.default_opts else Util.default_opts in
8284
let loc = e.pexp_loc in
@@ -88,7 +90,9 @@ let transformation ctx =
8890
begin
8991
fun case ->
9092
match case.pc_lhs.ppat_desc with
91-
| Ppat_extension ({ txt = "pcre" | "pcres" | "mik" | "miks" | "pcre_i" | "pcres_i" | "mik_i" | "miks_i"; _ }, _) -> true
93+
| Ppat_extension
94+
({ txt = "pcre" | "pcres" | "mikmatch" | "miksearch" | "pcre_i" | "pcres_i" | "mikmatch_i" | "miksearch_i"; _ }, _) ->
95+
true
9296
| _ -> false
9397
end
9498
cases
@@ -100,7 +104,9 @@ let transformation ctx =
100104
begin
101105
fun case ->
102106
match case.pc_lhs.ppat_desc with
103-
| Ppat_extension ({ txt = "pcre" | "pcres" | "mik" | "miks" | "pcre_i" | "pcres_i" | "mik_i" | "miks_i"; _ }, _) -> true
107+
| Ppat_extension
108+
({ txt = "pcre" | "pcres" | "mikmatch" | "miksearch" | "pcre_i" | "pcres_i" | "mikmatch_i" | "miksearch_i"; _ }, _) ->
109+
true
104110
| _ -> false
105111
end
106112
cases

ppx_regexp/transformations.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =
324324
let aux case =
325325
match case.pc_lhs.ppat_desc with
326326
| Ppat_extension
327-
( { txt = ("pcre" | "mik" | "pcre_i" | "mik_i") as ext; _ },
327+
( { txt = ("pcre" | "mikmatch" | "pcre_i" | "mikmatch_i") as ext; _ },
328328
(* anchored *)
329329
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (pat, str_loc, _)); _ }, _); _ } ] ) ->
330330
let pos = str_loc.loc_start in
@@ -336,7 +336,7 @@ let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =
336336
let re, bs, nG = extract_bindings ~parser ~pos ~ctx pat in
337337
`Mik (opts, re, nG, bs, case.pc_rhs, case.pc_guard)
338338
| Ppat_extension
339-
( { txt = ("pcres" | "miks" | "pcres_i" | "miks_i") as ext; _ },
339+
( { txt = ("pcres" | "miksearch" | "pcres_i" | "miksearch_i") as ext; _ },
340340
(* search, non anchored *)
341341
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (pat, str_loc, _)); _ }, _); _ } ] ) ->
342342
let pos = str_loc.loc_start in

0 commit comments

Comments
 (0)