You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: MIK.md
+29-29Lines changed: 29 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
2
-
# `%mik` extension
2
+
# `%mikmatch` extension
3
3
4
4
Accepts `mikmatch` syntax, along with some nice to haves.
5
5
@@ -68,34 +68,34 @@ Where `PREDEFINED_CLASS` is one of:
68
68
## Semantics and Examples
69
69
### Variable substitution
70
70
```ocaml
71
-
let%mik re1 = {| "hello" |}
72
-
let%mik re2 = {| re1 "world" |}
71
+
let%mikmatch re1 = {| "hello" |}
72
+
let%mikmatch re2 = {| re1 "world" |}
73
73
74
-
let do_something = function%mik
74
+
let do_something = function%mikmatch
75
75
| {|/ ... (re2) ... /|} -> ...
76
76
| _ -> ...
77
77
78
78
(* will expand to *)
79
-
let do_something = function%mik
79
+
let do_something = function%mikmatch
80
80
| {|/ ... ("hello" "world") ... /|} -> ...
81
81
| _ -> ...
82
82
```
83
83
84
84
### Variable capture
85
85
```ocaml
86
-
let%mik num = {| digit+ |}
86
+
let%mikmatch num = {| digit+ |}
87
87
88
-
let do_something = function%mik
88
+
let do_something = function%mikmatch
89
89
| {|/ ... (num as n) ... /|} -> ... (* (n : string) available here *)
90
90
| _ -> ...
91
91
```
92
92
93
93
Values are also available at the guard level:
94
94
95
95
```ocaml
96
-
let%mik num = {| digit+ |}
96
+
let%mikmatch num = {| digit+ |}
97
97
98
-
let do_something = function%mik
98
+
let do_something = function%mikmatch
99
99
| {|/ ... (num as n) ... /|} when n = "123" -> ...
100
100
| _ -> ...
101
101
```
@@ -104,23 +104,23 @@ let do_something = function%mik
104
104
It is possible to convert variables to `int` of `float` on the fly:
105
105
106
106
```ocaml
107
-
let%mik num = {| digit+ |}
107
+
let%mikmatch num = {| digit+ |}
108
108
109
-
let do_something = function%mik
109
+
let do_something = function%mikmatch
110
110
| {|/ 'd' (num as n : int) ... /|} when n = 123 -> ... (* (n : int) available here *)
111
111
| {|/ 'f' (num as n : float) ... /|} -> ... (* (n : float) available here *)
112
112
| _ -> ...
113
113
```
114
114
115
115
It is also possible to pass the variables into any `string -> 'a` function:
116
116
```ocaml
117
-
let%mik ip = {| (digit{1-3} '.'){3} digit{1-3}|}
117
+
let%mikmatch ip = {| (digit{1-3} '.'){3} digit{1-3}|}
118
118
let parse_ip = String.split_on_char '.'
119
-
let get_ip = function%mik
119
+
let get_ip = function%mikmatch
120
120
| {|/ ... (ip as ip := parse_ip) ... /|} -> ... (* (ip : string list) available here *)
121
121
| _ -> ...
122
122
123
-
let get_upper_name = function%mik
123
+
let get_upper_name = function%mikmatch
124
124
| {|/ ... (['a'-'z'] as name := String.uppercase) ... /|} -> ... (* (name : string) available here *)
125
125
| _ -> ...
126
126
```
@@ -141,31 +141,31 @@ let mk_example name num mode = match mode with
141
141
| Some 'b' -> { name; num; mode = `B}
142
142
| Some _ | None -> { name; num; mode = `Default}
143
143
144
-
let mk_example_re = function%mik
144
+
let mk_example_re = function%mikmatch
145
145
| {|/ (['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 *)
146
146
| _ -> ...
147
147
```
148
148
149
149
## Case Insensitive Match
150
150
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)
152
152
153
153
## Alternatives
154
154
### Defining variables
155
155
You have a choice between:
156
156
```ocaml
157
-
let%mik re = {|some regex|}
157
+
let%mikmatch re = {|some regex|}
158
158
(* and *)
159
-
let re = {%mik|some regex|}
159
+
let re = {%mikmatch|some regex|}
160
160
```
161
161
162
162
No `/` delimiters are needed here.
163
163
164
164
### Matching:
165
-
#### `match%mik` and `function%mik`
165
+
#### `match%mikmatch` and `function%mikmatch`
166
166
167
167
```ocaml
168
-
function%mik
168
+
function%mikmatch
169
169
| {|/ some regex /|} -> ...
170
170
| {|/ another regex /|} -> ...
171
171
...
@@ -175,23 +175,23 @@ function%mik
175
175
This match expression will compile all of the REs in the branches into one, and use marks to find which branch was executed.
176
176
Efficient if you have multiple branches.
177
177
178
-
#### `match%miks` and `function%miks` (search, not anchored)
178
+
#### `match%miksearch` and `function%miksearch` (search, not anchored)
179
179
180
180
The previous extension was **anchored**, meaning, it will only match at the beginning of the string.
181
181
182
182
This version is not, meaning, for example:
183
183
184
184
```ocaml
185
-
let mik_test = function%mik
185
+
let mik_test = function%mikmatch
186
186
| {|/ (digit+ as num) /|} -> ...
187
187
...
188
188
| _ -> failwith "no match"
189
189
190
190
let () = mik_test "123" ... (* match *)
191
191
let () = mik_test "test123" ... (* ERROR: no match *)
192
192
193
-
(* but, with %miks... *)
194
-
let miks_test = function%miks
193
+
(* but, with %miksearch... *)
194
+
let miks_test = function%miksearch
195
195
| {|/ (digit+ as num) /|} -> ...
196
196
...
197
197
| _ -> failwith "no match"
@@ -200,23 +200,23 @@ let () = miks_test "123" ... (* match *)
200
200
let () = miks_test "test123" ... (* match *)
201
201
```
202
202
203
-
Similar for `%miks_i`, except it is case insensitive.
203
+
Similar for `%miksearch_i`, except it is case insensitive.
204
204
205
205
#### General match/function
206
206
207
207
```ocaml
208
208
function
209
209
| "some string" -> ...
210
-
| {%mik|/ some regex /|} -> ...
210
+
| {%mikmatch|/ some regex /|} -> ...
211
211
...
212
212
| "another string" -> ...
213
-
| {%miks|/ some regex /|} -> ... (* non-anchored *)
213
+
| {%miksearch|/ some regex /|} -> ... (* non-anchored *)
214
214
...
215
215
| "yet another string" -> ...
216
-
| {%mik_i|/ another regex /|} -> ... (* case insensitive *)
216
+
| {%mikmatch_i|/ another regex /|} -> ... (* case insensitive *)
217
217
...
218
218
| "would you guess it" -> ...
219
-
| {%miks_i|/ another regex /|} -> ... (* non-anchored, case insensitive *)
219
+
| {%miksearch_i|/ another regex /|} -> ... (* non-anchored, case insensitive *)
0 commit comments