File: dfa_restart.ml

package info (click to toggle)
pcre-ocaml 7.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 324 kB
  • sloc: ml: 1,445; ansic: 631; makefile: 38
file content (43 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download | duplicates (2)
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
41
42
43
open Pcre
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 begin
      eprintf "%s: expected pattern argument\n" Sys.argv.(0);
      exit 1
    end
  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 pcre_dfa_exec ~rex ~flags ~workspace line with
    | res ->
        printf "match completed: %S\n" (show_array res);
        if not eof then begin
          printf "\n *input & workspace reset*\n";
          find_match [`PARTIAL] (new_workspace ()) 
        end
    | exception (Error Partial) ->
        printf "partial match, provide more input:\n";
        find_match [`DFA_RESTART; `PARTIAL] workspace
    | exception exn ->
        begin 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
        end;
        exit 1
  in
  find_match [`PARTIAL] (new_workspace ())