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

Specs

alphanumeric :: 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"]
alphanumeric(parser)

Specs

alphanumeric(parser) :: parser
bin_digit()

Specs

bin_digit :: 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]
bin_digit(parser)

Specs

bin_digit(parser) :: parser
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, arg)

Specs

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

Specs

digit :: parser

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]
digit(parser)

Specs

digit(parser) :: parser
fixed_integer(size)

Specs

fixed_integer(-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]
fixed_integer(parser, size)

Specs

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

Specs

float :: parser

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]
float(parser)

Specs

float(parser) :: parser
hex_digit()

Specs

hex_digit :: 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"]
hex_digit(parser)

Specs

hex_digit(parser) :: parser
integer()

Specs

integer :: 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]
integer(parser)

Specs

integer(parser) :: parser
letter()

Specs

letter :: 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"]
letter(parser)

Specs

letter(parser) :: parser
lower()

Specs

lower :: parser

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"]
lower(parser)

Specs

lower(parser) :: parser
newline()

Specs

newline :: 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"]
newline(parser)

Specs

newline(parser) :: parser
octal_digit()

Specs

octal_digit :: 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]
octal_digit(parser)

Specs

octal_digit(parser) :: parser
space()

Specs

space :: parser

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", " ", "!"]
space(parser)

Specs

space(parser) :: parser
spaces()

Specs

spaces :: 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"]
spaces(parser)

Specs

spaces(parser) :: parser
string(expected)

Specs

string(String.t) :: parser

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"]
string(parser, expected)

Specs

string(parser, String.t) :: parser
tab()

Specs

tab :: parser

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", "    ", "!"]
tab(parser)

Specs

tab(parser) :: parser
take_while(predicate)

Specs

take_while((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']
take_while(parser, predicate)

Specs

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

Specs

upper :: parser

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"]
upper(parser)

Specs

upper(parser) :: parser
word()

Specs

word :: parser

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

Specs

word(parser) :: parser
word_of(pattern)

Specs

word_of(Regex.t) :: parser

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!"]
word_of(parser, pattern)

Specs

word_of(parser, Regex.t) :: parser