@@ -115,6 +115,23 @@ let extract_bindings ~loc p =
115
115
| Re_perl. Parse_error -> error ~loc " Invalid regular expression." );
116
116
(Exp. constant (Const. string re_str), bs, nG)
117
117
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
+
118
135
let transform_cases ~loc cases =
119
136
let aux case =
120
137
if case.pc_guard <> None then
@@ -168,30 +185,13 @@ let transform_cases ~loc cases =
168
185
add_binding (Vb. mk (Pat. var {txt = var; loc}) comp);
169
186
let e_comp = Exp. ident {txt = Lident var; loc} in
170
187
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
188
188
let rec handle_cases i offG = function
189
189
| [] -> [% expr assert false ]
190
190
| (_ , nG , bs , rhs ) :: cases ->
191
191
let e_i = Exp. constant (Const. int i) in
192
192
[% expr
193
193
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]
195
195
else
196
196
[% e handle_cases (i + 1 ) (offG + nG) cases]]
197
197
in
0 commit comments