combine v0.10.0 Combine.Parsers.Text View Source

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.

Link to this section 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

Link to this section Types

Link to this section Functions

Link to this function alphanumeric(parser \\ nil) View Source
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"]
Link to this function bin_digit(parser \\ nil) View Source
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]

This parser parses a single valid character from the input.

Example

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

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"]
Link to this function char(parser \\ nil, arg) View Source
char(previous_parser, String.t | pos_integer) :: 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]
Link to this function fixed_integer(parser \\ nil, size) View Source
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]

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]
Link to this function hex_digit(parser \\ nil) View Source
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"]

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]

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"]

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"]

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"]
Link to this function octal_digit(parser \\ nil) View Source
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]

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", " ", "!"]

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"]
Link to this function string(parser \\ nil, expected) View Source

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"]

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", "    ", "!"]
Link to this function take_while(parser \\ nil, predicate) View Source
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']

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"]

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"]
Link to this function word_of(parser \\ nil, pattern) View Source

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!"]