Skip to content

Commit

Permalink
Add pending deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 21, 2024
1 parent 1fedf42 commit 0faac64
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
18 changes: 14 additions & 4 deletions lib/nimble_parsec/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ defmodule NimbleParsec.Compiler do

{acc, context} when acc != :error ->
IO.warn(
"Returning a two-element tuple {acc, context} in pre_traverse/post_traverse is deprecated, " <>
"returning a two-element tuple {acc, context} in pre_traverse/post_traverse is deprecated, " <>
"please return {rest, acc, context} instead"
)

Expand All @@ -443,9 +443,19 @@ defmodule NimbleParsec.Compiler do
# TODO: Deprecate two element tuple return that is not error
quote generated: true do
case unquote(quoted) do
{_, _, _} = res -> res
{:error, reason} -> {:error, reason}
{acc, context} -> {unquote(rest), acc, context}
{_, _, _} = res ->
res

{:error, reason} ->
{:error, reason}

{acc, context} ->
IO.warn(
"returning a two-element tuple {acc, context} in pre_traverse/post_traverse is deprecated, " <>
"please return {rest, acc, context} instead"
)

{unquote(rest), acc, context}
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ defmodule NimbleParsec.IntegrationTest do
|> post_traverse({:sign_int_value, []})
|> tag(:signed_int)

defp sign_int_value(_rest, [int, _neg], context, _, _) do
{[int * -1], context}
defp sign_int_value(rest, [int, _neg], context, _, _) do
{rest, [int * -1], context}
end

defp sign_int_value(_rest, res, context, _, _) do
{res, context}
defp sign_int_value(rest, res, context, _, _) do
{rest, res, context}
end

test "returns ok/error" do
Expand All @@ -90,8 +90,8 @@ defmodule NimbleParsec.IntegrationTest do

defparsec :language_code, language_code

defp atomize_language_code(_rest, [language_code], context, _line, _offset) do
{[String.to_atom(language_code)], context}
defp atomize_language_code(rest, [language_code], context, _line, _offset) do
{rest, [String.to_atom(language_code)], context}
end

test "returns ok/error" do
Expand Down
6 changes: 3 additions & 3 deletions test/nimble_parsec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,8 +1500,8 @@ defmodule NimbleParsecTest do
end
end

defp location(_rest, args, %{} = context, line, offset, tag) do
{[{tag, args, line, offset}], context}
defp location(rest, args, %{} = context, line, offset, tag) do
{rest, [{tag, args, line, offset}], context}
end

defp bound?(document) do
Expand Down Expand Up @@ -1554,6 +1554,6 @@ defmodule NimbleParsecTest do
defp private_join_and_wrap(rest, args, %{} = context, {line, line_offset}, byte_offset, joiner)
when is_binary(rest) and is_integer(line) and is_integer(line_offset) and
is_integer(byte_offset) do
{args |> Enum.join(joiner) |> List.wrap(), context}
{rest, args |> Enum.join(joiner) |> List.wrap(), context}
end
end

0 comments on commit 0faac64

Please sign in to comment.