View Source Elnom.Combinator (elnom v0.1.0)
General purpose combinators
Summary
Functions
Succeeds if all the input has been consumed by its child parser.
Transforms an Incomplete error into an Error.
Calls the parser if the condition is met.
If the child parser was successful, return the consumed input with the output as a tuple. Functions similarly to recognize/1 except it returns the parser output as well.
Transforms an Error (recoverable) to Failure (unrecoverable)
Returns its input if it is at the end of input data.
A parser which always fails.
Creates a new parser from the output of the first parser, then apply that parser over the rest of the input.
Maps a function on the result of a parser.
Applies a parser over the result of another one.
Applies a function successfully when returning {:ok, data} or unsuccesfully when returning anything else on the result of a parser.
Succeeds if the child parser returns an error.
Optional parser, will return nil on Error.
Tries to apply its parser without consuming the input.
If the child parser was successful, return the consumed input as produced value.
Return the remaining input.
Return the length of the remaining input.
A parser which always succeeds with given value without consuming any input.
Returns the provided value if the child parser succeeds.
Returns the result of the child parser if it satisfies a verification function.
Functions
Succeeds if all the input has been consumed by its child parser.
iex> parser = all_consuming(tag("abc"))
iex> parser.("abc")
{:ok, "", "abc"}
iex> parser.("abc123")
{:error, %Error{kind: :all_consuming, buffer: "123"}}
iex> parser.("ab")
{:error, %Error{kind: :tag, buffer: "ab"}}
Transforms an Incomplete error into an Error.
iex> parser = complete(length_data(u8(), :byte))
iex> parser.(<<5, "abcdefg">>)
{:ok, "fg", "abcde"}
iex> parser.(<<5, "abc">>)
{:error, %Error{kind: :complete, buffer: <<5, "abc">>}}
Calls the parser if the condition is met.
iex> parser = fn b -> cond(b, alpha1()) end
iex> parser.(true).("abcd;")
{:ok, ";", "abcd"}
iex> parser.(false).("abcd;")
{:ok, "abcd;", nil}
iex> parser.(true).("123;")
{:error, %Error{kind: :alpha, buffer: "123;"}}
iex> parser.(false).("123;")
{:ok, "123;", nil}
If the child parser was successful, return the consumed input with the output as a tuple. Functions similarly to recognize/1 except it returns the parser output as well.
This can be useful especially in cases where the output is not the same type as the input, or the input is a user defined type.
Returned tuple is of the format {consumed input, produced output}.
iex> parser = consumed(value(true, tag("abc")))
iex> parser.("abcdef")
{:ok, "def", {"abc", true}}
Transforms an Error (recoverable) to Failure (unrecoverable)
This commits the parse result, preventing alternative branch paths like with Elnom.Branch.alt/1.
Without cut/1:
iex> parser = alt({preceded(one_of("+-"), digit1()), rest()})
iex> parser.("+10 ab")
{:ok, " ab", "10"}
iex> parser.("ab")
{:ok, "", "ab"}
iex> parser.("+")
{:ok, "", "+"}
With cut/1:
iex> parser = alt({preceded(one_of("+-"), cut(digit1())), rest()})
iex> parser.("+10 ab")
{:ok, " ab", "10"}
iex> parser.("ab")
{:ok, "", "ab"}
iex> parser.("+")
{:error, %Failure{kind: :digit, buffer: ""}}
Returns its input if it is at the end of input data.
When we're at the end of the data, this combinator will succeed.
iex> eof().("abc")
{:error, %Error{kind: :eof, buffer: "abc"}}
iex> eof().("")
{:ok, "", ""}
A parser which always fails.
iex> fail().("abc")
{:error, %Error{kind: :fail, buffer: "abc"}}
Creates a new parser from the output of the first parser, then apply that parser over the rest of the input.
iex> parser = flat_map(u8(), &take/1)
iex> parser.(<<2, 0, 1, 2>>)
{:ok, <<2>>, <<0, 1>>}
iex> parser.(<<4, 0, 1, 2>>)
{:error, %Error{kind: :eof, buffer: <<0, 1, 2>>}}
Maps a function on the result of a parser.
iex> parser = map(digit1(), &String.length/1)
iex> parser.("123456")
{:ok, "", 6}
iex> parser.("abc")
{:error, %Error{kind: :digit, buffer: "abc"}}
Applies a parser over the result of another one.
iex> parser = map_parser(take(5), digit1())
iex> parser.("123456")
{:ok, "6", "12345"}
iex> parser.("123ab6")
{:ok, "6", "123"}
iex> parser.("123")
{:error, %Error{kind: :eof, buffer: "123"}}
iex> parser.("abcdef")
{:error, %Error{kind: :digit, buffer: "abcde"}}
Applies a function successfully when returning {:ok, data} or unsuccesfully when returning anything else on the result of a parser.
iex> to_integer = fn str ->
...> case Integer.parse(str) do
...> {int, ""} -> {:ok, int}
...> _ -> :error
...> end
...> end
iex> parser = map_res(take(3), to_integer)
iex> parser.("123abc")
{:ok, "abc", 123}
iex> parser.("abc")
:error
Succeeds if the child parser returns an error.
Because Elixir has a reserved not
function, this combinator is named not_
.
iex> parser = not_(alpha1())
iex> parser.("123")
{:ok, "123", nil}
iex> parser.("abc")
{:error, %Error{kind: :not, buffer: "abc"}}
Optional parser, will return nil on Error.
To chain an error up, see cut/1.
iex> parser = opt(alpha1())
iex> parser.("abc")
{:ok, "", "abc"}
iex> parser.("123")
{:ok, "123", nil}
Tries to apply its parser without consuming the input.
iex> parser = peek(alpha1())
iex> parser.("abcd;")
{:ok, "abcd;", "abcd"}
iex> parser.("123;")
{:error, %Error{kind: :alpha, buffer: "123;"}}
If the child parser was successful, return the consumed input as produced value.
iex> parser = recognize(separated_pair(alpha1(), char(","), alpha1()))
iex> parser.("abcd,efgh")
{:ok, "", "abcd,efgh"}
iex> parser.("abcd;")
{:error, %Error{kind: :char, buffer: ";"}}
Return the remaining input.
iex> parser = rest()
iex> parser.("abc")
{:ok, "", "abc"}
iex> parser.("")
{:ok, "", ""}
Return the length of the remaining input.
iex> parser = rest_len()
iex> parser.("abc")
{:ok, "", 3}
iex> parser.("")
{:ok, "", 0}
A parser which always succeeds with given value without consuming any input.
iex> parser = success(10)
iex> parser.("abc")
{:ok, "abc", 10}
iex> sign = alt({value(-1, char("-")), value(1, char("+")), success(1)})
iex> sign.("+10")
{:ok, "10", 1}
iex> sign.("-10")
{:ok, "10", -1}
iex> sign.("10")
{:ok, "10", 1}
Returns the provided value if the child parser succeeds.
iex> parser = value(1234, tag("abc"))
iex> parser.("abc")
{:ok, "", 1234}
iex> parser.("123")
{:error, %Error{kind: :tag, buffer: "123"}}
Returns the result of the child parser if it satisfies a verification function.
The verification function takes as argument a reference to the output of the parser.
iex> parser = verify(alpha1(), &String.length(&1) == 4)
iex> parser.("abcd")
{:ok, "", "abcd"}
iex> parser.("abcde")
{:error, %Error{kind: :verify, buffer: "abcde"}}
iex> parser.("123abcd;")
{:error, %Error{kind: :alpha, buffer: "123abcd;"}}