What is going wrong with parsing floats here?
Qqwy opened this issue · 3 comments
Qqwy commented
A very strange thing happens when using either(float, integer)
. I don't know if this is a bug, or if I am doing something wrong.
My very simple parser module
defmodule Jux do
use Combine
def parse(str) do
Combine.parse(str, parser())
end
def parser do
whitespace()
|> many1(sequence([term(), whitespace()]))
end
def whitespace do
ignore(option(spaces()))
end
def term do
number()
end
def number do
either(float(), integer())
end
end
This crashes if there is a non-floating-point number somewhere before the end of the parsed statement.
Inputs that work:
iex> Jux.parse("2.0")
[[2.0]]
iex> Jux.parse(" 2.0")
[[2.0]]
iex> Jux.parse("2.0 3")
[[2.0, 3]]
iex> Jux.parse("3")
[[3]]
Inputs that do not work:
iex> Jux.parse("2.0 3 ")
** (ArgumentError) argument error
:erlang.binary_to_float("3")
lib/combine/parsers/text.ex:568: Combine.Parsers.Text.float_impl/1
lib/combine/parsers/base.ex:105: Combine.Parsers.Base.either_impl/3
lib/combine/parsers/base.ex:163: Combine.Parsers.Base.do_pipe/3
lib/combine/parsers/base.ex:152: Combine.Parsers.Base.pipe_impl/3
lib/combine/parsers/base.ex:330: Combine.Parsers.Base.many1_loop/5
lib/combine/parsers/base.ex:316: Combine.Parsers.Base.many1_impl/2
lib/combine.ex:52: Combine.parse/2
iex> Jux.parse("2 3")
** (ArgumentError) argument error
:erlang.binary_to_float("2")
lib/combine/parsers/text.ex:568: Combine.Parsers.Text.float_impl/1
lib/combine/parsers/base.ex:105: Combine.Parsers.Base.either_impl/3
lib/combine/parsers/base.ex:163: Combine.Parsers.Base.do_pipe/3
lib/combine/parsers/base.ex:152: Combine.Parsers.Base.pipe_impl/3
lib/combine/parsers/base.ex:330: Combine.Parsers.Base.many1_loop/5
lib/combine/parsers/base.ex:316: Combine.Parsers.Base.many1_impl/2
lib/combine.ex:52: Combine.parse/2
What is going wrong here?
bitwalker commented
Sorry it's taken so long, I've been swamped. I found the issue here and pushed a fix.
Qqwy commented
The fix works great. I'm really happy to be able to rewrite the parser in the Elixir version of Jux using Combine now 😄 .
Thank you very much!
bitwalker commented
You're very welcome :)