Skip to content

Commit 40c9fb1

Browse files
committed
optimizations to default rhs case, code cleanup
1 parent 3de6169 commit 40c9fb1

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

ppx_regexp/ppx_regexp.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ let transformation ctx =
5555
let e_ext, acc = super#expression e_ext acc in
5656
let make_transformations ~mode ~opts ~loc = function
5757
| Pexp_function cases ->
58-
let cases, bindings = Transformations.transform_cases ~mode ~opts ~loc ~ctx cases in
59-
[%expr fun _ppx_regexp_v -> [%e cases]], bindings @ acc
58+
let cases, binding = Transformations.transform_cases ~mode ~opts ~loc ~ctx cases in
59+
[%expr fun _ppx_regexp_v -> [%e cases]], binding :: acc
6060
| Pexp_match (e, cases) ->
61-
let cases, bindings = Transformations.transform_cases ~mode ~opts ~loc ~ctx cases in
61+
let cases, binding = Transformations.transform_cases ~mode ~opts ~loc ~ctx cases in
6262
( [%expr
6363
let _ppx_regexp_v = [%e e] in
6464
[%e cases]],
65-
bindings @ acc )
65+
binding :: acc )
6666
| _ -> Util.error ~loc "[%%pcre] and [%%mik] only apply to match, function and global let declarations of strings."
6767
in
6868
match e_ext.pexp_desc with

ppx_regexp/transformations.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ let make_default_rhs ~loc = function
167167
| _ -> case)
168168
default_cases
169169
in
170-
pexp_match ~loc [%expr _ppx_regexp_v] transformed
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
171175

172176
let transform_let ~mode ~ctx =
173177
let parser = match mode with `Pcre -> Regexp.parse_exn ~target:`Let | `Mik -> Regexp.parse_mik_exn ~target:`Let in
@@ -316,7 +320,7 @@ let transform_cases ~mode ~opts ~loc ~ctx cases =
316320
[%expr match [%e dispatchers] with Some result -> result | None -> [%e default_rhs]]
317321
handlers]]
318322
in
319-
[%expr [%e match_expr]], [ re_binding ]
323+
match_expr, re_binding
320324

321325
(* processes each case individually instead of combining them into one RE *)
322326
let transform_mixed_match ~loc ~ctx ?matched_expr cases acc =

0 commit comments

Comments
 (0)