Ergo.Combinators (ergo v0.1.1)

Link to this section Summary

Functions

The choice/1 parser takes a list of parsers. It tries each in order attempting to match one. Once a match has been made choice returns the result of the matching parser.

The ignore/1 parser matches but ignores the AST of its child parser.

The lookahead parser accepts a parser and matches it but does not update the context when it succeeds.

Examples

iex> alias Ergo.{Context, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Ergo.Terminals.wc())
iex> parser.(context)
%Context{status: :ok, ast: [?H, ?e, ?l, ?l, ?o], input: " World", index: 5, col: 6, char: ?o}

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Terminals.wc(), min: 6)
iex> parser.(context)
%Context{status: {:error, :many_less_than_min}, ast: nil, input: " World", char: ?o, index: 5, col: 6}

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Terminals.wc(), max: 3)
iex> parser.(context)
%Context{status: :ok, ast: [?H, ?e, ?l], input: "lo World", char: ?l, index: 3, col: 4}

iex> alias Ergo.{Context, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Ergo.Terminals.wc(), map: &Enum.count/1)
iex> parser.(context)
%Context{status: :ok, ast: 5, input: " World", index: 5, col: 6, char: ?o}

The not_lookahead parser accepts a parser and attempts to match it. If the match fails the not_lookahead parser returns status: :ok but does not affect the context otherwise.

Examples

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.optional(Terminals.literal("Hello"))
iex> parser.(context)
%Context{status: :ok, ast: "Hello", input: " World", index: 5, col: 6, char: ?o}

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new(" World")
iex> parser = Combinators.optional(Terminals.literal("Hello"))
iex> parser.(context)
%Context{status: :ok, ast: nil, input: " World", index: 0, col: 1, char: 0}

Examples

iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators, Parsers}
iex> context = Context.new("Hello World")
iex> parser = sequence([literal("Hello"), ws(), literal("World")])
iex> parser.(context)
%Context{status: :ok, ast: ["Hello", ?\s, "World"], char: ?d, index: 11, line: 1, col: 12}

iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators, Parsers}
iex> context = Context.new("Hello World")
iex> parser = sequence([literal("Hello"), ws(), literal("World")], map: fn ast -> Enum.join(ast, " ") end)
iex> parser.(context)
%Context{status: :ok, ast: "Hello 32 World", char: ?d, index: 11, line: 1, col: 12}

The transform/2 parser runs a transforming function on the AST of its child parser.

Link to this section Functions

Link to this function

choice(parsers, opts \\ [])

The choice/1 parser takes a list of parsers. It tries each in order attempting to match one. Once a match has been made choice returns the result of the matching parser.

Examples

iex> alias Ergo.Context
iex> import Ergo.{Terminals, Parsers, Combinators}
iex> context = Context.new("Hello World")
iex> parser = choice([literal("Foo"), literal("Bar"), literal("Hello"), literal("World")])
iex> parser.(context)
%Context{status: :ok, ast: "Hello", input: " World", char: ?o, index: 5, col: 6}

iex> alias Ergo.Context
iex> import Ergo.{Terminals, Parsers, Combinators}
iex> context = Context.new("Hello World")
iex> parser = choice([literal("Foo"), literal("Bar")])
iex> parser.(context)
%Context{status: {:error, :no_valid_choice}, message: "No valid choice", ast: nil, input: "Hello World"}

The ignore/1 parser matches but ignores the AST of its child parser.

Examples

iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators, Parsers}
iex> context = Context.new("Hello World")
iex> parser = sequence([literal("Hello"), ignore(ws()), literal("World")])
iex> parser.(context)
%Context{status: :ok, ast: ["Hello", "World"], index: 11, col: 12, char: ?d}
Link to this function

lookahead(parser)

The lookahead parser accepts a parser and matches it but does not update the context when it succeeds.

Example

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.lookahead(Terminals.literal("Hello"))
iex> parser.(context)
%Context{status: :ok, ast: nil, input: "Hello World", char: 0, index: 0, line: 1, col: 1}

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.lookahead(Terminals.literal("Helga"))
iex> parser.(context)
%Context{status: {:error, :lookahead_fail}, ast: [?l, ?e, ?H], char: ?l, index: 3, col: 4, input: "lo World"}
Link to this function

