Skip to content

Commit ed71014

Browse files
committed
Support nested repeat operators.
They are not currently valid, but this makes the parser more rigid and future-proof.
1 parent de23c41 commit ed71014

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

ppx_regexp.ml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ let fresh_var =
3535
let c = ref 0 in
3636
fun () -> incr c; Printf.sprintf "_ppx_regexp_%d" !c
3737

38+
let rec must_match p i =
39+
let l = String.length p in
40+
if i = l then true else
41+
if p.[i] = '?' || p.[i] = '*' then false else
42+
if p.[i] = '{' then
43+
let j = String.index_from p (i + 1) '}' in
44+
(match String.split_on_char ',' (String.sub p (i + 1) (j - i - 1)) with
45+
| k :: _ when int_of_string k = 0 -> false
46+
| _ -> must_match p (j + 1))
47+
else
48+
true
49+
3850
let extract_bindings ~loc p =
3951
let l = String.length p in
4052
let buf = Buffer.create l in
@@ -74,10 +86,8 @@ let extract_bindings ~loc p =
7486
(bs, bs', stack'))
7587
in
7688
let bs =
77-
if i < l && (p.[i] = '?' || p.[i] = '*') ||
78-
i + 2 < l && p.[i] = '{' && p.[i + 1] = '0' && p.[i + 2] = ','
79-
then List.map (fun (varG, iG, _) -> (varG, iG, false)) bs
80-
else bs
89+
if must_match p i then bs else
90+
List.map (fun (varG, iG, _) -> (varG, iG, false)) bs
8191
in
8292
parse_normal nG stack' (List.rev_append bs bs') i
8393
in

0 commit comments

Comments
 (0)