Skip to content

Commit

Permalink
fix: literal encoding for kw_identifier and capture ints
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Mar 9, 2024
1 parent 7836151 commit c6b206f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/spitfire.ex
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,11 @@ defmodule Spitfire do
end

defp parse_kw_identifier(%{current_token: {:kw_identifier, meta, token}} = parser) do
token = encode_literal(parser, token, meta)
parser = parser |> next_token() |> eat_eol()

{expr, parser} = parse_expression(parser, @kw_identifier, false, false, false)

token = encode_literal(parser, token, meta)
{{token, expr}, parser}
end

Expand All @@ -486,10 +486,10 @@ defmodule Spitfire do
end

defp parse_bracketless_kw_list(%{current_token: {:kw_identifier, meta, token}} = parser) do
token = encode_literal(parser, token, meta)
parser = parser |> next_token() |> eat_eol()

{value, parser} = parse_expression(parser, @kw_identifier, false, false, false)
token = encode_literal(parser, token, meta)

{kvs, parser} =
while2 peek_token(parser) == :"," <- parser do
Expand Down Expand Up @@ -601,7 +601,9 @@ defmodule Spitfire do
token = current_token(parser)
meta = current_meta(parser)
parser = next_token(parser)
{encoder, parser} = Map.pop(parser, :literal_encoder)
{rhs, parser} = parse_int(parser)
parser = Map.put(parser, :literal_encoder, encoder)

ast = {token, meta, [rhs]}

Expand Down Expand Up @@ -2321,6 +2323,10 @@ defmodule Spitfire do
[delimiter: "'"]
end

defp additional_meta(_literal, %{current_token: {:kw_identifier, _, _}}) do
[format: :keyword]
end

defp additional_meta(_, %{current_token: {type, _, indent, _token}}) when type in [:list_heredoc] do
[delimiter: ~s"'''", indentation: indent]
end
Expand Down
5 changes: 4 additions & 1 deletion test/spitfire_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,10 @@ defmodule SpitfireTest do
foo
'''
""",
~S'{one, two}'
~S'{one, two}',
~S'''
Foo.run(f: &{:ok, {:__literal__, &2, [&1]}})
'''
]

encoder = fn l, m -> {:ok, {:__literal__, m, [l]}} end
Expand Down

0 comments on commit c6b206f

Please sign in to comment.