Skip to content

Commit 2f7c29c

Browse files
committed
Support toplevel eval and move REs out of functors.
1 parent ab55d61 commit 2f7c29c

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,3 @@ end
5151
- Is it feasible to cover any reasonable ground with exhaustiveness checks?
5252
No attempt is currently made, and no warning is issued for a missing
5353
catch-all case.
54-
- The extension does not work on top-level evals, but it works if you bind
55-
to a unit value.

ppx_regexp.ml

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ open Longident
2626

2727
let error ~loc msg = raise (Location.Error (Location.error ~loc msg))
2828

29-
let dyn_istop = ref true
3029
let dyn_bindings = ref []
3130
let clear_bindings () = dyn_bindings := []
3231
let add_binding binding = dyn_bindings := binding :: !dyn_bindings
@@ -156,24 +155,19 @@ let rewrite_expr mapper e_ext =
156155
error ~loc "[%pcre] only applies to match an function.")
157156
| _ -> default_mapper.expr mapper e_ext)
158157

159-
let rewrite_value_binding mapper pvb =
160-
let istop = !dyn_istop in
161-
dyn_istop := false;
162-
let pvb = default_mapper.value_binding mapper pvb in
163-
dyn_istop := istop;
164-
if not istop then pvb else
165-
(match get_bindings () with
166-
| [] -> pvb
158+
let rewrite_structure mapper sis =
159+
let sis' =
160+
default_mapper.structure {default_mapper with expr = rewrite_expr} sis
161+
in
162+
(match get_bindings () |> List.rev with
163+
| [] -> sis'
167164
| bindings ->
168165
clear_bindings ();
169-
let e_let = {pexp_desc = Pexp_let (Nonrecursive, bindings, pvb.pvb_expr);
170-
pexp_loc = pvb.pvb_loc; pexp_attributes = []} in
171-
{pvb with pvb_expr = e_let})
172-
173-
let regexp_mapper _config _cookies = {
174-
default_mapper with
175-
value_binding = rewrite_value_binding;
176-
expr = rewrite_expr;
177-
}
166+
let si' = {
167+
pstr_desc = Pstr_value (Nonrecursive, bindings);
168+
pstr_loc = Location.none;
169+
} in
170+
si' :: sis')
178171

179-
let () = Driver.register ~name:"ppx_regexp" ocaml_version regexp_mapper
172+
let () = Driver.register ~name:"ppx_regexp" ocaml_version
173+
(fun _config _cookies -> {default_mapper with structure = rewrite_structure})

test_ppx_regexp.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ let f =
2020
| _ -> None)
2121

2222
let () = assert (f "x: 1" = Some ("x", Some "1"))
23+
24+
module F (M : Map.OrderedType) = struct
25+
let f x =
26+
(match%pcre x with
27+
| {|#(?<space>\s)?(?<comment>.*)|} -> Some (space <> None, comment)
28+
| _ -> None)
29+
end
30+
31+
let r = ref false
32+
;;(match%pcre "" with
33+
| "$^" -> r := true
34+
| _ -> assert false)
35+
;;assert (!r = true)

0 commit comments

Comments
 (0)