Skip to content

Commit

Permalink
Remove warnings on Elixir v1.17-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 8, 2023
1 parent b8c624c commit be6c5ac
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 54 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ jobs:
matrix:
include:
- pair:
elixir: 1.6.6
otp: 20.3
- pair:
elixir: 1.11.4
elixir: 1.12.4
otp: 23.3
- pair:
elixir: 1.13.3
otp: 24.2
elixir: 1.15.7
otp: 26.1
lint: lint
steps:
- uses: actions/checkout@v3
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for NimbleParsec

## v1.4.0-dev (2023-11-08)

### Enhancements

* Remove warnings and require Elixir v1.12

## v1.3.1 (2023-04-30)

### Bug fixes
Expand Down
8 changes: 4 additions & 4 deletions lib/nimble_parsec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,11 @@ defmodule NimbleParsec do
end
end

defp exclude_bin_segment?({:not, min..max}, gen), do: gen >= min and gen <= max
defp exclude_bin_segment?({:not, _.._//_ = range}, gen), do: gen in range
defp exclude_bin_segment?({:not, char}, gen) when is_integer(char), do: char == gen

defp int_random(nil), do: Enum.random(0..3)
defp int_random(_.._ = range), do: Enum.random(range)
defp int_random(_.._//_ = range), do: Enum.random(range)
defp int_random(int) when is_integer(int), do: int

# Enum.random uses reservoir sampling but our lists are short, so we use length + fetch!
Expand Down Expand Up @@ -1822,9 +1822,9 @@ defmodule NimbleParsec do
end

defp split_range!(x, _context) when is_integer(x), do: true
defp split_range!(_.._, _context), do: true
defp split_range!(_.._//step, _context) when abs(step) == 1, do: true
defp split_range!({:not, x}, _context) when is_integer(x), do: false
defp split_range!({:not, _.._}, _context), do: false
defp split_range!({:not, _.._//step}, _context) when abs(step) == 1, do: false

defp split_range!(range, context) do
raise ArgumentError, "unknown range #{inspect(range)} given to #{context}"
Expand Down
26 changes: 13 additions & 13 deletions lib/nimble_parsec/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ defmodule NimbleParsec.Compiler do

defp newline_allowed?(ors) do
Enum.any?(ors, fn
_.._ = range -> ?\n in range
_.._//_ = range -> ?\n in range
codepoint -> ?\n === codepoint
end)
end
Expand All @@ -937,7 +937,7 @@ defmodule NimbleParsec.Compiler do

defp newline_forbidden?(ands) do
Enum.any?(ands, fn
{:not, _.._ = range} -> ?\n in range
{:not, _.._//_ = range} -> ?\n in range
{:not, codepoint} -> ?\n === codepoint
end)
end
Expand Down Expand Up @@ -1044,33 +1044,33 @@ defmodule NimbleParsec.Compiler do

defp bin_range_to_guard(var, range) do
case range do
min..max when min < max ->
min..min//step when abs(step) == 1 ->
quote(do: unquote(var) === unquote(min))

min..max//1 ->
quote(do: unquote(var) >= unquote(min) and unquote(var) <= unquote(max))

min..max when min > max ->
min..max//-1 ->
quote(do: unquote(var) >= unquote(max) and unquote(var) <= unquote(min))

min..min ->
quote(do: unquote(var) === unquote(min))

min when is_integer(min) ->
quote(do: unquote(var) === unquote(min))

{:not, min..max} when min < max ->
{:not, min..min//step} when abs(step) == 1 ->
quote(do: unquote(var) !== unquote(min))

{:not, min..max//1} ->
quote(do: unquote(var) < unquote(min) or unquote(var) > unquote(max))

{:not, min..max} when min > max ->
{:not, min..max//-1} ->
quote(do: unquote(var) < unquote(max) or unquote(var) > unquote(min))

{:not, min..min} ->
quote(do: unquote(var) !== unquote(min))

{:not, min} when is_integer(min) ->
quote(do: unquote(var) !== unquote(min))
end
end

defp inspect_bin_range(min..max, printable?) do
defp inspect_bin_range(min..max//_, printable?) do
{" in the range #{inspect_char(min)} to #{inspect_char(max)}",
printable? and printable?(min) and printable?(max)}
end
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule NimbleParsec.MixProject do
use Mix.Project

@version "1.3.1"
@version "1.4.0-dev"
@url "https://github.com/dashbitco/nimble_parsec"

def project do
[
app: :nimble_parsec,
version: @version,
elixir: "~> 1.6",
elixir: "~> 1.12",
name: "NimbleParsec",
description: "A simple and fast library for text-based parser combinators",
aliases: [docs: &build_docs/1],
Expand Down
66 changes: 37 additions & 29 deletions test/nimble_parsec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ defmodule NimbleParsecTest do

describe "ascii_char/2 combinator without newlines" do
defparsecp :only_ascii, ascii_char([?0..?9]) |> ascii_char([])
defparsecp :multi_ascii, ascii_char([?0..?9, ?z..?a])
defparsecp :multi_ascii_with_not, ascii_char([?0..?9, ?z..?a, not: ?c])
defparsecp :multi_ascii_with_multi_not, ascii_char([?0..?9, ?z..?a, not: ?c, not: ?d..?e])
defparsecp :multi_ascii, ascii_char([?0..?9, ?z..?a//-1])
defparsecp :multi_ascii_with_not, ascii_char([?0..?9, ?z..?a//-1, not: ?c])
defparsecp :multi_ascii_with_multi_not, ascii_char([?0..?9, ?z..?a//-1, not: ?c, not: ?d..?e])
defparsecp :ascii_newline, ascii_char([?0..?9, ?\n]) |> ascii_char([?a..?z, ?\n])
defparsecp :ascii_only_newline, ascii_char([?\n])
defparsecp :none_ascii, ascii_char([?\a..?\n])
Expand Down Expand Up @@ -60,14 +60,14 @@ defmodule NimbleParsecTest do
@error "expected ASCII character equal to \"\\n\""

test "returns ok/error on only newline" do
assert ascii_only_newline("\n") == {:ok, '\n', "", %{}, {2, 1}, 1}
assert ascii_only_newline("\n") == {:ok, ~c"\n", "", %{}, {2, 1}, 1}
assert ascii_only_newline("x") == {:error, @error, "x", %{}, {1, 0}, 0}
end

@error "expected ASCII character in the range \"\\a\" to \"\\n\""

test "returns ok/error on none ascii range" do
assert none_ascii("\a\t\n") == {:ok, '\a', "\t\n", %{}, {1, 0}, 1}
assert none_ascii("\a\t\n") == {:ok, ~c"\a", "\t\n", %{}, {1, 0}, 1}
assert none_ascii("x") == {:error, @error, "x", %{}, {1, 0}, 0}
end

Expand Down Expand Up @@ -497,18 +497,18 @@ defmodule NimbleParsecTest do

test "returns error from traversal" do
assert remote_post_traverse_error_when_last_is_z("abcdef") ==
{:ok, 'abcdef', "", %{}, {1, 0}, 6}
{:ok, ~c"abcdef", "", %{}, {1, 0}, 6}

assert remote_post_traverse_error_when_last_is_z("abcdez") ==
{:error, "last is z", "", %{}, {1, 0}, 6}
end

test "post traverrse with rest lookahead" do
assert remote_post_traverse_lookahead("abcdef#XcanbeanythingX12") ==
{:ok, ['abcdef', {"X", "canbeanything"}, 12], "", %{}, {1, 0}, 9}
{:ok, [~c"abcdef", {"X", "canbeanything"}, 12], "", %{}, {1, 0}, 9}

assert remote_post_traverse_lookahead("abcdef#ZcanbeanythingZ12") ==
{:ok, ['abcdef', {"Z", "canbeanything"}, 12], "", %{}, {1, 0}, 9}
{:ok, [~c"abcdef", {"Z", "canbeanything"}, 12], "", %{}, {1, 0}, 9}

assert remote_post_traverse_lookahead("abcdef#Zcanbeanything") ==
{:error, "missing closing Z", "Zcanbeanything", %{}, {1, 0}, 7}
Expand Down Expand Up @@ -554,7 +554,7 @@ defmodule NimbleParsecTest do

test "returns error from traversal" do
assert local_post_traverse_error_when_last_is_z("abcdef") ==
{:ok, 'abcdef', "", %{}, {1, 0}, 6}
{:ok, ~c"abcdef", "", %{}, {1, 0}, 6}

assert local_post_traverse_error_when_last_is_z("abcdez") ==
{:error, "last is z", "", %{}, {1, 0}, 6}
Expand Down Expand Up @@ -599,7 +599,7 @@ defmodule NimbleParsecTest do

test "returns error from traversal" do
assert remote_pre_traverse_error_when_last_is_z("abcdef") ==
{:ok, 'abcdef', "", %{}, {1, 0}, 6}
{:ok, ~c"abcdef", "", %{}, {1, 0}, 6}

assert remote_pre_traverse_error_when_last_is_z("abcdez") ==
{:error, "last is z", "", %{}, {1, 0}, 6}
Expand Down Expand Up @@ -645,7 +645,7 @@ defmodule NimbleParsecTest do

test "returns error from traversal" do
assert local_pre_traverse_error_when_last_is_z("abcdef") ==
{:ok, 'abcdef', "", %{}, {1, 0}, 6}
{:ok, ~c"abcdef", "", %{}, {1, 0}, 6}

assert local_pre_traverse_error_when_last_is_z("abcdez") ==
{:error, "last is z", "", %{}, {1, 0}, 6}
Expand Down Expand Up @@ -717,15 +717,21 @@ defmodule NimbleParsecTest do
end

test "aborts choice on no match" do
assert lookahead_with_choice_digits_first("a0") == {:ok, [first: 'a'], "0", %{}, {1, 0}, 1}
assert lookahead_with_choice_digits_first("aa") == {:ok, [second: 'a'], "a", %{}, {1, 0}, 1}
assert lookahead_with_choice_digits_last("a0") == {:ok, [second: 'a'], "0", %{}, {1, 0}, 1}
assert lookahead_with_choice_digits_last("aa") == {:ok, [first: 'a'], "a", %{}, {1, 0}, 1}
assert lookahead_with_choice_digits_first("a0") ==
{:ok, [first: ~c"a"], "0", %{}, {1, 0}, 1}

assert lookahead_with_choice_digits_first("aa") ==
{:ok, [second: ~c"a"], "a", %{}, {1, 0}, 1}

assert lookahead_with_choice_digits_last("a0") ==
{:ok, [second: ~c"a"], "0", %{}, {1, 0}, 1}

assert lookahead_with_choice_digits_last("aa") == {:ok, [first: ~c"a"], "a", %{}, {1, 0}, 1}
end

test "with inner choice" do
assert lookahead_with_inner_choice("aa") == {:ok, 'a', "a", %{}, {1, 0}, 1}
assert lookahead_with_inner_choice("af") == {:ok, 'a', "f", %{}, {1, 0}, 1}
assert lookahead_with_inner_choice("aa") == {:ok, ~c"a", "a", %{}, {1, 0}, 1}
assert lookahead_with_inner_choice("af") == {:ok, ~c"a", "f", %{}, {1, 0}, 1}

assert lookahead_with_inner_choice("az") ==
{:error,
Expand All @@ -738,7 +744,7 @@ defmodule NimbleParsecTest do
{:error, "expected ASCII character in the range \"0\" to \"9\"", "", %{}, {1, 0},
1}

assert lookahead_with_times("a0") == {:ok, 'a', "0", %{}, {1, 0}, 1}
assert lookahead_with_times("a0") == {:ok, ~c"a", "0", %{}, {1, 0}, 1}

assert lookahead_with_times("aa0") ==
{:error, "expected ASCII character in the range \"0\" to \"9\"", "a0", %{}, {1, 0},
Expand Down Expand Up @@ -792,20 +798,20 @@ defmodule NimbleParsecTest do

test "aborts choice on match" do
assert lookahead_not_with_choice_digits_first("a0") ==
{:ok, [second: 'a'], "0", %{}, {1, 0}, 1}
{:ok, [second: ~c"a"], "0", %{}, {1, 0}, 1}

assert lookahead_not_with_choice_digits_first("aa") ==
{:ok, [first: 'a'], "a", %{}, {1, 0}, 1}
{:ok, [first: ~c"a"], "a", %{}, {1, 0}, 1}

assert lookahead_not_with_choice_digits_last("a0") ==
{:ok, [first: 'a'], "0", %{}, {1, 0}, 1}
{:ok, [first: ~c"a"], "0", %{}, {1, 0}, 1}

assert lookahead_not_with_choice_digits_last("aa") ==
{:ok, [second: 'a'], "a", %{}, {1, 0}, 1}
{:ok, [second: ~c"a"], "a", %{}, {1, 0}, 1}
end

test "with inner choice" do
assert lookahead_not_with_inner_choice("az") == {:ok, 'a', "z", %{}, {1, 0}, 1}
assert lookahead_not_with_inner_choice("az") == {:ok, ~c"a", "z", %{}, {1, 0}, 1}

assert lookahead_not_with_inner_choice("aa") ==
{:error,
Expand All @@ -823,8 +829,8 @@ defmodule NimbleParsecTest do
{:error, "did not expect ASCII character in the range \"0\" to \"9\"", "0", %{},
{1, 0}, 1}

assert lookahead_not_with_times("aa0") == {:ok, 'a', "a0", %{}, {1, 0}, 1}
assert lookahead_not_with_times("aaa0") == {:ok, 'aa', "a0", %{}, {1, 0}, 2}
assert lookahead_not_with_times("aa0") == {:ok, ~c"a", "a0", %{}, {1, 0}, 1}
assert lookahead_not_with_times("aaa0") == {:ok, ~c"aa", "a0", %{}, {1, 0}, 2}
end

test "repeaet_until" do
Expand Down Expand Up @@ -1133,7 +1139,7 @@ defmodule NimbleParsecTest do

test "returns ok/error with repeat" do
assert repeat_while_repeat_digit("a1b2c3d4e5f6g7h8", context: %{count: 3}) ==
{:ok, 'a1b2', "c3d4e5f6g7h8", %{count: 0}, {1, 0}, 4}
{:ok, ~c"a1b2", "c3d4e5f6g7h8", %{count: 0}, {1, 0}, 4}
end

def not_3(<<?3, _::binary>>, %{} = context, {line, line_offset}, byte_offset)
Expand Down Expand Up @@ -1467,13 +1473,15 @@ defmodule NimbleParsecTest do

test "returns ok" do
string = "123abc"
assert {:ok, '123', "abc" = rest, %{}, {1, 0} = line, byte_offset} = digits(string)
assert chars(rest, line: line, byte_offset: byte_offset) == {:ok, 'abc', "", %{}, {1, 0}, 6}
assert {:ok, ~c"123", "abc" = rest, %{}, {1, 0} = line, byte_offset} = digits(string)

assert chars(rest, line: line, byte_offset: byte_offset) ==
{:ok, ~c"abc", "", %{}, {1, 0}, 6}
end

test "returns error" do
string = "123:abc"
assert {:ok, '123', ":abc" = rest, %{}, {1, 0} = line, byte_offset} = digits(string)
assert {:ok, ~c"123", ":abc" = rest, %{}, {1, 0} = line, byte_offset} = digits(string)

assert chars(rest, line: line, byte_offset: byte_offset) ==
{:error, "expected chars", ":abc", %{}, {1, 0}, 3}
Expand Down

0 comments on commit be6c5ac

Please sign in to comment.