many(parser, opts \\ [])

Examples

iex> alias Ergo.{Context, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Ergo.Terminals.wc())
iex> parser.(context)
%Context{status: :ok, ast: [?H, ?e, ?l, ?l, ?o], input: " World", index: 5, col: 6, char: ?o}

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Terminals.wc(), min: 6)
iex> parser.(context)
%Context{status: {:error, :many_less_than_min}, ast: nil, input: " World", char: ?o, index: 5, col: 6}

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Terminals.wc(), max: 3)
iex> parser.(context)
%Context{status: :ok, ast: [?H, ?e, ?l], input: "lo World", char: ?l, index: 3, col: 4}

iex> alias Ergo.{Context, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.many(Ergo.Terminals.wc(), map: &Enum.count/1)
iex> parser.(context)
%Context{status: :ok, ast: 5, input: " World", index: 5, col: 6, char: ?o}
Link to this function

not_lookahead(parser)

The not_lookahead parser accepts a parser and attempts to match it. If the match fails the not_lookahead parser returns status: :ok but does not affect the context otherwise.

If the match succeeds the not_lookahead parser fails with {:error, :lookahead_fail}

Examples

iex> alias Ergo.{Context, Terminals, Combinators} iex> context = Context.new("Hello World") iex> parser = Combinators.not_lookahead(Terminals.literal("Foo")) iex> parser.(context) %Context{status: :ok, input: "Hello World"}

iex> alias Ergo.{Context, Terminals, Combinators} iex> context = Context.new("Hello World") iex> parser = Combinators.not_lookahead(Terminals.literal("Hello")) iex> parser.(context) %Context{status: {:error, :lookahead_fail}, input: "Hello World"}

Link to this function

optional(parser, opts \\ [])

Examples

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new("Hello World")
iex> parser = Combinators.optional(Terminals.literal("Hello"))
iex> parser.(context)
%Context{status: :ok, ast: "Hello", input: " World", index: 5, col: 6, char: ?o}

iex> alias Ergo.{Context, Terminals, Combinators}
iex> context = Context.new(" World")
iex> parser = Combinators.optional(Terminals.literal("Hello"))
iex> parser.(context)
%Context{status: :ok, ast: nil, input: " World", index: 0, col: 1, char: 0}
Link to this function

parse_many(parser, ctx, min, max, count)

Link to this function

sequence(parsers, opts \\ [])

Examples

iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators, Parsers}
iex> context = Context.new("Hello World")
iex> parser = sequence([literal("Hello"), ws(), literal("World")])
iex> parser.(context)
%Context{status: :ok, ast: ["Hello", ?\s, "World"], char: ?d, index: 11, line: 1, col: 12}

iex> alias Ergo.Context
iex> import Ergo.{Terminals, Combinators, Parsers}
iex> context = Context.new("Hello World")
iex> parser = sequence([literal("Hello"), ws(), literal("World")], map: fn ast -> Enum.join(ast, " ") end)
iex> parser.(context)
%Context{status: :ok, ast: "Hello 32 World", char: ?d, index: 11, line: 1, col: 12}
Link to this function

transform(parser, t_fn)

The transform/2 parser runs a transforming function on the AST of its child parser.

Examples

# Sum the digits
iex> alias Ergo.{Context, Terminals, Combinators}
iex> digit_to_int = fn d -> List.to_string([d]) |> String.to_integer() end
iex> t_fn = fn ast -> ast |> Enum.map(digit_to_int) |> Enum.sum() end
iex> context = Context.new("1234")
iex> parser_1 = Combinators.sequence([Terminals.digit(), Terminals.digit(), Terminals.digit(), Terminals.digit()])
iex> parser_2 = Combinators.transform(parser_1, t_fn)
iex> parser_2.(context)
%Context{status: :ok, ast: 10, char: ?4, index: 4, line: 1, col: 5}