Skip to content

Commit a9ed9a8

Browse files
committed
Move out a local function from transform_cases.
1 parent e1a125f commit a9ed9a8

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

ppx_regexp.ml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ let extract_bindings ~loc p =
115115
| Re_perl.Parse_error -> error ~loc "Invalid regular expression.");
116116
(Exp.constant (Const.string re_str), bs, nG)
117117

118+
let rec wrap_group_bindings ~loc rhs offG = function
119+
| [] -> rhs
120+
| (varG, iG, mustG) :: bs ->
121+
let eG = match iG with
122+
| None ->
123+
[%expr Re.Group.get _g 0]
124+
| Some iG ->
125+
[%expr Re.Group.get _g [%e Exp.constant (Const.int (offG + iG + 1))]]
126+
in
127+
let eG =
128+
if mustG then eG else
129+
[%expr try Some [%e eG] with Not_found -> None]
130+
in
131+
[%expr
132+
let [%p Pat.var {txt = varG; loc}] = [%e eG] in
133+
[%e wrap_group_bindings ~loc rhs offG bs]]
134+
118135
let transform_cases ~loc cases =
119136
let aux case =
120137
if case.pc_guard <> None then
@@ -168,30 +185,13 @@ let transform_cases ~loc cases =
168185
add_binding (Vb.mk (Pat.var {txt = var; loc}) comp);
169186
let e_comp = Exp.ident {txt = Lident var; loc} in
170187

171-
let rec wrap_groups rhs offG = function
172-
| [] -> rhs
173-
| (varG, iG, mustG) :: bs ->
174-
let eG = match iG with
175-
| None ->
176-
[%expr Re.Group.get _g 0]
177-
| Some iG ->
178-
[%expr Re.Group.get _g [%e Exp.constant (Const.int (offG + iG + 1))]]
179-
in
180-
let eG =
181-
if mustG then eG else
182-
[%expr try Some [%e eG] with Not_found -> None]
183-
in
184-
[%expr
185-
let [%p Pat.var {txt = varG; loc}] = [%e eG] in
186-
[%e wrap_groups rhs offG bs]]
187-
in
188188
let rec handle_cases i offG = function
189189
| [] -> [%expr assert false]
190190
| (_, nG, bs, rhs) :: cases ->
191191
let e_i = Exp.constant (Const.int i) in
192192
[%expr
193193
if Re.Mark.test _g (snd [%e e_comp]).([%e e_i]) then
194-
[%e wrap_groups rhs offG bs]
194+
[%e wrap_group_bindings ~loc rhs offG bs]
195195
else
196196
[%e handle_cases (i + 1) (offG + nG) cases]]
197197
in

0 commit comments

Comments
 (0)