Minipeg.Parsers (Minipeg v0.7.8)

Copy Markdown View Source

A collection of basic parsers

Summary

Functions

Parses any character in the specified set, defaulting to all characters

Parser that only succeeds on empty input / end of input

The escaped_char_parser parses a character after an escape returning only the escaped char

Succeeds if any of the given keywords parse

Parses only an exact occurrance of the literal string

Combines many characters into one string, this is a convenience for

This is the pendant to the many_chars_parser. It needs the negated charset to be defined though

Parses any character but delimiter, unless it is escpaed by escape_char or doubled in case allow_doubles is true.

Parses any char with the exception of chars in the forbidden set

parses only if parser parses a string with a prefix that is discarded

This parser always succeeds. N.B. It does not advance the input

parses a string up to the first char in charset fails iff less then min_count chars are parsed. If a character from charset is encountered it is removed from the input stream, but is not returned in the ast.

Parses a sequence of at least min_count whitespaces. min_count defaults to 0

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_class_t()

@type char_class_t() ::
  :alnum
  | :alpha
  | :blank
  | :cntrl
  | :digit
  | :graph
  | :lower
  | :print
  | :punct
  | :space
  | :upper
  | :word
  | :xdigit

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()

many_chars_option()

@type many_chars_option() :: {:min, non_neg_integer()} | {:escape, binary()}

many_chars_options()

@type many_chars_options() :: [many_chars_option()]

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()

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()

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

char_parser(specified_set \\ nil, name \\ nil)

@spec char_parser(maybe(atom() | binary() | list()), binary?()) :: Minipeg.Parser.t()

See Minipeg.Parsers.BasicParsers.char_parser/2.

delimited_string_parser(delimiter, escape_char \\ "\\", allow_double_escapes \\ true, name \\ nil)

@spec delimited_string_parser(binary(), binary(), boolean(), binary?()) ::
  Minipeg.Parser.t()

Parses any character in the specified set, defaulting to all characters

Succeeds if next char in input is in the specified set Fails if input is empty or next char is not in the specified set

If the specified charset is an atom it is interpreted as a POSIX character class described in the docs of the Regex module, here are the currently supported values:

  :alnum | :alpha | :blank | :cntrl | :digit | :graph | :lower | :print | :punct | :space | :upper | :word | :xdigit

end_parser(name \\ nil)

@spec end_parser(binary?()) :: Minipeg.Parser.t()

Parser that only succeeds on empty input / end of input

escaped_char_parser(escape_char \\ "\\", name \\ nil, allowed_set \\ nil)

@spec escaped_char_parser(binary?(), binary?(), char_set_spec_t()) ::
  Minipeg.Parser.t()

The escaped_char_parser parses a character after an escape returning only the escaped char

iex(1)> parse_string(escaped_char_parser(), "\\a")
{:ok, "a"}

However only escaped chars are parsed

iex(2)> {:error, _} = parse_string(escaped_char_parser(), "a")

The default escape character can be changed

iex(3)> parse_string(escaped_char_parser(","), ",b")
{:ok, "b"}

iex(4)> {:error, _} = parse_string(escaped_char_parser(","), "\\b")

failure_parser(reason \\ nil, name \\ nil)

@spec failure_parser(binary?(), binary?()) :: Minipeg.Parser.t()

This parser always fails

ident_parser(name \\ nil, opts \\ [])

@spec ident_parser(binary?(), Keyword.t()) :: Minipeg.Parser.t()

See Minipeg.Parsers.CommonParsers.ident_parser/2.

int_parser(name \\ nil)

@spec int_parser(binary?()) :: Minipeg.Parser.t()

See Minipeg.Parsers.CommonParsers.int_parser/1.

keywords_parser(keywords, name \\ nil)

@spec keywords_parser([binary()], binary?()) :: Minipeg.Parser.t()

Succeeds if any of the given keywords parse

literal_parser(literal, name \\ nil)

@spec literal_parser(binary(), binary?()) :: Minipeg.Parser.t()

Parses only an exact occurrance of the literal string

many_chars_parser(charset \\ nil, opts \\ [])

@spec many_chars_parser(maybe(binary() | list()), many_chars_options()) ::
  Minipeg.Parser.t()

Combines many characters into one string, this is a convenience for

    char_parser(...) |> many(...) |> map_to_string

By default many gets min set to 1

iex(5)> parse_string(many_chars_parser(), "a")
{:ok, "a"}

