@@ -41,7 +41,7 @@ let extract_bindings ~loc p =
41
41
let
42
42
rec parse_normal nG stack bs i =
43
43
if i = l then
44
- if stack = [] then bs else
44
+ if stack = [] then (bs, nG) else
45
45
error ~loc " Unmatched start of group."
46
46
else begin
47
47
Buffer. add_char buf p.[i];
@@ -52,7 +52,7 @@ let extract_bindings ~loc p =
52
52
| _ -> parse_normal nG stack bs (i + 1 ))
53
53
end
54
54
and parse_escape nG stack bs i =
55
- if i = l then bs else begin
55
+ if i = l then (bs, nG) else begin
56
56
Buffer. add_char buf p.[i];
57
57
parse_normal nG stack bs (i + 1 )
58
58
end
@@ -81,24 +81,24 @@ let extract_bindings ~loc p =
81
81
in
82
82
parse_normal nG stack' (List. rev_append bs bs') i
83
83
in
84
- let bs = parse_normal 1 [] [] 0 in
85
- (Buffer. contents buf, bs)
84
+ let bs, nG = parse_normal 0 [] [] 0 in
85
+ (Buffer. contents buf, bs, nG )
86
86
87
87
let transform_cases ~loc e cases =
88
88
let aux case =
89
89
if case.pc_guard <> None then
90
90
error ~loc " Guards are not implemented for match%pcre." else
91
91
(match case.pc_lhs with
92
92
| {ppat_desc = Ppat_any } ->
93
- (Exp. constant (Const. string " " ), [] , case.pc_rhs)
93
+ (Exp. constant (Const. string " " ), 0 , [] , case.pc_rhs)
94
94
| {ppat_desc = Ppat_constant (Pconst_string (re_src ,_ )); ppat_loc = loc } ->
95
- let re_str, bs = extract_bindings ~loc re_src in
96
- (Exp. constant (Const. string re_str), bs, case.pc_rhs)
95
+ let re_str, bs, nG = extract_bindings ~loc re_src in
96
+ (Exp. constant (Const. string re_str), nG, bs, case.pc_rhs)
97
97
| {ppat_loc = loc } ->
98
98
error ~loc " Regular expression pattern should be a string." )
99
99
in
100
100
let cases = List. map aux cases in
101
- let res = Exp. array (List. map (fun (re , _ , _ ) -> re) cases) in
101
+ let res = Exp. array (List. map (fun (re , _ , _ , _ ) -> re) cases) in
102
102
let comp = [% expr
103
103
let a = Array. map (fun s -> Re. mark (Re_pcre. re s)) [% e res] in
104
104
let marks = Array. map fst a in
@@ -109,27 +109,29 @@ let transform_cases ~loc e cases =
109
109
add_binding (Vb. mk (Pat. var {txt = var; loc}) comp);
110
110
let e_comp = Exp. ident {txt = Lident var; loc} in
111
111
112
- let rec wrap_groups rhs = function
112
+ let rec wrap_groups rhs offG = function
113
113
| [] -> rhs
114
114
| (varG , iG , mustG ) :: bs ->
115
- let eG = [% expr Re.Group. get _g [% e Exp. constant (Const. int iG)]] in
115
+ let eG =
116
+ [% expr Re.Group. get _g [% e Exp. constant (Const. int (offG + iG + 1 ))]]
117
+ in
116
118
let eG =
117
119
if mustG then eG else
118
120
[% expr try Some [% e eG] with Not_found -> None ]
119
121
in
120
122
[% expr
121
123
let [% p Pat. var {txt = varG; loc}] = [% e eG] in
122
- [% e wrap_groups rhs bs]]
124
+ [% e wrap_groups rhs offG bs]]
123
125
in
124
- let rec handle_cases i = function
126
+ let rec handle_cases i offG = function
125
127
| [] -> [% expr assert false ]
126
- | (_ , bs , rhs ) :: cases ->
128
+ | (_ , nG , bs , rhs ) :: cases ->
127
129
let e_i = Exp. constant (Const. int i) in
128
130
[% expr
129
131
if Re.Mark. test _g (snd [% e e_comp]).([% e e_i]) then
130
- [% e wrap_groups rhs bs]
132
+ [% e wrap_groups rhs offG bs]
131
133
else
132
- [% e handle_cases (i + 1 ) cases]]
134
+ [% e handle_cases (i + 1 ) (offG + nG) cases]]
133
135
in
134
136
let pos = loc.loc_start in
135
137
let e0 = Exp. constant (Const. string pos.pos_fname) in
@@ -140,7 +142,7 @@ let transform_cases ~loc e cases =
140
142
try Re. exec (fst [% e e_comp]) [% e e] with
141
143
Not_found -> raise (Match_failure ([% e e0], [% e e1], [% e e2]))
142
144
in
143
- [% e handle_cases 0 cases]]
145
+ [% e handle_cases 0 0 cases]]
144
146
145
147
let rewrite_expr mapper e_ext =
146
148
(match e_ext.pexp_desc with
0 commit comments