View Source Elnom.Sequence (elnom v0.1.0)

Combinators applying parsers in sequence

Summary

Functions

Matches an object from the first parser and discards it, then gets an object from the second parser, and finally matches an object from the third parser and discards it.

Gets an object from the first parser, then gets another object from the second parser.

Matches an object from the first parser and discards it, then gets an object from the second parser.

Gets an object from the first parser, then matches an object from the sep_parser and discards it, then gets another object from the second parser.

Gets an object from the first parser, then matches an object from the second parser and discards it.

Applies a tuple of parsers one by one and returns their results as a tuple.

Functions

Link to this function

delimited(first, second, third)

View Source

Matches an object from the first parser and discards it, then gets an object from the second parser, and finally matches an object from the third parser and discards it.

  iex> parser = delimited(tag("("), tag("abc"), tag(")"))
  iex> parser.("(abc)")
  {:ok, "", "abc"}
  iex> parser.("(abc)def")
  {:ok, "def", "abc"}
  iex> parser.("")
  {:error, %Error{kind: :tag, buffer: ""}}
  iex> parser.("123")
  {:error, %Error{kind: :tag, buffer: "123"}}

Gets an object from the first parser, then gets another object from the second parser.

iex> parser = pair(tag("abc"), tag("efg"))
iex> parser.("abcefg")
{:ok, "", {"abc", "efg"}}
iex> parser.("abcefg123")
{:ok, "123", {"abc", "efg"}}
iex> parser.("")
{:error, %Error{kind: :tag, buffer: ""}}
iex> parser.("123")
{:error, %Error{kind: :tag, buffer: "123"}}

Matches an object from the first parser and discards it, then gets an object from the second parser.

iex> parser = preceded(tag("abc"), tag("efg"))
iex> parser.("abcefg")
{:ok, "", "efg"}
iex> parser.("abcefghij")
{:ok, "hij", "efg"}
iex> parser.("")
{:error, %Error{kind: :tag, buffer: ""}}
iex> parser.("123")
{:error, %Error{kind: :tag, buffer: "123"}}
Link to this function

separated_pair(first, sep, second)

View Source

Gets an object from the first parser, then matches an object from the sep_parser and discards it, then gets another object from the second parser.

iex> parser = separated_pair(tag("abc"), tag("|"), tag("efg"))
iex> parser.("abc|efg")
{:ok, "", {"abc", "efg"}}
iex> parser.("abc|efg123")
{:ok, "123", {"abc", "efg"}}
iex> parser.("")
{:error, %Error{kind: :tag, buffer: ""}}
iex> parser.("123")
{:error, %Error{kind: :tag, buffer: "123"}}
Link to this function

terminated(first, second)

View Source

Gets an object from the first parser, then matches an object from the second parser and discards it.

iex> parser = terminated(tag("abc"), tag("efg"))
iex> parser.("abcefg")
{:ok, "", "abc"}
iex> parser.("abcefg123")
{:ok, "123", "abc"}
iex> parser.("")
{:error, %Error{kind: :tag, buffer: ""}}
iex> parser.("123")
{:error, %Error{kind: :tag, buffer: "123"}}

Applies a tuple of parsers one by one and returns their results as a tuple.

Either a tuple or a list of parsers can be provided.

iex> parser = tuple({alpha1(), digit1(), alpha1()})
iex> parser.("abc123def")
{:ok, "", {"abc", "123", "def"}}
iex> parser.("123def")
{:error, %Error{kind: :alpha, buffer: "123def"}}