@@ -26,7 +26,7 @@ module Regexp = struct
26
26
nG', ((res, None , Some (Pipe_all_func func), must_match) :: inner_bs) @ bs
27
27
| Call _ -> Util. error ~loc " (&...) is not implemented for %%pcre and %%mik."
28
28
in
29
- function { Location. txt = Capture_as (idr , _ , e ); _ } -> recurse true e (0 , [ idr, None , None , true ]) | e -> recurse true e (0 , [] )
29
+ function { Location. txt = Capture_as (idr , conv , e ); _ } -> recurse true e (0 , [ idr, None , conv , true ]) | e -> recurse true e (0 , [] )
30
30
31
31
let to_string ~ctx =
32
32
let p_alt, p_seq, p_suffix, p_atom = 0 , 1 , 2 , 3 in
@@ -129,6 +129,12 @@ let rec separate_defaults acc = function
129
129
| ({ pc_lhs = { ppat_desc = Ppat_var _ ; _ } ; _ } as case ) :: rest -> acc, case :: rest
130
130
| case :: rest -> separate_defaults (case :: acc) rest
131
131
132
+ let rec create_opts ~loc = function
133
+ | [] -> [% expr []]
134
+ | `Caseless :: xs -> [% expr `Caseless :: [% e create_opts ~loc xs]]
135
+ | `Anchored :: xs -> [% expr `Anchored :: [% e create_opts ~loc xs]]
136
+ | `Dollar_endonly :: xs -> [% expr `Dollar_endonly :: [% e create_opts ~loc xs]]
137
+
132
138
let extract_bindings ~(parser : ?pos:position -> string -> string Regexp_types.t ) ~ctx ~pos s =
133
139
let r = parser ~pos s in
134
140
let nG, bs = Regexp. bindings r in
@@ -241,7 +247,7 @@ let transform_cases ~mode ~opts ~loc ~ctx cases =
241
247
242
248
let res = pexp_array ~loc @@ List. map (fun (re , _ , _ ) -> re) processed_cases in
243
249
244
- let opts_expr = match opts with [] -> [ % expr []] | [ `Caseless ] -> [ % expr [ `Caseless ]] | _ -> failwith " Unknown option " in
250
+ let opts_expr = create_opts ~loc opts in
245
251
246
252
let comp =
247
253
[% expr
@@ -319,10 +325,23 @@ let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =
319
325
match case.pc_lhs.ppat_desc with
320
326
| Ppat_extension
321
327
( { txt = (" pcre" | " mik" | " pcre_i" | " mik_i" ) as ext; _ },
328
+ (* anchored *)
329
+ PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (pat, str_loc, _)); _ }, _); _ } ] ) ->
330
+ let pos = str_loc.loc_start in
331
+ let mode = if String. starts_with ~prefix: " pcre" ext then `Pcre else `Mik in
332
+ let opts =
333
+ if String. ends_with ~suffix: " _i" ext then `Caseless :: `Anchored :: Util. default_opts else `Anchored :: Util. default_opts
334
+ in
335
+ let parser = match mode with `Pcre -> Regexp. parse_exn ~target: `Match | `Mik -> Regexp. parse_mik_exn ~target: `Match in
336
+ let re, bs, nG = extract_bindings ~parser ~pos ~ctx pat in
337
+ `Mik (opts, re, nG, bs, case.pc_rhs, case.pc_guard)
338
+ | Ppat_extension
339
+ ( { txt = (" pcres" | " miks" | " pcres_i" | " miks_i" ) as ext; _ },
340
+ (* search, non anchored *)
322
341
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (pat, str_loc, _)); _ }, _); _ } ] ) ->
323
342
let pos = str_loc.loc_start in
324
343
let mode = if String. starts_with ~prefix: " pcre" ext then `Pcre else `Mik in
325
- let opts = if String. ends_with ~suffix: " _i" ext then [ `Caseless ] else [] in
344
+ let opts = if String. ends_with ~suffix: " _i" ext then `Caseless :: Util. default_opts else Util. default_opts in
326
345
let parser = match mode with `Pcre -> Regexp. parse_exn ~target: `Match | `Mik -> Regexp. parse_mik_exn ~target: `Match in
327
346
let re, bs, nG = extract_bindings ~parser ~pos ~ctx pat in
328
347
`Mik (opts, re, nG, bs, case.pc_rhs, case.pc_guard)
@@ -344,9 +363,7 @@ let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =
344
363
match case with
345
364
| `Mik (opts , re , _ , _ , _ , _ ) ->
346
365
let comp_var = Util. fresh_var () in
347
- let opts_expr =
348
- match opts with [] -> [% expr []] | [ `Caseless ] -> [% expr [ `Caseless ]] | _ -> failwith " Unknown option"
349
- in
366
+ let opts_expr = create_opts ~loc opts in
350
367
let comp_expr = [% expr Re. compile (Re.Perl. re ~opts: [% e opts_expr] [% e re])] in
351
368
let binding = value_binding ~loc ~pat: (ppat_var ~loc { txt = comp_var; loc }) ~expr: comp_expr in
352
369
Some (i, comp_var, binding)
0 commit comments