mirror of
https://github.com/comby-tools/comby.git
synced 2024-09-11 13:25:36 +03:00
Allow all kinds of hole syntax in rewrite template
This commit is contained in:
parent
69b9987df8
commit
e7fdaa5229
@ -3,16 +3,25 @@ open Core
|
||||
open Match
|
||||
|
||||
let substitute template env =
|
||||
let substitution_formats =
|
||||
[ ":[ ", "]"
|
||||
; ":[", ".]"
|
||||
; ":[", "\n]"
|
||||
; ":[[", "]]"
|
||||
; ":[", "]"
|
||||
]
|
||||
in
|
||||
Environment.vars env
|
||||
|> List.fold ~init:(template, []) ~f:(fun (acc, vars) variable ->
|
||||
match Environment.lookup env variable with
|
||||
| Some value ->
|
||||
if Option.is_some (String.substr_index template ~pattern:(":["^variable^"]")) then
|
||||
(String.substr_replace_all acc ~pattern:(":["^variable^"]") ~with_:value, variable::vars)
|
||||
else if Option.is_some (String.substr_index template ~pattern:(":[["^variable^"]]")) then
|
||||
(String.substr_replace_all acc ~pattern:(":[["^variable^"]]") ~with_:value, variable::vars)
|
||||
else
|
||||
acc, vars
|
||||
List.find_map substitution_formats ~f:(fun (left,right) ->
|
||||
let pattern = left^variable^right in
|
||||
if Option.is_some (String.substr_index template ~pattern) then
|
||||
Some (String.substr_replace_all acc ~pattern ~with_:value, variable::vars)
|
||||
else
|
||||
None)
|
||||
|> Option.value ~default:(acc,vars)
|
||||
| None -> acc, vars)
|
||||
|
||||
let of_match_context
|
||||
|
@ -162,6 +162,41 @@ let%expect_test "single_token_match_inside_paren_no_succeeding_whitespace" =
|
||||
|> print_string;
|
||||
[%expect_exact "foo : bar"]
|
||||
|
||||
let%expect_test "whitespace_hole_rewrite" =
|
||||
let template = {|:[ w]this|} in
|
||||
let rewrite_template = "space:[ w]here" in
|
||||
let source = {| this|} in
|
||||
all template source
|
||||
|> (fun matches -> Option.value_exn (Rewrite.all ~source ~rewrite_template matches))
|
||||
|> (fun { rewritten_source; _ } -> rewritten_source)
|
||||
|> print_string;
|
||||
[%expect_exact "space here"]
|
||||
|
||||
let%expect_test "punctuation_hole_rewrite" =
|
||||
let template = {|:[x.]|} in
|
||||
let rewrite_template = "->:[x.]<-" in
|
||||
let source = {|now.this. is,pod|racing|} in
|
||||
all template source
|
||||
|> (fun matches -> Option.value_exn (Rewrite.all ~source ~rewrite_template matches))
|
||||
|> (fun { rewritten_source; _ } -> rewritten_source)
|
||||
|> print_string;
|
||||
[%expect_exact "->now.this.<- ->is,pod|racing<-"]
|
||||
|
||||
let%expect_test "newline_hole_rewrite" =
|
||||
let template = {|:[x\n]|} in
|
||||
let rewrite_template = "->:[x\n]<-" in
|
||||
let source = {|now.this.
|
||||
is,pod|racing
|
||||
|} in
|
||||
all template source
|
||||
|> (fun matches -> Option.value_exn (Rewrite.all ~source ~rewrite_template matches))
|
||||
|> (fun { rewritten_source; _ } -> rewritten_source)
|
||||
|> print_string;
|
||||
[%expect_exact "->now.this.
|
||||
<-->is,pod|racing
|
||||
<-"]
|
||||
|
||||
|
||||
|
||||
let%expect_test "shift_or_at_least_dont_get_stuck" =
|
||||
let template = ":[1]" in
|
||||
|
Loading…
Reference in New Issue
Block a user