From d0d4c6111853e2e066e8941f09601fd7846219ac Mon Sep 17 00:00:00 2001 From: Rijnard van Tonder Date: Sun, 25 Apr 2021 14:39:08 -0700 Subject: [PATCH] update --- lib/kernel/rewriter/rewrite_template.ml | 4 ++- test/common/test_parse_rewrite_template.ml | 33 +++++++++++++++++++- test/example/metasyntax/dangling.json | 11 +++++++ test/example/metasyntax/emoji.json | 24 ++++++++++++++ test/example/metasyntax/inverted-parens.json | 11 +++++++ test/example/metasyntax/lambda.json | 25 +++++++++++++++ test/example/metasyntax/wutspace.json | 17 ++++++++++ 7 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 test/example/metasyntax/dangling.json create mode 100644 test/example/metasyntax/emoji.json create mode 100644 test/example/metasyntax/inverted-parens.json create mode 100644 test/example/metasyntax/lambda.json create mode 100644 test/example/metasyntax/wutspace.json diff --git a/lib/kernel/rewriter/rewrite_template.ml b/lib/kernel/rewriter/rewrite_template.ml index 85c2cff..8e5ac40 100644 --- a/lib/kernel/rewriter/rewrite_template.ml +++ b/lib/kernel/rewriter/rewrite_template.ml @@ -103,9 +103,11 @@ module Make (Metasyntax : Matchers.Metasyntax.S) = struct let hole_prefixes = List.map Metasyntax.syntax ~f:(function | Hole (_, Delimited (Some left, _)) - | Regex (left, _, _) -> Some left + | Regex (left, _, _) -> Some [left] + | Hole (_, Reserved_identifiers l) -> Some l | _ -> None) |> List.filter_opt + |> List.concat (** Not smart enough: only looks for hole prefix to stop scanning constant, because there isn't a good 'not' parser *) diff --git a/test/common/test_parse_rewrite_template.ml b/test/common/test_parse_rewrite_template.ml index ef562e2..735745d 100644 --- a/test/common/test_parse_rewrite_template.ml +++ b/test/common/test_parse_rewrite_template.ml @@ -34,4 +34,35 @@ let%expect_test "interpret_incomplete_hole_as_constant_metasyntax" = in parse metasyntax template |> print_string; [%expect_exact {|((Constant $) (Constant ":x ") (Hole ((variable B) (pattern "$B:x "))) - (Constant " ") (Hole ((variable A) (pattern $A))))|}]; + (Constant " ") (Hole ((variable A) (pattern $A))))|}] + +let%expect_test "interpret_incomplete_hole_as_constant_metasyntax" = + let template = "( , , )" in + let metasyntax = + Matchers.Metasyntax.{ + syntax = + [ Hole (Everything, Delimited (Some "NOTHING", None)) + ; Hole (Everything, Reserved_identifiers [" "; " "]) + ] + ; identifier = "AB" + } + in + parse metasyntax template |> print_string; + [%expect_exact {|((Constant "(") (Hole ((variable " ") (pattern " "))) (Constant ,) + (Hole ((variable " ") (pattern " "))) (Constant ,) + (Hole ((variable " ") (pattern " "))) (Constant ")"))|}] + +let%expect_test "interpret_incomplete_hole_as_constant_metasyntax" = + let template = "(..,.)" in + let metasyntax = + Matchers.Metasyntax.{ + syntax = + [ Hole (Everything, Delimited (Some "NOTHING", None)) + ; Hole (Everything, Reserved_identifiers [".."; "."]) + ] + ; identifier = "AB" + } + in + parse metasyntax template |> print_string; + [%expect_exact {|((Constant "(") (Hole ((variable ..) (pattern ..))) (Constant ,) + (Hole ((variable .) (pattern .))) (Constant ")"))|}] diff --git a/test/example/metasyntax/dangling.json b/test/example/metasyntax/dangling.json new file mode 100644 index 0000000..56a46c2 --- /dev/null +++ b/test/example/metasyntax/dangling.json @@ -0,0 +1,11 @@ +{ + "syntax": [ + [ "Hole", [ "Everything" ], [ "Delimited", ")", null ] ], + [ "Hole", [ "Expression" ], [ "Delimited", "))", null ] ] + ], + "identifier": + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" +} +// echo 'swap(x, y)' | \ +// ./comby -stdin -custom-metasyntax test/example/metasyntax/dangling.json \ +// 'swap()1, ))2)' 'swap()2, )1)' diff --git a/test/example/metasyntax/emoji.json b/test/example/metasyntax/emoji.json new file mode 100644 index 0000000..38c9565 --- /dev/null +++ b/test/example/metasyntax/emoji.json @@ -0,0 +1,24 @@ +{ + "syntax": [ + [ "Hole", [ "Everything" ], [ "Delimited", "place-holder", null ] ], + [ "Hole", [ "Everything" ], + [ "Reserved_identifiers", + [ "❀️", "πŸ’™", "πŸ’š", "πŸ’œ", "πŸ’›", "🧑" ] + ] + ], + [ "Hole", [ "Expression" ], + [ "Reserved_identifiers", + [ "πŸ˜‚", "🚑", "πŸ˜ƒ", "😬", "😈", "🐡", "βœ‹", "😻", "🍌" ] + ] + ] + ], + "identifier": + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" +} +// echo 'swap(x, y)' | \ +// ./comby -stdin -custom-metasyntax test/example/metasyntax/emoji.json \ +// 'swap(🐡, 🍌)' 'swap(🍌, 🐡)' + +// echo 'add(x, x)' | \ [17:25:08] +// ./comby -stdin -custom-metasyntax test/example/metasyntax/emoji.json \ +// 'add(πŸ˜‚, πŸ˜‚)' 'multiply(2, πŸ˜‚)' diff --git a/test/example/metasyntax/inverted-parens.json b/test/example/metasyntax/inverted-parens.json new file mode 100644 index 0000000..f153047 --- /dev/null +++ b/test/example/metasyntax/inverted-parens.json @@ -0,0 +1,11 @@ +{ + "syntax": [ + [ "Hole", [ "Everything" ], [ "Delimited", ")", "(" ] ], + [ "Hole", [ "Expression" ], [ "Delimited", "))", "((" ] ] + ], + "identifier": + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" +} +// echo 'swap(x, y)' | \ +// ./comby -stdin -custom-metasyntax test/example/metasyntax/inverted-parens.json \ +// 'swap()1(, )2()' 'swap()2(, )1()' diff --git a/test/example/metasyntax/lambda.json b/test/example/metasyntax/lambda.json new file mode 100644 index 0000000..845c49d --- /dev/null +++ b/test/example/metasyntax/lambda.json @@ -0,0 +1,25 @@ +{ + "syntax": [ + [ "Hole", [ "Everything" ], [ "Delimited", "place-holder", null ] ], + [ "Hole", [ "Everything" ], + [ "Reserved_identifiers", + [ "Ξ“", "Ξ”", "Θ", "Ξ›", "Ξ", "Ξ ", "Ξ£", "Ξ¦", "Ξ¨", "Ξ©" ] + ] + ], + [ "Hole", [ "Expression" ], + [ "Reserved_identifiers", + [ + "Ξ±", "Ξ²", "Ξ³", "Ξ΄", "Ξ΅", "ΞΆ", "Ξ·", "ΞΈ", "ΞΉ", "ΞΊ", "Ξ»", + "ΞΌ", "ΞΎ", "Ο€", "ρ", "Ο‚", "Οƒ", "Ο„", "Ο…", "Ο†", "Ο‡", "ψ", + "Ο‰" + ] + ] + ] + ], + "identifier": + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" +} +// echo 'swap(x, y)' | \ +// ./comby -stdin -custom-metasyntax test/example/metasyntax/lambda.json \ +// 'Ξ»(Ξ±, Ξ²)' 'Ξ»(Ξ², Ξ±)' + diff --git a/test/example/metasyntax/wutspace.json b/test/example/metasyntax/wutspace.json new file mode 100644 index 0000000..5d5839d --- /dev/null +++ b/test/example/metasyntax/wutspace.json @@ -0,0 +1,17 @@ +{ + "syntax": [ + // Currently a placeholder is needed if we only care about Reserved_idenitifersl to avoid trickery. + // Order is significant. + [ "Hole", [ "Everything" ], [ "Delimited", "place-holder", null ] ], + [ "Hole", [ "Everything" ], + [ "Reserved_identifiers", + [ " ", " " ] + ] + ] + ], + "identifier": + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" +} +// echo 'swap(x,y)' | \ +// ./comby -stdin -custom-metasyntax test/example/metasyntax/wutspace.json \ +// 'swap( , )' 'swap( , )'