iex(6)> parse_string(many_chars_parser(), "")
{:error, "Missing 1 parses in many (in char_parser()) in <binary>:1,1"}

We can change the default minimum though

iex(7)> parse_string(many_chars_parser(nil, min: 2), "a")
{:error, "Missing 1 parses in many (in char_parser()) in <binary>:1,1"}

iex(8)> parse_string(many_chars_parser(nil, min: 2), "ab")
{:ok, "ab"}

iex(9)> parse_string(many_chars_parser(nil, min: 0), "")
{:ok, ""}

If we define a charset only characters from the set are parsed

iex(10)> parse_string(many_chars_parser("abc"), "axab")
{:ok, "a"}

iex(11)> {:error, _} = parse_string(many_chars_parser("abc"), "xab")

We can however use escape to allow any character

iex(12)> parser = many_chars_parser("ab")
...(12)> parse_string(parser, "\\xa\\bc")
{:ok, "xab"}

many_not_chars_parser(charset, opts \\ [])

@spec many_not_chars_parser(binary() | list(), many_chars_options()) ::
  Minipeg.Parser.t()

This is the pendant to the many_chars_parser. It needs the negated charset to be defined though

iex(13)> parse_string(many_not_chars_parser("xy"), "aby")
{:ok, "ab"}

iex(14)> parser = many_not_chars_parser("xy")
...(14)> parse_string(parser, "ab\\ycx")
{:ok, "abyc"}

maybe_escaped_char_parser(delimiter, escape_char \\ "\\", allow_doubles \\ true, name \\ nil)

@spec maybe_escaped_char_parser(binary(), binary(), boolean(), binary?()) ::
  Minipeg.Parser.t()

Parses any character but delimiter, unless it is escpaed by escape_char or doubled in case allow_doubles is true.

not_char_parser(forbidden, name \\ nil)

@spec not_char_parser(char_set_t(), binary?()) :: Minipeg.Parser.t()

Parses any char with the exception of chars in the forbidden set

prefixed_parser(prefix, parser, name \\ nil)

@spec prefixed_parser(char_set_t(), Minipeg.Parser.t(), binary?()) ::
  Minipeg.Parser.t()

parses only if parser parses a string with a prefix that is discarded

rgx_capture_parser(rgx_string, name \\ nil, options \\ [])

@spec rgx_capture_parser(binary(), binary?(), atoms()) :: Minipeg.Parser.t()

See Minipeg.Parsers.RgxParsers.rgx_capture_parser/3.

rgx_match_parser(rgx_string, name \\ nil, options \\ [])

@spec rgx_match_parser(binary(), binary?(), atoms()) :: Minipeg.Parser.t()

See Minipeg.Parsers.RgxParsers.rgx_match_parser/3.

rgx_parser(rgx_string, name \\ nil, options \\ [])

@spec rgx_parser(binary(), binary?(), atoms()) :: Minipeg.Parser.t()

See Minipeg.Parsers.RgxParsers.rgx_parser/3.

string_parser(paren_chars \\ ["\"", "'"], escape_char \\ "\\", allow_double_escapes \\ true, name \\ nil)

@spec string_parser(char_set_t(), binary?(), boolean(), binary?()) ::
  Minipeg.Parser.t()

A classic string parser which parses input like

  • ""
  • '"a\\'b"c'
  • "ab""c"

but not

  • 'ab
  • "abc'
  • "ad"e (e not parsed)

success_parser(return_ast \\ nil, name \\ nil)

@spec success_parser(ast_t(), binary?()) :: Minipeg.Parser.t()

This parser always succeeds. N.B. It does not advance the input

token_parser(tokens, options \\ [])

@spec token_parser(token_spec_t(), Keyword.t()) :: Minipeg.Parser.t()

unsigned_int_parser(name \\ nil)

@spec unsigned_int_parser(binary?()) :: Minipeg.Parser.t()

See Minipeg.Parsers.CommonParsers.unsigned_int_parser/1.

upto_parser(charset, min_count \\ 0, name \\ nil)

@spec upto_parser(char_set_t(), non_neg_integer(), binary?()) :: Minipeg.Parser.t()

parses a string up to the first char in charset fails iff less then min_count chars are parsed. If a character from charset is encountered it is removed from the input stream, but is not returned in the ast.

ws_parser(allow_newline \\ false, min_count \\ 0, name \\ nil)

@spec ws_parser(boolean(), non_neg_integer(), binary?()) :: Minipeg.Parser.t()

Parses a sequence of at least min_count whitespaces. min_count defaults to 0