@@ -15,7 +15,7 @@ module Regexp = struct
15
15
| Opt e -> recurse false e
16
16
| Repeat ({ Location. txt = i , _ ; _ } , e ) -> recurse (must_match && i > 0 ) e
17
17
| Nongreedy e -> recurse must_match e
18
- | Capture _ -> Util. error ~loc " Unnamed capture is not allowed for %%pcre and %%mik ."
18
+ | Capture _ -> Util. error ~loc " Unnamed capture is not allowed for %%pcre and %%mikmatch ."
19
19
| Capture_as (idr , conv , e ) -> fun (nG , bs ) -> recurse must_match e (nG + 1 , (idr, Some nG, conv, must_match) :: bs)
20
20
| Named_subs (idr , None , conv , e ) | Named_subs (_ , Some idr , conv , e ) ->
21
21
fun (nG , bs ) -> recurse must_match e (nG + 1 , (idr, Some nG, conv, must_match) :: bs)
@@ -24,7 +24,7 @@ module Regexp = struct
24
24
fun (nG , bs ) ->
25
25
let nG', inner_bs = recurse must_match e (nG, [] ) in
26
26
nG', ((res, None , Some (Pipe_all_func func), must_match) :: inner_bs) @ bs
27
- | Call _ -> Util. error ~loc " (&...) is not implemented for %%pcre and %%mik ."
27
+ | Call _ -> Util. error ~loc " (&...) is not implemented for %%pcre and %%mikmatch ."
28
28
in
29
29
function { Location. txt = Capture_as (idr , conv , e ); _ } -> recurse true e (0 , [ idr, None , conv, true ]) | e -> recurse true e (0 , [] )
30
30
@@ -36,7 +36,8 @@ module Regexp = struct
36
36
let content =
37
37
match Util.Ctx. find var_name ctx with
38
38
| Some value -> value
39
- | None -> Util. error ~loc " Variable '%s' not found. %%pcre and %%mik only support global let bindings for substitution." var_name
39
+ | None ->
40
+ Util. error ~loc " Variable '%s' not found. %%pcre and %%mikmatch only support global let bindings for substitution." var_name
40
41
in
41
42
content
42
43
in
@@ -57,7 +58,7 @@ module Regexp = struct
57
58
let j_str = match j_opt with None -> " " | Some j -> string_of_int j in
58
59
delimit_if (p > p_suffix) (Printf. sprintf " %s{%d,%s}" (recurse p_atom e) i j_str)
59
60
| Nongreedy e -> recurse p_suffix e ^ " ?"
60
- | Capture _ -> Util. error ~loc " Unnamed capture is not allowed for %%pcre and %%mik ."
61
+ | Capture _ -> Util. error ~loc " Unnamed capture is not allowed for %%pcre and %%mikmatch ."
61
62
| Capture_as (_ , _ , e ) -> " (" ^ recurse p_alt e ^ " )"
62
63
| Named_subs (idr , _ , _ , _ ) ->
63
64
let content = get_parsed ~loc idr in
@@ -66,7 +67,7 @@ module Regexp = struct
66
67
let content = get_parsed ~loc idr in
67
68
recurse p_atom content
68
69
| Pipe_all (_ , _ , e ) -> recurse p_alt e
69
- | Call _ -> Util. error ~loc " (&...) is not implemented for %%pcre and %%mik ."
70
+ | Call _ -> Util. error ~loc " (&...) is not implemented for %%pcre and %%mikmatch ."
70
71
in
71
72
function { Location. txt = Capture_as (_ , _ , e ); _ } -> recurse 0 e | e -> recurse 0 e
72
73
end
@@ -167,11 +168,9 @@ let make_default_rhs ~loc = function
167
168
| _ -> case)
168
169
default_cases
169
170
in
170
- match transformed with
171
- | [{ pc_lhs = { ppat_desc = Ppat_any ; _ }; pc_guard = None ; pc_rhs; _ }] ->
172
- pc_rhs
173
- | _ ->
174
- pexp_match ~loc [% expr _ppx_regexp_v] transformed
171
+ (match transformed with
172
+ | [ { pc_lhs = { ppat_desc = Ppat_any ; _ }; pc_guard = None ; pc_rhs; _ } ] -> pc_rhs
173
+ | _ -> pexp_match ~loc [% expr _ppx_regexp_v] transformed)
175
174
176
175
let transform_let ~mode ~ctx =
177
176
let parser = match mode with `Pcre -> Regexp. parse_exn ~target: `Let | `Mik -> Regexp. parse_mik_exn ~target: `Let in
@@ -264,7 +263,7 @@ let transform_cases ~mode ~opts ~loc ~ctx cases =
264
263
let re_binding = value_binding ~loc ~pat: (ppat_var ~loc { txt = var; loc }) ~expr: comp in
265
264
let e_comp = pexp_ident ~loc { txt = Lident var; loc } in
266
265
267
- let case_handlers =
266
+ let case_bindings =
268
267
List. mapi
269
268
begin
270
269
fun i (_ , case_group , offG ) ->
@@ -284,41 +283,25 @@ let transform_cases ~mode ~opts ~loc ~ctx cases =
284
283
in
285
284
[% expr fun _g -> [% e mk_guard_chains (List. rev case_group)]]
286
285
in
287
- handler_name, handler_body
286
+ value_binding ~loc ~pat: (ppat_var ~loc { txt = handler_name; loc }) ~expr: handler_body
288
287
end
288
+ processed_cases
289
289
in
290
290
291
- let mk_checks cases_with_offsets =
292
- let indexed = List. mapi (fun i x -> i, x) cases_with_offsets in
293
- List. fold_right
294
- begin
295
- fun (i , _ ) acc ->
296
- let handler_name = Printf. sprintf " _case_%d" i in
297
- [% expr
298
- if Re.Mark. test _g (snd [% e e_comp]).([% e eint ~loc i]) then [% e pexp_ident ~loc { txt = Lident handler_name; loc }] _g
299
- else [% e acc]]
300
- end
301
- indexed [% expr None ]
291
+ let handlers_array =
292
+ pexp_array ~loc @@ List. mapi (fun i _ -> pexp_ident ~loc { txt = Lident (Printf. sprintf " _case_%d" i); loc }) processed_cases
302
293
in
303
294
304
- let handlers = case_handlers processed_cases in
305
- let dispatchers = mk_checks processed_cases in
306
-
307
295
let match_expr =
308
296
[% expr
309
297
match Re. exec_opt (fst [% e e_comp]) _ppx_regexp_v with
310
298
| None -> [% e default_rhs]
311
299
| Some _g ->
312
300
[% e
313
- List. fold_left
314
- begin
315
- fun acc (name , body ) ->
316
- [% expr
317
- let [% p ppat_var ~loc { txt = name; loc }] = [% e body] in
318
- [% e acc]]
319
- end
320
- [% expr match [% e dispatchers] with Some result -> result | None -> [% e default_rhs]]
321
- handlers]]
301
+ pexp_let ~loc Nonrecursive case_bindings
302
+ [% expr
303
+ let handlers = [% e handlers_array] in
304
+ match __ppx_regexp_dispatch (snd [% e e_comp]) handlers _g with Some result -> result | None -> [% e default_rhs]]]]
322
305
in
323
306
match_expr, re_binding
324
307
@@ -341,13 +324,13 @@ let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =
341
324
342
325
let prepared_cases = List. map aux cases in
343
326
344
- let has_mik = List. exists (function `Ext _ -> true | _ -> false ) prepared_cases in
327
+ let has_ext = List. exists (function `Ext _ -> true | _ -> false ) prepared_cases in
345
328
346
- if not has_mik then begin
329
+ if not has_ext then begin
347
330
match matched_expr with None -> pexp_function ~loc cases, acc | Some m -> pexp_match ~loc m cases, acc
348
331
end
349
332
else begin
350
- let mik_compilations =
333
+ let compilations =
351
334
List. mapi
352
335
begin
353
336
fun i case ->
@@ -364,18 +347,18 @@ let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =
364
347
|> List. filter_map (fun x -> x)
365
348
in
366
349
367
- let bindings = List. map (fun (_ , _ , b ) -> b) mik_compilations in
350
+ let bindings = List. map (fun (_ , _ , b ) -> b) compilations in
368
351
369
- let rec build_ordered_match input_var case_idx cases mik_comps =
370
- match cases, mik_comps with
352
+ let rec build_ordered_match input_var case_idx cases comps =
353
+ match cases, comps with
371
354
| [] , _ ->
372
355
(* should not happen if original had catch-all *)
373
356
[% expr raise (Match_failure (" " , 0 , 0 ))]
374
357
| `Regular case :: rest , _ ->
375
358
[% expr
376
359
match [% e input_var] with
377
360
| [% p case.pc_lhs] when [% e Option. value case.pc_guard ~default: [% expr true ]] -> [% e case.pc_rhs]
378
- | _ -> [% e build_ordered_match input_var (case_idx + 1 ) rest mik_comps ]]
361
+ | _ -> [% e build_ordered_match input_var (case_idx + 1 ) rest comps ]]
379
362
| `Ext (_ , _ , _ , bs , rhs , guard ) :: rest , (idx , comp_var , _ ) :: rest_comps when idx = case_idx ->
380
363
let comp_ident = pexp_ident ~loc { txt = Lident comp_var; loc } in
381
364
[% expr
@@ -391,29 +374,18 @@ let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =
391
374
| None -> [% e build_ordered_match input_var (case_idx + 1 ) rest rest_comps]]
392
375
| `Ext _ :: rest , _ ->
393
376
(* shouldn't happen if indices are correct *)
394
- build_ordered_match input_var (case_idx + 1 ) rest mik_comps
377
+ build_ordered_match input_var (case_idx + 1 ) rest comps
395
378
in
396
379
397
- let match_body = build_ordered_match [% expr _ppx_regexp_v] 0 prepared_cases mik_compilations in
380
+ let match_body = build_ordered_match [% expr _ppx_regexp_v] 0 prepared_cases compilations in
398
381
399
382
let match_expr =
400
- let init =
401
- match matched_expr with
402
- | None -> [% expr fun _ppx_regexp_v -> [% e match_body]]
403
- | Some m ->
404
- [% expr
405
- let _ppx_regexp_v = [% e m] in
406
- [% e match_body]]
407
- in
408
- List. fold_left
409
- begin
410
- fun expr binding ->
411
- [% expr
412
- let [% p binding.pvb_pat] = [% e binding.pvb_expr] in
413
- [% e expr]]
414
- end
415
- init bindings
383
+ match matched_expr with
384
+ | None -> [% expr fun _ppx_regexp_v -> [% e match_body]]
385
+ | Some m ->
386
+ [% expr
387
+ let _ppx_regexp_v = [% e m] in
388
+ [% e match_body]]
416
389
in
417
-
418
390
match_expr, bindings @ acc
419
391
end
0 commit comments