From c6b206f3de4d43df612cd9cbddbe0b03327549e5 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Sat, 9 Mar 2024 07:47:16 -0500 Subject: [PATCH] fix: literal encoding for kw_identifier and capture ints --- lib/spitfire.ex | 10 ++++++++-- test/spitfire_test.exs | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/spitfire.ex b/lib/spitfire.ex index 6a97885..641def0 100644 --- a/lib/spitfire.ex +++ b/lib/spitfire.ex @@ -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 @@ -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 @@ -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]} @@ -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 diff --git a/test/spitfire_test.exs b/test/spitfire_test.exs index f714c59..bd43cc1 100644 --- a/test/spitfire_test.exs +++ b/test/spitfire_test.exs @@ -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