From 5304b3118921d8ace346ca78cd8a5af30737b022 Mon Sep 17 00:00:00 2001 From: Rijnard van Tonder Date: Mon, 19 Apr 2021 02:00:00 -0700 Subject: [PATCH] merge rule evaluation (#278) --- lib/app/comby.ml | 7 +- .../configuration/command_configuration.ml | 5 +- .../configuration/command_configuration.mli | 2 +- lib/app/pipeline/pipeline.ml | 23 +-- lib/app/pipeline/pipeline.mli | 1 - lib/kernel/comby_kernel.ml | 6 +- .../language/{alpha_rule.ml => evaluate.ml} | 0 lib/kernel/language/omega_rule.ml | 172 ------------------ lib/kernel/language/rule.ml | 3 +- lib/kernel/language/rule.mli | 3 +- src/main.ml | 2 +- test/common/test_alpha.ml | 5 +- test/common/test_go_alpha.ml | 3 +- test/common/test_go_omega.ml | 3 +- test/common/test_helpers.ml | 52 +++++- test/common/test_match_rule_alpha.ml | 59 ++---- test/common/test_match_rule_omega.ml | 59 ++---- test/common/test_nested_matches_alpha.ml | 23 +-- test/common/test_nested_matches_omega.ml | 25 +-- test/common/test_omega.ml | 5 +- test/common/test_pipeline_alpha.ml | 7 - test/common/test_pipeline_omega.ml | 7 - test/common/test_regex_holes_alpha.ml | 20 +- test/common/test_regex_holes_omega.ml | 20 +- test/common/test_rewrite_rule_alpha.ml | 12 +- test/common/test_rewrite_rule_omega.ml | 12 +- test/common/test_statistics.ml | 2 +- 27 files changed, 127 insertions(+), 411 deletions(-) rename lib/kernel/language/{alpha_rule.ml => evaluate.ml} (100%) delete mode 100644 lib/kernel/language/omega_rule.ml diff --git a/lib/app/comby.ml b/lib/app/comby.ml index 1691150..7fc601c 100644 --- a/lib/app/comby.ml +++ b/lib/app/comby.ml @@ -25,11 +25,12 @@ module Rule = struct type t = Rule.t type result = Rule.result - let sat = Rule.Alpha.sat - let result_env = Rule.Alpha.result_env + let sat = Rule.sat + let result_env = Rule.result_env let create = Rule.create - let apply = Rule.Alpha.apply + let apply = Rule.apply end + type rule = Rule.t module Replacement = Replacement diff --git a/lib/app/configuration/command_configuration.ml b/lib/app/configuration/command_configuration.ml index d806fb0..5cad9ed 100644 --- a/lib/app/configuration/command_configuration.ml +++ b/lib/app/configuration/command_configuration.ml @@ -223,6 +223,7 @@ type user_input_options = ; override_matcher : string option ; regex_pattern : bool ; ripgrep_args : string option + ; omega : bool } type number_of_workers = int @@ -239,7 +240,6 @@ type run_options = ; dump_statistics : bool ; substitute_in_place : bool ; disable_substring_matching : bool - ; omega : bool ; fast_offset_conversion : bool ; match_newline_toplevel : bool ; bound_count : int option @@ -717,8 +717,9 @@ let create ; override_matcher ; regex_pattern ; ripgrep_args + ; omega } - ; run_options = ({ omega; _ } as run_options) + ; run_options ; output_options = ({ overwrite_file_in_place ; color diff --git a/lib/app/configuration/command_configuration.mli b/lib/app/configuration/command_configuration.mli index b5d9748..4b3307f 100644 --- a/lib/app/configuration/command_configuration.mli +++ b/lib/app/configuration/command_configuration.mli @@ -56,6 +56,7 @@ type user_input_options = ; override_matcher : string option ; regex_pattern : bool ; ripgrep_args : string option + ; omega : bool } type compute_mode = @@ -70,7 +71,6 @@ type run_options = ; dump_statistics : bool ; substitute_in_place : bool ; disable_substring_matching : bool - ; omega : bool ; fast_offset_conversion : bool ; match_newline_toplevel : bool ; bound_count : int option diff --git a/lib/app/pipeline/pipeline.ml b/lib/app/pipeline/pipeline.ml index 4ddbf03..c0ea2aa 100644 --- a/lib/app/pipeline/pipeline.ml +++ b/lib/app/pipeline/pipeline.ml @@ -27,25 +27,18 @@ let infer_equality_constraints environment = else acc) -let apply_rule ?(substitute_in_place = true) ?metasyntax matcher omega rule matches = +let apply_rule ?(substitute_in_place = true) ?metasyntax matcher rule matches = let open Option in List.filter_map matches ~f:(fun ({ environment; _ } as matched) -> let rule = rule @ infer_equality_constraints environment in - let apply = - if omega then - Rule.Omega.apply ?metasyntax - else - Rule.Alpha.apply ?metasyntax - in let fresh () = Uuid_unix.(Fn.compose Uuid.to_string create ()) in - let sat, env = apply ~fresh ~substitute_in_place ~matcher rule environment in + let sat, env = Rule.apply ?metasyntax ~fresh ~substitute_in_place ~matcher rule environment in (if sat then env else None) >>| fun environment -> { matched with environment }) let timed_run (module Matcher : Matchers.Matcher.S) ?(fast_offset_conversion = false) - ?(omega = false) ?substitute_in_place ?metasyntax ~configuration @@ -56,9 +49,9 @@ let timed_run | Some template -> Matcher.set_rewrite_template template; | None -> ()); let rule = Option.value rule ~default:[Ast.True] in - let options = if omega then Rule.Omega.options rule else Rule.Alpha.options rule in + let options = Rule.options rule in let matches = Matcher.all ~nested:options.nested ~configuration ~template ~source () in - let matches = apply_rule ?substitute_in_place ?metasyntax (module Matcher) omega rule matches in + let matches = apply_rule ?substitute_in_place ?metasyntax (module Matcher) rule matches in List.map matches ~f:(Match.convert_offset ~fast:fast_offset_conversion ~source) type output = @@ -80,7 +73,6 @@ let log_to_file path = let process_single_source matcher - ?(omega = false) ?(fast_offset_conversion = false) ?(substitute_in_place = false) ?(verbose = false) @@ -105,7 +97,6 @@ let process_single_source matcher ?metasyntax ~substitute_in_place - ~omega ~fast_offset_conversion ~configuration ~specification @@ -237,7 +228,6 @@ let run_batch ~f:per_unit sources compute_mode bound_count = let run_interactive specifications matcher - omega fast_offset_conversion substitute_in_place match_configuration @@ -252,7 +242,6 @@ let run_interactive (fun (input : single_source) specification -> process_single_source matcher - ~omega ~fast_offset_conversion ~substitute_in_place ~verbose @@ -287,7 +276,6 @@ let run ; dump_statistics ; substitute_in_place ; disable_substring_matching - ; omega ; fast_offset_conversion ; match_newline_toplevel ; bound_count @@ -321,7 +309,6 @@ let run (fun input specification -> process_single_source matcher - ~omega ~fast_offset_conversion ~substitute_in_place ~verbose @@ -345,7 +332,6 @@ let run run_interactive specifications matcher - omega fast_offset_conversion substitute_in_place match_configuration @@ -368,7 +354,6 @@ let execute specification = process_single_source matcher - ~omega:false ~fast_offset_conversion:false ?substitute_in_place ~verbose:false diff --git a/lib/app/pipeline/pipeline.mli b/lib/app/pipeline/pipeline.mli index d0f7e74..c41a6f2 100644 --- a/lib/app/pipeline/pipeline.mli +++ b/lib/app/pipeline/pipeline.mli @@ -8,7 +8,6 @@ type output = val process_single_source : (module Matchers.Matcher.S) - -> ?omega:bool -> ?fast_offset_conversion:bool -> ?substitute_in_place:bool -> ?verbose:bool diff --git a/lib/kernel/comby_kernel.ml b/lib/kernel/comby_kernel.ml index 0fc5e17..b386e5b 100644 --- a/lib/kernel/comby_kernel.ml +++ b/lib/kernel/comby_kernel.ml @@ -25,10 +25,10 @@ module Rule = struct type t = Rule.t type result = Rule.result - let sat = Rule.Alpha.sat - let result_env = Rule.Alpha.result_env + let sat = Rule.sat + let result_env = Rule.result_env let create = Rule.create - let apply = Rule.Alpha.apply + let apply = Rule.apply end type rule = Rule.t diff --git a/lib/kernel/language/alpha_rule.ml b/lib/kernel/language/evaluate.ml similarity index 100% rename from lib/kernel/language/alpha_rule.ml rename to lib/kernel/language/evaluate.ml diff --git a/lib/kernel/language/omega_rule.ml b/lib/kernel/language/omega_rule.ml deleted file mode 100644 index b0b5fe1..0000000 --- a/lib/kernel/language/omega_rule.ml +++ /dev/null @@ -1,172 +0,0 @@ -open Core_kernel -open Angstrom - -open Ast - -open Match -open Rewriter - - -module Configuration = Matchers.Configuration - -type t = Ast.t - -type result = bool * environment option - -type options = Options.t - -let options = Options.of_rule - -let (|>>) p f = - p >>= fun x -> return (f x) - -let sat = fst - -let result_env = snd - -let match_configuration_of_syntax template = - (* decide match configuration based on whether there are holes *) - let antecedent_contains_hole_syntax case = - String.is_substring case ~substring:Syntax.variable_left_delimiter - in - if antecedent_contains_hole_syntax template then - Configuration.create ~match_kind:Fuzzy () - else - Configuration.create ~match_kind:Exact () - -let merge_match_environments matches environment' = - List.map matches ~f:(fun { environment; _ } -> - Environment.merge environment environment') - -type rewrite_context = - { variable : string } - -let counter = - let uuid_for_id_counter = ref 0 in - fun () -> - uuid_for_id_counter := !uuid_for_id_counter + 1; - Format.sprintf "gu3ssme_%012d" !uuid_for_id_counter - -let rec apply - ?(matcher = (module Matchers.Omega.Generic : Matchers.Matcher.S)) - ?(substitute_in_place = true) - ?(fresh = counter) - ?metasyntax - predicates - env = - let open Option in - let (module Matcher) = matcher in - - let equal_in_environment var value env = - match Environment.lookup env var with - | None -> false, Some env - | Some var_value -> String.equal var_value value, Some env - in - (* accepts only one expression *) - let rec rule_match ?(rewrite_context : rewrite_context option) env = - function - | True -> true, Some env - | False -> false, Some env - | Option _ -> true, Some env - | Equal (Variable var, String value) - | Equal (String value, Variable var) -> - equal_in_environment var value env - | Equal (String left, String right) -> - String.equal left right, Some env - | Equal (Variable left, Variable right) -> - let result = - Environment.lookup env left >>= fun left -> - Environment.lookup env right >>= fun right -> - return (String.equal left right) - in - Option.value result ~default:false, Some env - | Not_equal (left, right) -> - let sat, env = rule_match env (Equal (left, right)) in - not sat, env - | Match (Variable variable, cases) -> - let result = - Environment.lookup env variable >>= fun source -> - List.find_map cases ~f:(fun (template, case_expression) -> - match template with - | String template -> - begin - let configuration = match_configuration_of_syntax template in - Matcher.all ~configuration ~template ~source () |> function - | [] -> None - | matches -> - (* merge environments. overwrite behavior is undefined *) - let fold_matches (sat, out) { environment; _ } = - let fold_cases (sat, out) predicate = - if sat then - let env' = Environment.merge env environment in - rule_match ?rewrite_context env' predicate - else - (sat, out) - in - List.fold case_expression ~init:(sat, out) ~f:fold_cases - in - List.fold matches ~init:(true, None) ~f:fold_matches - |> Option.some - end - | Variable _ -> - failwith "| :[hole] is invalid. Maybe you meant to put quotes") - in - Option.value_map result ~f:ident ~default:(false, Some env) - | Match (String template, cases) -> - let source, _ = Rewriter.Rewrite_template.substitute ?metasyntax template env in - let fresh_var = fresh () in - let env = Environment.add env fresh_var source in - rule_match env (Match (Variable fresh_var, cases)) - | RewriteTemplate rewrite_template -> - begin - match rewrite_context with - | None -> false, None - | Some { variable; _ } -> - (* FIXME(RVT) assumes only contextual rewrite for now. *) - let env = - Rewrite_template.substitute ?metasyntax rewrite_template env - |> fst - |> fun replacement' -> - Environment.update env variable replacement' - |> Option.some - in - true, env - end - | Rewrite (Variable variable, (match_template, rewrite_expression)) -> - begin match rewrite_expression with - | RewriteTemplate rewrite_template -> - let template = - match match_template with - | Variable _ -> failwith "Invalid syntax in rewrite LHS" - | String template -> template - in - let result = - Environment.lookup env variable >>= fun source -> - let configuration = Configuration.create ~match_kind:Fuzzy () in - let matches = Matcher.all ~configuration ~template ~source () in - let source = if substitute_in_place then Some source else None in - let result = Rewrite.all ?metasyntax ?source ~rewrite_template matches in - match result with - | Some { rewritten_source; _ } -> - (* substitute for variables that are in the outside scope *) - let rewritten_source, _ = Rewrite_template.substitute rewritten_source env in - let env = Environment.update env variable rewritten_source in - return (true, Some env) - | None -> - return (true, Some env) - in - Option.value_map result ~f:ident ~default:(false, Some env) - | _ -> failwith "Not implemented yet" - end - | Rewrite _ -> failwith "TODO/Invalid: Have not decided whether rewrite \":[x]\" is useful." - in - List.fold predicates ~init:(true, None) ~f:(fun (sat, out) predicate -> - if sat then - let env = - Option.value_map out - ~f:(fun out -> Environment.merge out env) - ~default:env - in - rule_match env predicate - else - (sat, out)) diff --git a/lib/kernel/language/rule.ml b/lib/kernel/language/rule.ml index fed748f..0c0a4a7 100644 --- a/lib/kernel/language/rule.ml +++ b/lib/kernel/language/rule.ml @@ -2,5 +2,4 @@ include Types let create = Parser.create -module Alpha = Alpha_rule -module Omega = Omega_rule +include Evaluate diff --git a/lib/kernel/language/rule.mli b/lib/kernel/language/rule.mli index d0c91a6..d4fe7a8 100644 --- a/lib/kernel/language/rule.mli +++ b/lib/kernel/language/rule.mli @@ -4,5 +4,4 @@ include module type of Types val create : string -> t Or_error.t -module Alpha : Engine -module Omega : Engine +include Engine diff --git a/src/main.ml b/src/main.ml index 274cb97..5fda937 100644 --- a/src/main.ml +++ b/src/main.ml @@ -227,6 +227,7 @@ let base_command_parameters : (unit -> 'result) Command.Param.t = ; override_matcher ; regex_pattern ; ripgrep_args + ; omega } ; run_options = { verbose @@ -234,7 +235,6 @@ let base_command_parameters : (unit -> 'result) Command.Param.t = ; dump_statistics ; substitute_in_place ; disable_substring_matching - ; omega ; fast_offset_conversion ; match_newline_toplevel ; bound_count diff --git a/test/common/test_alpha.ml b/test/common/test_alpha.ml index b668f13..3010636 100644 --- a/test/common/test_alpha.ml +++ b/test/common/test_alpha.ml @@ -1,5 +1,2 @@ -open Language - include Matchers.Alpha -let create = Language.Rule.create -module Rule = Rule.Alpha +module Rule = Language.Rule diff --git a/test/common/test_go_alpha.ml b/test/common/test_go_alpha.ml index ccb67c5..ec19e15 100644 --- a/test/common/test_go_alpha.ml +++ b/test/common/test_go_alpha.ml @@ -15,7 +15,8 @@ let run ?(rule = "where true") source match_template rewrite_template = if Rule.(sat @@ apply rule environment) then Rewrite.all ~source ~rewrite_template [result] |> (fun x -> Option.value_exn x) - |> (fun { rewritten_source; _ } -> rewritten_source) |> print_string + |> (fun { rewritten_source; _ } -> rewritten_source) + |> print_string else assert false | Error _ -> diff --git a/test/common/test_go_omega.ml b/test/common/test_go_omega.ml index dbe6c38..a8d26b4 100644 --- a/test/common/test_go_omega.ml +++ b/test/common/test_go_omega.ml @@ -15,7 +15,8 @@ let run ?(rule = "where true") source match_template rewrite_template = if Rule.(sat @@ apply rule environment) then Rewrite.all ~source ~rewrite_template [result] |> (fun x -> Option.value_exn x) - |> (fun { rewritten_source; _ } -> rewritten_source) |> print_string + |> (fun { rewritten_source; _ } -> rewritten_source) + |> print_string else assert false | Error _ -> diff --git a/test/common/test_helpers.ml b/test/common/test_helpers.ml index abded6e..c68d6d6 100644 --- a/test/common/test_helpers.ml +++ b/test/common/test_helpers.ml @@ -1,7 +1,8 @@ open Core -open Matchers -let configuration = Configuration.create ~match_kind:Fuzzy () +open Rewriter + +let configuration = Matchers.Configuration.create ~match_kind:Fuzzy () let format s = let s = String.chop_prefix_exn ~prefix:"\n" s in @@ -23,3 +24,50 @@ let print_only_match matches = |> (fun matches -> `List matches) |> Yojson.Safe.pretty_to_string |> print_string + +let run ?(configuration = configuration) (module M : Matchers.Matcher.S) source match_template ?rule rewrite_template = + let rule = + match rule with + | Some rule -> Language.Rule.create rule |> Or_error.ok_exn + | None -> Language.Rule.create "where true" |> Or_error.ok_exn + in + M.all ~configuration ~template:match_template ~source () + |> List.filter ~f:(fun { Match.environment; _ } -> Language.Rule.(sat @@ apply rule environment)) + |> function + | [] -> print_string "No matches." + | results -> + Option.value_exn (Rewrite.all ~source ~rewrite_template results) + |> (fun { rewritten_source; _ } -> rewritten_source) + |> print_string + +let run_nested + (module M : Matchers.Matcher.S) + ?(configuration = configuration) + ?rule + source + match_template + () = + let nested = + match rule with + | None -> true + | Some rule -> + let options = Language.Rule.create rule |> Or_error.ok_exn |> Language.Rule.options in + options.nested + in + M.all ~configuration ~nested ~template:match_template ~source () + |> function + | [] -> print_string "No matches." + | matches -> + let matches = List.map matches ~f:(Match.convert_offset ~fast:true ~source) in + Format.asprintf "%a" Match.pp (None, matches) + |> print_string + +(** Rule tests *) +let sat ?(env = Match.Environment.create ()) rule = + let rule = Language.Rule.create rule |> Or_error.ok_exn in + Format.sprintf "%b" (Language.Rule.(sat @@ apply rule env)) + +let make_env bindings = + List.fold bindings + ~init:(Match.Environment.create ()) + ~f:(fun env (var, value) -> Match.Environment.add env var value) diff --git a/test/common/test_match_rule_alpha.ml b/test/common/test_match_rule_alpha.ml index 9ece4a7..b50df3e 100644 --- a/test/common/test_match_rule_alpha.ml +++ b/test/common/test_match_rule_alpha.ml @@ -1,21 +1,11 @@ open Core -open Matchers open Match open Test_helpers include Test_alpha -let sat ?(env = Environment.create ()) rule = - let rule = create rule |> Or_error.ok_exn in - Format.sprintf "%b" (Rule.(sat @@ apply rule env)) - -let make_env bindings = - List.fold bindings - ~init:(Environment.create ()) - ~f:(fun env (var, value) -> Environment.add env var value) - let%expect_test "rule_sat" = let rule = {| where "x" != "y" |} in sat rule |> print_string; @@ -60,19 +50,6 @@ let%expect_test "rule_sat_with_env" = sat ~env rule |> print_string; [%expect_exact {|false|}] -let configuration = Configuration.create ~match_kind:Fuzzy () - -let format s = - let s = s |> String.chop_prefix_exn ~prefix:"\n" in - let leading_indentation = - Option.value_exn (String.lfindi s ~f:(fun _ c -> not (Char.equal c ' '))) in - s - |> String.split ~on:'\n' - |> List.map ~f:(Fn.flip String.drop_prefix leading_indentation) - |> String.concat ~sep:"\n" - |> String.chop_suffix_exn ~suffix:"\n" - - let%expect_test "where_true" = let template = {| @@ -91,7 +68,7 @@ let%expect_test "where_true" = let rule = {| where true |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -140,7 +117,7 @@ let%expect_test "match_sat" = | ":[_],:[_]" -> false } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -156,7 +133,7 @@ let%expect_test "match_sat" = | ":[_],:[_]" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -199,7 +176,7 @@ let%expect_test "match_sat" = | ":[_]" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -235,7 +212,7 @@ let%expect_test "match_sat" = | ":[_]" -> :[1] == "a" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -270,7 +247,7 @@ let%expect_test "match_sat" = | ":[_]" -> :[1] == "b" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -289,7 +266,7 @@ let%expect_test "match_s_suffix" = let rule = {| where true |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -325,7 +302,7 @@ let%expect_test "match_s_suffix" = let rule = {| where true |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -363,7 +340,7 @@ let%expect_test "configuration_choice_based_on_case" = | "ame" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -382,7 +359,7 @@ let%expect_test "configuration_choice_based_on_case" = | "names" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -419,7 +396,7 @@ let%expect_test "configuration_choice_based_on_case" = | "names" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -440,7 +417,7 @@ let%expect_test "match_using_environment_merge" = let rule = {| where match :[1] { | "{ :[x] : :[y] }" -> :[x] == :[y] } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -475,7 +452,7 @@ let%expect_test "match_using_environment_merge" = let rule = {| where match :[1] { | "{ :[x] : :[y] }" -> :[x] == :[y] } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -500,7 +477,7 @@ let%expect_test "nested_matches" = } } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -541,7 +518,7 @@ let%expect_test "nested_matches" = } } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -562,7 +539,7 @@ let%expect_test "match_on_template" = | "poodles" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -599,7 +576,7 @@ let%expect_test "match_on_template" = | "poodles" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -636,7 +613,7 @@ let%expect_test "match_on_template" = | "poodle" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in diff --git a/test/common/test_match_rule_omega.ml b/test/common/test_match_rule_omega.ml index 7746399..29e439e 100644 --- a/test/common/test_match_rule_omega.ml +++ b/test/common/test_match_rule_omega.ml @@ -1,21 +1,11 @@ open Core -open Matchers open Match open Test_helpers include Test_omega -let sat ?(env = Environment.create ()) rule = - let rule = create rule |> Or_error.ok_exn in - Format.sprintf "%b" (Rule.(sat @@ apply rule env)) - -let make_env bindings = - List.fold bindings - ~init:(Environment.create ()) - ~f:(fun env (var, value) -> Environment.add env var value) - let%expect_test "rule_sat" = let rule = {| where "x" != "y" |} in sat rule |> print_string; @@ -60,19 +50,6 @@ let%expect_test "rule_sat_with_env" = sat ~env rule |> print_string; [%expect_exact {|false|}] -let configuration = Configuration.create ~match_kind:Fuzzy () - -let format s = - let s = s |> String.chop_prefix_exn ~prefix:"\n" in - let leading_indentation = - Option.value_exn (String.lfindi s ~f:(fun _ c -> not (Char.equal c ' '))) in - s - |> String.split ~on:'\n' - |> List.map ~f:(Fn.flip String.drop_prefix leading_indentation) - |> String.concat ~sep:"\n" - |> String.chop_suffix_exn ~suffix:"\n" - - let%expect_test "where_true" = let template = {| @@ -91,7 +68,7 @@ let%expect_test "where_true" = let rule = {| where true |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -140,7 +117,7 @@ let%expect_test "match_sat" = | ":[_],:[_]" -> false } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -156,7 +133,7 @@ let%expect_test "match_sat" = | ":[_],:[_]" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -199,7 +176,7 @@ let%expect_test "match_sat" = | ":[_]" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -235,7 +212,7 @@ let%expect_test "match_sat" = | ":[_]" -> :[1] == "a" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -270,7 +247,7 @@ let%expect_test "match_sat" = | ":[_]" -> :[1] == "b" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -289,7 +266,7 @@ let%expect_test "match_s_suffix" = let rule = {| where true |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -325,7 +302,7 @@ let%expect_test "match_s_suffix" = let rule = {| where true |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -363,7 +340,7 @@ let%expect_test "configuration_choice_based_on_case" = | "ame" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -382,7 +359,7 @@ let%expect_test "configuration_choice_based_on_case" = | "names" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -419,7 +396,7 @@ let%expect_test "configuration_choice_based_on_case" = | "names" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -440,7 +417,7 @@ let%expect_test "match_using_environment_merge" = let rule = {| where match :[1] { | "{ :[x] : :[y] }" -> :[x] == :[y] } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -475,7 +452,7 @@ let%expect_test "match_using_environment_merge" = let rule = {| where match :[1] { | "{ :[x] : :[y] }" -> :[x] == :[y] } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -500,7 +477,7 @@ let%expect_test "nested_matches" = } } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -541,7 +518,7 @@ let%expect_test "nested_matches" = } } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -562,7 +539,7 @@ let%expect_test "match_on_template" = | "poodles" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -599,7 +576,7 @@ let%expect_test "match_on_template" = | "poodles" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -636,7 +613,7 @@ let%expect_test "match_on_template" = | "poodle" -> true } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in diff --git a/test/common/test_nested_matches_alpha.ml b/test/common/test_nested_matches_alpha.ml index e4549df..86befc7 100644 --- a/test/common/test_nested_matches_alpha.ml +++ b/test/common/test_nested_matches_alpha.ml @@ -4,28 +4,7 @@ open Test_helpers include Test_alpha -let run - ?(m = (module Generic : Matchers.Matcher.S)) - ?(configuration = configuration) - ?rule - source - match_template - () = - let (module M) = m in - let nested = - match rule with - | None -> true - | Some rule -> - let options = create rule |> Or_error.ok_exn |> Rule.options in - options.nested - in - M.all ~configuration ~nested ~template:match_template ~source () - |> function - | [] -> print_string "No matches." - | matches -> - let matches = List.map matches ~f:(Match.convert_offset ~fast:true ~source) in - Format.asprintf "%a" Match.pp (None, matches) - |> print_string +let run = run_nested (module Matchers.Alpha.Generic) let%expect_test "nested_matches" = let source = {| diff --git a/test/common/test_nested_matches_omega.ml b/test/common/test_nested_matches_omega.ml index 8e5e73f..b9c4e65 100644 --- a/test/common/test_nested_matches_omega.ml +++ b/test/common/test_nested_matches_omega.ml @@ -2,30 +2,7 @@ open Core open Test_helpers -include Test_omega - -let run - ?(m = (module Generic : Matchers.Matcher.S)) - ?(configuration = configuration) - ?rule - source - match_template - () = - let (module M) = m in - let nested = - match rule with - | None -> true - | Some rule -> - let options = create rule |> Or_error.ok_exn |> Rule.options in - options.nested - in - M.all ~configuration ~nested ~template:match_template ~source () - |> function - | [] -> print_string "No matches." - | matches -> - let matches = List.map matches ~f:(Match.convert_offset ~fast:true ~source) in - Format.asprintf "%a" Match.pp (None, matches) - |> print_string +let run = run_nested (module Matchers.Omega.Generic) let%expect_test "nested_matches" = let source = {| diff --git a/test/common/test_omega.ml b/test/common/test_omega.ml index 0bb0459..721bfda 100644 --- a/test/common/test_omega.ml +++ b/test/common/test_omega.ml @@ -1,5 +1,2 @@ -open Language - include Matchers.Omega -let create = Language.Rule.create -module Rule = Rule.Omega +module Rule = Language.Rule diff --git a/test/common/test_pipeline_alpha.ml b/test/common/test_pipeline_alpha.ml index 47d90ab..598346f 100644 --- a/test/common/test_pipeline_alpha.ml +++ b/test/common/test_pipeline_alpha.ml @@ -13,7 +13,6 @@ let configuration = ; dump_statistics = false ; substitute_in_place = true ; disable_substring_matching = false - ; omega = false ; fast_offset_conversion = false ; match_newline_toplevel = false ; bound_count = None @@ -25,12 +24,6 @@ let configuration = ; metasyntax = None } -let configuration = - { configuration with - run_options = - { configuration.run_options with omega = false } - } - (* TODO restore this, can't access the Parallel_hack module *) (* let%expect_test "interactive_paths" = diff --git a/test/common/test_pipeline_omega.ml b/test/common/test_pipeline_omega.ml index da39bda..6ac202a 100644 --- a/test/common/test_pipeline_omega.ml +++ b/test/common/test_pipeline_omega.ml @@ -13,7 +13,6 @@ let configuration = ; dump_statistics = false ; substitute_in_place = true ; disable_substring_matching = false - ; omega = false ; fast_offset_conversion = false ; match_newline_toplevel = false ; bound_count = None @@ -25,12 +24,6 @@ let configuration = ; metasyntax = None } -let configuration = - { configuration with - run_options = - { configuration.run_options with omega = true } - } - (* TODO restore this, can't access the Parallel_hack module *) (* let%expect_test "interactive_paths" = diff --git a/test/common/test_regex_holes_alpha.ml b/test/common/test_regex_holes_alpha.ml index faba212..dc5757c 100644 --- a/test/common/test_regex_holes_alpha.ml +++ b/test/common/test_regex_holes_alpha.ml @@ -1,26 +1,8 @@ open Core -open Rewriter - +open Test_helpers open Matchers.Alpha -let configuration = Matchers.Configuration.create ~match_kind:Fuzzy () - -let run ?(configuration = configuration) (module M : Matchers.Matcher.S) source match_template ?rule rewrite_template = - let rule = - match rule with - | Some rule -> Language.Rule.create rule |> Or_error.ok_exn - | None -> Language.Rule.create "where true" |> Or_error.ok_exn - in - M.all ~configuration ~template:match_template ~source () - |> List.filter ~f:(fun { Match.environment; _ } -> Language.Rule.Alpha.(sat @@ apply rule environment)) - |> function - | [] -> print_string "No matches." - | results -> - Option.value_exn (Rewrite.all ~source ~rewrite_template results) - |> (fun { rewritten_source; _ } -> rewritten_source) - |> print_string - let%expect_test "regex_holes_simple" = let source = {|foo|} in let match_template = {|:[x~\w+]|} in diff --git a/test/common/test_regex_holes_omega.ml b/test/common/test_regex_holes_omega.ml index 988284a..dcfd128 100644 --- a/test/common/test_regex_holes_omega.ml +++ b/test/common/test_regex_holes_omega.ml @@ -1,26 +1,8 @@ open Core -open Rewriter - +open Test_helpers open Matchers.Omega -let configuration = Matchers.Configuration.create ~match_kind:Fuzzy () - -let run ?(configuration = configuration) (module M : Matchers.Matcher.S) source match_template ?rule rewrite_template = - let rule = - match rule with - | Some rule -> Language.Rule.create rule |> Or_error.ok_exn - | None -> Language.Rule.create "where true" |> Or_error.ok_exn - in - M.all ~configuration ~template:match_template ~source () - |> List.filter ~f:(fun { Match.environment; _ } -> Language.Rule.Alpha.(sat @@ apply rule environment)) - |> function - | [] -> print_string "No matches." - | results -> - Option.value_exn (Rewrite.all ~source ~rewrite_template results) - |> (fun { rewritten_source; _ } -> rewritten_source) - |> print_string - let%expect_test "regex_holes_simple" = let source = {|foo|} in let match_template = {|:[x~\w+]|} in diff --git a/test/common/test_rewrite_rule_alpha.ml b/test/common/test_rewrite_rule_alpha.ml index b0c8ee6..47b7e03 100644 --- a/test/common/test_rewrite_rule_alpha.ml +++ b/test/common/test_rewrite_rule_alpha.ml @@ -34,7 +34,7 @@ let%expect_test "rewrite_rule" = {| where rewrite :[1] { "int" -> "expect" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -52,7 +52,7 @@ let%expect_test "sequenced_rewrite_rule" = rewrite :[a] { "a" -> "qqq" }, rewrite :[rest] { "{ b : { :[other] } }" -> "{ :[other] }" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -68,7 +68,7 @@ let%expect_test "rewrite_rule_for_list" = {| where rewrite :[contents] { ":[[x]]," -> ":[[x]];" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -84,7 +84,7 @@ let%expect_test "rewrite_rule_for_list_strip_last" = {| where rewrite :[contents] { ":[x], " -> ":[x]; " } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -105,7 +105,7 @@ let%expect_test "haskell_example" = {| where rewrite :[contents] { "," -> "++" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -129,7 +129,7 @@ let%expect_test "rewrite_freeform_antecedent_pattern" = {| where rewrite :[contents] { concat [:[x]] -> "nice" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in diff --git a/test/common/test_rewrite_rule_omega.ml b/test/common/test_rewrite_rule_omega.ml index 4ef6f76..8499aa8 100644 --- a/test/common/test_rewrite_rule_omega.ml +++ b/test/common/test_rewrite_rule_omega.ml @@ -34,7 +34,7 @@ let%expect_test "rewrite_rule" = {| where rewrite :[1] { "int" -> "expect" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -52,7 +52,7 @@ let%expect_test "sequenced_rewrite_rule" = rewrite :[a] { "a" -> "qqq" }, rewrite :[rest] { "{ b : { :[other] } }" -> "{ :[other] }" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -68,7 +68,7 @@ let%expect_test "rewrite_rule_for_list" = {| where rewrite :[contents] { ":[[x]]," -> ":[[x]];" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -84,7 +84,7 @@ let%expect_test "rewrite_rule_for_list_strip_last" = {| where rewrite :[contents] { ":[x], " -> ":[x]; " } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -105,7 +105,7 @@ let%expect_test "haskell_example" = {| where rewrite :[contents] { "," -> "++" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in @@ -129,7 +129,7 @@ let%expect_test "rewrite_freeform_antecedent_pattern" = {| where rewrite :[contents] { concat [:[x]] -> "nice" } |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in diff --git a/test/common/test_statistics.ml b/test/common/test_statistics.ml index fa8d476..5797468 100644 --- a/test/common/test_statistics.ml +++ b/test/common/test_statistics.ml @@ -28,7 +28,7 @@ let %expect_test "statistics" = let rule = {| where true |} - |> create + |> Language.Rule.create |> Or_error.ok_exn in Go.all ~configuration ~template ~source ()