Ergo.Parser (Ergo v0.8.0)

Ergo.Parser contains the Parser record type. Ergo parsers are anonymous functions but we embed them in a Parser record that can hold arbitrary metadata. The primary use for the metadata is the storage of debugging information.

Link to this section Summary

Functions

Create a new combinator parser with the given label, parsing function, and optional metadata.

invoke/2 is the main entry point for the parsing process. It looks up the parser control function within the Context and uses it to run the given parser.

Create a new terminal parser with the given label, parsing function, and optional metadata.

track_parser first checks if the parser has already been tracked for the current input index and, if it has, raises a CycleError to indicate the parser is in a loop. Otherwise it adds the parser at the current index.

Link to this section Functions

Link to this function

combinator(type, label, parser_fn, meta \\ [])

Create a new combinator parser with the given label, parsing function, and optional metadata.

Link to this function

invoke(ctx, parser)

invoke/2 is the main entry point for the parsing process. It looks up the parser control function within the Context and uses it to run the given parser.

This indirection allows a different control function to be specified, e.g. by the diagnose entry point which can wrap the parser call, while still calling the same parsing function (i.e. we are not introducing debugging variants of the parsers that could be subject to different behaviours)

Link to this function

pop_entry_point(ctx)

Link to this function

push_entry_point(ctx)

Link to this function

terminal(type, label, parser_fn, meta \\ [])

Create a new terminal parser with the given label, parsing function, and optional metadata.

Link to this function

track_parser(ctx)

track_parser first checks if the parser has already been tracked for the current input index and, if it has, raises a CycleError to indicate the parser is in a loop. Otherwise it adds the parser at the current index.

Examples

iex> alias Ergo.{Context, Parser} iex> import Ergo.{Terminals, Combinators} iex> parser = many(char(?H)) iex> context = ...> Context.new("Hello World") ...> |> Map.put(:parser, parser) ...> |> Parser.track_parser() iex> assert Context.parser_tracked?(context, parser.ref)