mirror of
https://github.com/comby-tools/comby.git
synced 2024-08-16 16:50:37 +03:00
merge rule evaluation (#278)
This commit is contained in:
parent
f7bf5c7b40
commit
5304b31189
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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))
|
@ -2,5 +2,4 @@ include Types
|
||||
|
||||
let create = Parser.create
|
||||
|
||||
module Alpha = Alpha_rule
|
||||
module Omega = Omega_rule
|
||||
include Evaluate
|
||||
|
@ -4,5 +4,4 @@ include module type of Types
|
||||
|
||||
val create : string -> t Or_error.t
|
||||
|
||||
module Alpha : Engine
|
||||
module Omega : Engine
|
||||
include Engine
|
||||
|
@ -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
|
||||
|
@ -1,5 +1,2 @@
|
||||
open Language
|
||||
|
||||
include Matchers.Alpha
|
||||
let create = Language.Rule.create
|
||||
module Rule = Rule.Alpha
|
||||
module Rule = Language.Rule
|
||||
|
@ -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 _ ->
|
||||
|
@ -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 _ ->
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 = {|
|
||||
|
@ -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 = {|
|
||||
|
@ -1,5 +1,2 @@
|
||||
open Language
|
||||
|
||||
include Matchers.Omega
|
||||
let create = Language.Rule.create
|
||||
module Rule = Rule.Omega
|
||||
module Rule = Language.Rule
|
||||
|
@ -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" =
|
||||
|
@ -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" =
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 ()
|
||||
|
Loading…
Reference in New Issue
Block a user