Skip to content

Commit 7326458

Browse files
committed
feat: string interpolation
1 parent 991aaf5 commit 7326458

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/spitfire.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,32 @@ defmodule Spitfire do
571571
{string, parser}
572572
end
573573

574+
defp parse_string(%{current_token: {:bin_string, _, tokens}} = parser) do
575+
args =
576+
for token <- tokens do
577+
case token do
578+
token when is_binary(token) ->
579+
token
580+
581+
{_start, _stop, tokens} ->
582+
# construct a new parser
583+
parser = %{
584+
tokens: tokens ++ [:eof],
585+
current_token: nil,
586+
peek_token: nil,
587+
nestings: [],
588+
stab_depth: 0
589+
}
590+
591+
{ast, _parser} = parse_expression(parser |> next_token() |> next_token())
592+
593+
{:"::", [], [{{:., [], [Kernel, :to_string]}, [], [ast]}, {:binary, [], Elixir}]}
594+
end
595+
end
596+
597+
{{:<<>>, [], args}, parser}
598+
end
599+
574600
defp parse_alias(%{current_token: {:alias, _, alias}} = parser) do
575601
aliases = [alias]
576602

test/spitfire_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ defmodule SpitfireTest do
5050
assert Spitfire.parse(code) == "foobar"
5151
end
5252

53+
test "parses string interpolation" do
54+
code = ~S'''
55+
"foo#{alice}bar"
56+
'''
57+
58+
assert Spitfire.parse(code) ==
59+
{:<<>>, [],
60+
[
61+
"foo",
62+
{:"::", [],
63+
[
64+
{{:., [], [Kernel, :to_string]}, [], [{:alice, [], Elixir}]},
65+
{:binary, [], Elixir}
66+
]},
67+
"bar"
68+
]}
69+
end
70+
5371
test "parses atoms" do
5472
code = ~s'''
5573
:foobar

0 commit comments

Comments
 (0)