View Source Minipeg.Parser (Minipeg v0.7.5)

A struct containung a parser function and a name

Summary

Functions

Create a new struct

Parses an input of type Input.t (and potentially a cache) with a given parser and returns a result of type Success.t or Failure.t

A convenience function wrapping parse such as we can use a string as input and get and {:ok, ast} | {:error, reason} tuple response

Another convenience function, unwrapping an :ok result of parse_string and raising a Minipeg.Error otherwise

Types

ast_list_t()

@type ast_list_t() :: [ast_t()]

ast_t()

@type ast_t() :: any()

atoms()

@type atoms() :: [atom()]

binaries()

@type binaries() :: [binary()]

binary?()

@type binary?() :: maybe(binary())

char_set_spec_t()

@type char_set_spec_t() :: maybe(binary() | list())

char_set_t()

@type char_set_t() :: [binary()] | binary()

either(success_t, error_t)

@type either(success_t, error_t) :: {:ok, success_t} | {:error, error_t}

input_t()

@type input_t() :: binary() | [binary()] | Minipeg.Input.t()

maybe(t)

@type maybe(t) :: nil | t

name_t()

@type name_t() :: atom() | binary()

name_t?()

@type name_t?() :: maybe(name_t())

parser_function_t()

@type parser_function_t() :: (Minipeg.Input.t(), Minipeg.Cache.t(), binary() ->
                          result_t())

position_t()

@type position_t() :: {pos_integer(), pos_integer()}

result_t()

@type result_t() :: Minipeg.Failure.t() | Minipeg.Ignore.t() | Minipeg.Success.t()

result_tuple_t(ast_t)

@type result_tuple_t(ast_t) :: {:ok, ast_t} | {:error, binary()}

satisfier_result_t()

@type satisfier_result_t() :: either(any(), binary())

satisfier_t()

@type satisfier_t() :: (any() -> satisfier_result_t())

str_or_count_t()

@type str_or_count_t() :: binary() | non_neg_integer()

t()

@type t() :: %Minipeg.Parser{name: binary(), parser_function: parser_function_t()}

token1_comp_t()

@type token1_comp_t() ::
  {atom(), token_parse_part2()}
  | {atom(), token_parse_part2(), (binary() -> any())}

token1_spec_t()

@type token1_spec_t() ::
  {atom(), token_parse_part1()}
  | {atom(), token_parse_part1(), (binary() -> any())}

token_comp_t()

@type token_comp_t() :: [token1_comp_t()]

token_spec_t()

@type token_spec_t() :: [token1_spec_t()]

Functions

new(name, parser_function)

@spec new(binary(), parser_function_t()) :: t()

Create a new struct

parse(parser, input, cache \\ %Cache{})

@spec parse(t(), Minipeg.Input.t(), Minipeg.Cache.t()) :: result_t()

Parses an input of type Input.t (and potentially a cache) with a given parser and returns a result of type Success.t or Failure.t

iex(1)> case parse(char_parser("a"), Input.new("abc")) do
...(1)>   %Success{ast: ast, rest: %Input{input: rest}} -> {ast, rest}
...(1)> end
{"a", "bc"}


iex(2)> case parse(char_parser("a"), Input.new("")) do
...(2)>   %Failure{reason: reason} -> reason
...(2)> end
"encountered end of input"

parse_string(parser, input_string)

@spec parse_string(t(), binary()) :: result_tuple_t(any())

A convenience function wrapping parse such as we can use a string as input and get and {:ok, ast} | {:error, reason} tuple response

iex(3)> parse_string(char_parser("a"), "abc")
{:ok, "a"}


iex(4)> parse_string(char_parser("a"), "")
{:error, "encountered end of input (in char_parser(\"a\")) in <binary>:1,1"}

parse_string!(parser, input_string)

@spec parse_string!(t(), binary()) :: any()

Another convenience function, unwrapping an :ok result of parse_string and raising a Minipeg.Error otherwise

iex(5)> parse_string!(char_parser("a"), "abc")
"a"


iex(6)> assert_raise(Error, fn -> parse_string!(char_parser("a"), "") end)