bitwalker/combine

Another question

jxxcarlson opened this issue · 1 comments

First off - is there a better forum for my questions? They are not issues with Combine, but rather are problems due to my lack of experience. I very much appreciate your help and will try to keep questions to a bare minimum as I learn Combine.

The problem. The parsing functions that I write work in isolation but not when I combine them. Here is an example (in module MU.Parse):

  def str2(input) do
    parser = take_while(fn(a) -> !Enum.member?('-=+*\n\r', a) end)
    input |> Combine.parse(parser)
  end

And here is verification that it works:

iex(6)> MU.Parse.str2("foo\nbar")
['foo']

But when I try to use Parse.str2 inside Combine.parse, I get an error:

iex(1)> use Combine
Combine.Parsers.Binary

iex(2)> MU.Parse.str2("foo-bar")
['foo']

iex(4)> Combine.parse("[foo bar]", sequence([char("["), MU.Parse.str2(), char("]")]))
** (UndefinedFunctionError) function MU.Parse.str2/0 is undefined or private. Did you mean one of:

      * str2/1

    (mu_server) MU.Parse.str2()

There seems to be an arity problem, but MU.Parse.str2 in place of MU.Parse.str2() doesn't work either.

First, you can always open issues for questions here, it helps others who come after :)

Your problem here is that you need to remove Combine.parse/2 from the str2 implementation. It will then return a parser which can be combined as you are trying to do. Combine.parse/2 expects specifically implemented parser functions, which you can easily implement, but are not the same as plain functions. If you are interested in implementing parser functions, take a look at defparser and Combine's implementation of some of the various parsers.