-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.ml
More file actions
99 lines (81 loc) · 3.35 KB
/
main.ml
File metadata and controls
99 lines (81 loc) · 3.35 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
open Oracle
open Regex
open Bytecode
open Compiler
open Cdn
open Interpreter
open Tojs
open Charclasses
open Flags
open Regs
(* different registers implementations *)
type reg_impl =
| RegArray
| RegList
| RegTree
(* Executing the OCaml linear engine on a regex and a string *)
let input_str = ref ""
let input_regex = ref ""
let str_set = ref false
let rgx_set = ref false
let compare_js = ref false
let reg_implem = ref RegList (* by default, use lists *)
(* fails if the regex is not correct *)
let parse_raw (str:string) : raw_regex =
let r:raw_regex = Regex_parser.main Regex_lexer.token (Lexing.from_string str) in
assert (regex_wf r);
r
(* Appendix example *)
let appendix_regex : raw_regex =
let behind = Raw_lookaround(Lookbehind,Raw_con(raw_char('c'),raw_star(Raw_capture(raw_char('a'))))) in
let ahead = Raw_lookaround(Lookahead,Raw_con(raw_star(raw_char('a')),Raw_con(behind,raw_char('b')))) in
Raw_con(Raw_capture(raw_char('c')),raw_star(Raw_con(raw_char('a'),ahead)))
let appendix_string : string = "caab"
(* choosing the right functions depending on the register implementation *)
let compare (ri:reg_impl) : raw_regex -> string -> bool =
match ri with
| RegArray -> let module INT = Interpreter(Regs.Array_Regs) in
let module CMP = Tojs.Compare(INT) in
CMP.compare_engines
| RegList -> let module INT = Interpreter(Regs.List_Regs) in
let module CMP = Tojs.Compare(INT) in
CMP.compare_engines
| RegTree -> let module INT = Interpreter(Regs.Map_Regs) in
let module CMP = Tojs.Compare(INT) in
CMP.compare_engines
let linear (ri:reg_impl) : raw_regex -> string -> string =
match ri with
| RegArray -> let module INT = Interpreter(Regs.Array_Regs) in
INT.get_linear_result
| RegList -> let module INT = Interpreter(Regs.List_Regs) in
INT.get_linear_result
| RegTree -> let module INT = Interpreter(Regs.Map_Regs) in
INT.get_linear_result
let main =
let speclist =
[("-regex", Arg.Tuple [Arg.Set_string input_regex; Arg.Set rgx_set], "Regex");
("-string", Arg.Tuple [Arg.Set_string input_str; Arg.Set str_set], "String");
("-v", Arg.Set verbose, "Verbose Mode");
("-d", Arg.Set debug, "Debug Mode");
("-cmp", Arg.Set compare_js, "Comparison with the Node engine");
("-array", Arg.Unit (fun _ -> reg_implem := RegArray), "Use Array registers");
("-tree", Arg.Unit (fun _ -> reg_implem := RegTree), "Use Tree registers");
("-list", Arg.Unit (fun _ -> reg_implem := RegList), "Use List registers");
] in
let usage = "./main.native [-regex \"(b)|.*\"] [-string \"abc\"] [-v] [-d] [-cmp]" in
Arg.parse speclist (fun _ -> ()) usage;
(* if no regex or string were provided, ask the user to input them *)
if not !rgx_set then begin
Printf.printf "\027[36mEnter your regex:\027[0m\n";
input_regex := read_line ()
end;
let regex = parse_raw !input_regex in
if !verbose then Printf.printf "\027[33mParsed Regex:\027[0m\n%s\n" (report_raw regex);
if not !str_set then begin
Printf.printf "\027[36mEnter your string:\027[0m\n";
input_str := read_line ()
end;
if !compare_js then
ignore ((compare !reg_implem) regex !input_str)
else
Printf.printf "%s" ((linear !reg_implem) regex !input_str)