combine v0.9.2 Combine.Parsers.Text

This module defines common textual parsers, i.e. char, word, space, etc. To use them, just add import Combine.Parsers.Text to your module, or reference them directly.

Summary

Functions

Parses any alphanumeric character (0-9, A-Z, a-z)

Parses any binary digit (0 | 1)

This parser parses a single valid character from the input

Parses a single character from the input and verifies it matches the provided character

Parses any digit (0..9). Result is returned as an integer

Parses an integer value from the input with a fixed width

Parses a floating point number from the input

Parses any hexadecimal digit (0-9, A-F, a-f)

Parses an integer value from the input

Parses any letter in the English alphabet (A..Z or a..z)

Parses any lower case character

This parser will parse a single newline from the input, this can be either LF, or CRLF newlines ( or ). The result is normalized to

Parses any octal digit (0-7)

This parser parses a single space character from the input

Parses spaces until a non-space character is encountered. Returns all spaces collapsed as a single result

Parses the given string constant from the input

This parser will parse a single tab character from the input

Consumes input character-by-character while the provided predicate is true. This parser cannot fail, so do not use it with many1/many, as it will never terminate

Parses any upper case character

Parses a string consisting of word characters from the input

Parses a string where each character matches the provided regular expression

Types

Functions

alphanumeric(parser \\ nil)

Specs

alphanumeric(previous_parser) :: parser

Parses any alphanumeric character (0-9, A-Z, a-z).

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("d3", alphanumeric)
["d"]
...> Combine.parse("d3", alphanumeric |> alphanumeric)
["d", "3"]
bin_digit(parser \\ nil)

Specs

bin_digit(previous_parser) :: parser

Parses any binary digit (0 | 1).

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("1010", bin_digit)
[1]
...> Combine.parse("1010", bin_digit |> bin_digit)
[1, 0]
char()

Specs

char :: parser

This parser parses a single valid character from the input.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("Hi", char)
["H"]
char(c)

Specs

char(parser | String.t | pos_integer) :: parser

Parses a single character from the input and verifies it matches the provided character.

Example

iex> import Elixir.Combine.Parsers.Text
...> parser = char("H")
...> Combine.parse("Hi!", parser)
["H"]
...> parser = char(?H)
...> Combine.parse("Hi!", parser)
["H"]
char(parser \\ nil, arg)

Specs

char(parser, String.t | pos_integer) :: parser
digit(parser \\ nil)

Specs

Parses any digit (0..9). Result is returned as an integer.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("1010", digit)
[1]
...> Combine.parse("1010", digit |> digit)
[1, 0]
fixed_integer(parser \\ nil, size)

Specs

fixed_integer(previous_parser, -1 | pos_integer) :: parser

Parses an integer value from the input with a fixed width

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("123, stuff", fixed_integer(3))
[123]
...> Combine.parse(":1234", char |> fixed_integer(3))
[":", 123]
float(parser \\ nil)

Specs

Parses a floating point number from the input.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("1234.5, stuff", float)
[1234.5]
...> Combine.parse("float: 1234.5", word |> char(":") |> space |> float)
["float", ":", " ", 1234.5]
hex_digit(parser \\ nil)

Specs

hex_digit(previous_parser) :: parser

Parses any hexadecimal digit (0-9, A-F, a-f).

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("d3adbeeF", hex_digit)
["d"]
...> Combine.parse("d3adbeeF", hex_digit |> hex_digit)
["d", "3"]
integer(parser \\ nil)

Specs

integer(previous_parser) :: parser

Parses an integer value from the input.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("1234, stuff", integer)
[1234]
...> Combine.parse("stuff, 1234", word |> char(",") |> space |> integer)
["stuff", ",", " ", 1234]
letter(parser \\ nil)

Specs

letter(previous_parser) :: parser

Parses any letter in the English alphabet (A..Z or a..z).

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("hi", letter)
["h"]
...> Combine.parse("hi", char("h") |> letter)
["h", "i"]
lower(parser \\ nil)

Specs

Parses any lower case character.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("hi", lower)
["h"]
...> Combine.parse("Hi", char("H") |> lower)
["H", "i"]
newline(parser \\ nil)

Specs

newline(previous_parser) :: parser

This parser will parse a single newline from the input, this can be either LF, or CRLF newlines ( or ). The result is normalized to .

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("\r\n", newline)
["\n"]
...> Combine.parse("H\r\n", upper |> newline)
["H", "\n"]
octal_digit(parser \\ nil)

Specs

octal_digit(previous_parser) :: parser

Parses any octal digit (0-7).

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("3157", octal_digit)
[3]
...> Combine.parse("3157", octal_digit |> octal_digit)
[3, 1]
space(parser \\ nil)

Specs

This parser parses a single space character from the input.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("  ", space)
[" "]
...> parser = char("h") |> char("i") |> space |> char("!")
...> Combine.parse("hi !", parser)
["h", "i", " ", "!"]
spaces(parser \\ nil)

Specs

spaces(previous_parser) :: parser

Parses spaces until a non-space character is encountered. Returns all spaces collapsed as a single result.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("   hi!", spaces)
[" "]
...> Combine.parse("Hi   Paul", string("Hi") |> spaces |> string("Paul"))
["Hi", " ", "Paul"]
string(parser \\ nil, expected)

Specs

Parses the given string constant from the input.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("Hi Paul", string("Hi"))
["Hi"]
...> Combine.parse("Hi Paul", string("Hi") |> space |> string("Paul"))
["Hi", " ", "Paul"]
tab(parser \\ nil)

Specs

This parser will parse a single tab character from the input.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("    ", tab)
["  "]
...> parser = char("h") |> char("i") |> tab |> char("!")
...> Combine.parse("hi  !", parser)
["h", "i", "    ", "!"]
take_while(parser \\ nil, predicate)

Specs

take_while(previous_parser, (char -> boolean)) :: parser

Consumes input character-by-character while the provided predicate is true. This parser cannot fail, so do not use it with many1/many, as it will never terminate.

Examples

iex> import Elixir.Combine.Parsers.Text
...> parser = take_while(fn ?a -> true; _ -> false end)
...> Combine.parse("aaaaabbbbb", parser)
['aaaaa']
upper(parser \\ nil)

Specs

Parses any upper case character.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("Hi", upper)
["H"]
...> Combine.parse("HI", char("H") |> upper)
["H", "I"]
word(parser \\ nil)

Specs

Parses a string consisting of word characters from the input.

Example

iex> import Elixir.Combine.Parsers.Text
...> Combine.parse("Hi, Paul", word)
["Hi"]
...> Combine.parse("Hi Paul", word |> space |> word)
["Hi", " ", "Paul"]
word_of(parser \\ nil, pattern)

Specs

Parses a string where each character matches the provided regular expression.

Example

iex> import Elixir.Combine.Parsers.Text
...> valid_chars = ~r/[!:_\-\w]+/
...> Combine.parse("something_with-special:characters!", word_of(valid_chars))
["something_with-special:characters!"]