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