Skip to content

Commit 58d54af

Browse files
committed
Merge pull request #2511 from ericmj/macro-tostring-sigils
Use sigil syntax in Macro.to_string
2 parents 4bcd25f + 001d776 commit 58d54af

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/elixir/lib/macro.ex

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,15 @@ defmodule Macro do
525525

526526
# All other calls
527527
def to_string({target, _, args} = ast, fun) when is_list(args) do
528-
{list, last} = :elixir_utils.split_last(args)
529-
fun.(ast, case kw_blocks?(last) do
530-
true -> call_to_string_with_args(target, list, fun) <> kw_blocks_to_string(last, fun)
531-
false -> call_to_string_with_args(target, args, fun)
532-
end)
528+
if sigil = sigil_call(ast, fun) do
529+
sigil
530+
else
531+
{list, last} = :elixir_utils.split_last(args)
532+
fun.(ast, case kw_blocks?(last) do
533+
true -> call_to_string_with_args(target, list, fun) <> kw_blocks_to_string(last, fun)
534+
false -> call_to_string_with_args(target, args, fun)
535+
end)
536+
end
533537
end
534538

535539
# Two-item tuples
@@ -565,6 +569,26 @@ defmodule Macro do
565569
defp module_to_string(atom, _fun) when is_atom(atom), do: inspect(atom, [])
566570
defp module_to_string(other, fun), do: call_to_string(other, fun)
567571

572+
defp sigil_call({func, _, [{:<<>>, _, [string]}, args]} = ast, fun) when is_list(args) do
573+
sigil =
574+
case Atom.to_string(func) do
575+
<<"sigil_", name>> ->
576+
"~" <> <<name>> <>
577+
fun.(string, inspect(string, [])) <>
578+
sigil_args(args, fun)
579+
_ ->
580+
nil
581+
end
582+
fun.(ast, sigil)
583+
end
584+
585+
defp sigil_call(_other, _fun) do
586+
nil
587+
end
588+
589+
defp sigil_args([], _fun), do: ""
590+
defp sigil_args(args, fun), do: fun.(args, List.to_string(args))
591+
568592
defp call_to_string(atom, _fun) when is_atom(atom), do: Atom.to_string(atom)
569593
defp call_to_string({:., _, [arg]}, fun), do: module_to_string(arg, fun) <> "."
570594
defp call_to_string({:., _, [left, right]}, fun), do: module_to_string(left, fun) <> "." <> call_to_string(right, fun)

lib/elixir/test/elixir/macro_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ defmodule MacroTest do
275275
assert Macro.to_string(quote do: Foo.Bar.baz([1, 2, 3])) == "Foo.Bar.baz([1, 2, 3])"
276276
end
277277

278+
test :sigil_call_to_string do
279+
assert Macro.to_string(quote do: ~r"123") == ~s/~r"123"/
280+
assert Macro.to_string(quote do: ~r"123"u) == ~s/~r"123"u/
281+
assert Macro.to_string(quote do: ~r"\n123") == ~s/~r"\\\\n123"/
282+
283+
assert Macro.to_string(quote do: ~R"123") == ~s/~R"123"/
284+
assert Macro.to_string(quote do: ~R"123"u) == ~s/~R"123"u/
285+
assert Macro.to_string(quote do: ~R"\n123") == ~s/~R"\\\\n123"/
286+
end
287+
278288
test :arrow_to_string do
279289
assert Macro.to_string(quote do: foo(1, (2 -> 3))) == "foo(1, (2 -> 3))"
280290
end

0 commit comments

Comments
 (0)