1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
open Pcre2
open Printf
let show_array arr =
Array.map string_of_int arr
|> Array.to_list |> String.concat ";" |> sprintf "[|%s|]"
let new_workspace () = Array.make 50 0
let () =
let pat =
if Array.length Sys.argv > 1 then Sys.argv.(1)
else (
eprintf "%s: expected pattern argument\n" Sys.argv.(0);
exit 1)
in
let rex = regexp pat in
let rec find_match flags workspace =
print_string "> ";
let line, eof =
try (read_line (), false) with End_of_file -> ("", true)
in
match pcre2_dfa_match ~rex ~flags ~workspace line with
| res ->
printf "match completed: %S\n" (show_array res);
if not eof then (
printf "\n *input & workspace reset*\n";
find_match [ `PARTIAL_SOFT ] (new_workspace ()))
| exception Error Partial ->
printf "partial match, provide more input:\n";
find_match [ `DFA_RESTART; `PARTIAL_SOFT ] workspace
| exception exn ->
(match exn with
| Not_found -> eprintf "pattern match failed\n"
| Error WorkspaceSize -> eprintf "need larger workspace vector\n"
| Error (InternalError s) -> eprintf "internal error: %s\n" s
| exn -> raise exn);
exit 1
in
find_match [ `PARTIAL_SOFT ] (new_workspace ())
|