From 74b72bb44fdbe1a7548d02682c2b7fa22ef30d93 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sat, 26 Oct 2024 09:32:04 +0100 Subject: [PATCH] test: reproduce #186 (#553) --- lib/posix.ml | 7 ++++ lib/posix.mli | 5 +++ lib_test/expect/test_186.ml | 76 +++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 lib_test/expect/test_186.ml diff --git a/lib/posix.ml b/lib/posix.ml index 517f3266..1a551e00 100644 --- a/lib/posix.ml +++ b/lib/posix.ml @@ -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) diff --git a/lib/posix.mli b/lib/posix.mli index 74173ad8..a2213b0d 100644 --- a/lib/posix.mli +++ b/lib/posix.mli @@ -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 diff --git a/lib_test/expect/test_186.ml b/lib_test/expect/test_186.ml new file mode 100644 index 00000000..a177df6a --- /dev/null +++ b/lib_test/expect/test_186.ml @@ -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) |}] +;;