Skip to content

Commit

Permalink
test: reproduce #186
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrinberg committed Oct 25, 2024
1 parent 39f0439 commit 16ef7c1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/posix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,12 @@ let re ?(opts = []) s =
r
;;

let re_result ?opts s =
match re ?opts s with
| s -> Ok s
| exception Not_supported -> Error `Not_supported
| exception Parse_error -> Error `Parse_error
;;

let compile re = Re.compile (Re.longest re)
let compile_pat ?(opts = []) s = compile (re ~opts s)
5 changes: 5 additions & 0 deletions lib/posix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type opt =
(** Parsing of a Posix extended regular expression *)
val re : ?opts:opt list -> string -> Core.t

val re_result
: ?opts:opt list
-> string
-> (Core.t, [ `Not_supported | `Parse_error ]) result

(** [compile r] is defined as [Core.compile (Core.longest r)] *)
val compile : Core.t -> Core.re

Expand Down
76 changes: 76 additions & 0 deletions lib_test/expect/test_186.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
open Import

let print re result =
Printf.printf
"%s: %s\n"
re
(match result with
| Ok _ -> "backward range parsed"
| Error `Parse_error -> "parse error"
| Error `Not_supported -> "not supported")
;;

let cases = [ "[1-0]"; "[5-1]"; "[6-6]"; "[z-a]"; "[b-b]" ]

let test f =
List.iter cases ~f:(fun re ->
let result = f re in
print re result)
;;

let%expect_test "perl" =
test Re.Perl.re_result;
[%expect
{|
[1-0]: backward range parsed
[5-1]: backward range parsed
[6-6]: backward range parsed
[z-a]: backward range parsed
[b-b]: backward range parsed
|}]
;;

let%expect_test "pcre" =
test Re.Pcre.re_result;
[%expect
{|
[1-0]: backward range parsed
[5-1]: backward range parsed
[6-6]: backward range parsed
[z-a]: backward range parsed
[b-b]: backward range parsed
|}]
;;

let%expect_test "posix" =
test Re.Posix.re_result;
[%expect
{|
[1-0]: backward range parsed
[5-1]: backward range parsed
[6-6]: backward range parsed
[z-a]: backward range parsed
[b-b]: backward range parsed
|}]
;;

(* CR-someday rgrinberg: is this correct? *)
let%expect_test "emacs" =
test Re.Emacs.re_result;
[%expect
{|
[1-0]: backward range parsed
[5-1]: backward range parsed
[6-6]: backward range parsed
[z-a]: backward range parsed
[b-b]: backward range parsed
|}]
;;

(* We allow backward ranges in re. We could forbid them? *)
let%expect_test "re" =
Format.printf "%a@." Re.pp (Re.rg '5' '0');
[%expect {| (Set 48-53) |}];
Format.printf "%a@." Re.pp (Re.rg '0' '5');
[%expect {| (Set 48-53) |}]
;;

0 comments on commit 16ef7c1

Please sign in to comment.