View Source Elnom.Character.Complete (elnom v0.1.0)
Parser functions recognizing specific characters
Summary
Functions
Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z
Recognizes one or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z
Recognizes zero or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z
Recognizes one or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z
Matches one utf-8 character.
Recognizes one utf-8 character.
Recognizes the string “\r\n”.
Recognizes zero or more ASCII numerical characters: 0-9
Recognizes one or more ASCII numerical characters: 0-9
Recognizes zero or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f
Recognizes one or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f
Parses a number in text form to an integer.
Recognizes an end of line (both ‘\n’ and ‘\r\n’).
Recognizes zero or more spaces, tabs, carriage returns and line feeds.
Recognizes one or more spaces, tabs, carriage returns and line feeds.
Matches a newline character ‘\n’.
Recognizes a character that is not in the provided characters.
Recognizes a string of any char except ‘\r\n’ or ‘\n’.
Recognizes zero or more octal characters: 0-7
Recognizes one or more octal characters: 0-7
Recognizes one of the provided utf-8 characters.
Recognizes one utf-8 character and checks that it satisfies a predicate
Recognizes zero or more spaces and tabs.
Recognizes one or more spaces and tabs.
Matches a tab character ‘\t’.
Functions
Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z
Complete version: Will return the whole input if no terminating token is found (a non alphabetic character).
iex> alpha0().("aB1c")
{:ok, "1c", "aB"}
iex> alpha0().("1c")
{:ok, "1c", ""}
iex> alpha0().("")
{:ok, "", ""}
Recognizes one or more lowercase and uppercase ASCII alphabetic characters: a-z, A-Z
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non alphabetic character).
iex> alpha1().("aB1c")
{:ok, "1c", "aB"}
iex> alpha1().("1c")
{:error, %Error{kind: :alpha, buffer: "1c"}}
iex> alpha1().("")
{:error, %Error{kind: :alpha, buffer: ""}}
Recognizes zero or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z
Complete version: Will return the whole input if no terminating token is found (a non alphanumerical character).
iex> alphanumeric0().("21cZ%1")
{:ok, "%1", "21cZ"}
iex> alphanumeric0().("%1")
{:ok, "%1", ""}
iex> alphanumeric0().("")
{:ok, "", ""}
Recognizes one or more ASCII numerical and alphabetic characters: 0-9, a-z, A-Z
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non alphanumerical character).
iex> alphanumeric1().("21cZ%1")
{:ok, "%1", "21cZ"}
iex> alphanumeric1().("%1")
{:error, %Error{kind: :alphanumeric, buffer: "%1"}}
iex> alphanumeric1().("")
{:error, %Error{kind: :alphanumeric, buffer: ""}}
Matches one utf-8 character.
Complete version: Will return an error if there’s not enough input data.
iex> anychar().("abc")
{:ok, "bc", "a"}
iex> anychar().("")
{:error, %Error{kind: :anychar, buffer: ""}}
Recognizes one utf-8 character.
Complete version: Will return an error if there’s not enough input data.
iex> char("h").("hello")
{:ok, "ello", "h"}
iex> char("e").("hello")
{:error, %Error{kind: :char, buffer: "hello"}}
Recognizes the string “\r\n”.
Complete version: Will return an error if there’s not enough input data.
iex> crlf().("\r\nc")
{:ok, "c", "\r\n"}
iex> crlf().("ab\r\nc")
{:error, %Error{kind: :crlf, buffer: "ab\r\nc"}}
iex> crlf().("")
{:error, %Error{kind: :crlf, buffer: ""}}
Recognizes zero or more ASCII numerical characters: 0-9
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non digit character).
iex> digit0().("21c")
{:ok, "c", "21"}
iex> digit0().("21")
{:ok, "", "21"}
iex> digit0().("a21c")
{:ok, "a21c", ""}
iex> digit0().("")
{:ok, "", ""}
Recognizes one or more ASCII numerical characters: 0-9
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non digit character).
iex> digit1().("213c")
{:ok, "c", "213"}
iex> digit1().("c1")
{:error, %Error{kind: :digit, buffer: "c1"}}
iex> digit1().("")
{:error, %Error{kind: :digit, buffer: ""}}
You can use this function in combination with Elnom.Combinator.map/2
to parse an integer:
iex> parser = map(digit1(), &String.to_integer/1)
iex> parser.("123abc")
{:ok, "abc", 123}
Recognizes zero or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f
Complete version: Will return the whole input if no terminating token is found (a non hexadecimal digit character).
iex> hex_digit0().("21cZ")
{:ok, "Z", "21c"}
iex> hex_digit0().("%1")
{:ok, "%1", ""}
iex> hex_digit0().("")
{:ok, "", ""}
Recognizes one or more ASCII hexadecimal numerical characters: 0-9, A-F, a-f
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non hexadecimal digit character).
iex> hex_digit1().("21cZ")
{:ok, "Z", "21c"}
iex> hex_digit1().("%1")
{:error, %Error{kind: :hex_digit, buffer: "%1"}}
iex> hex_digit1().("")
{:error, %Error{kind: :hex_digit, buffer: ""}}
See integer/0
See integer/0
See integer/0
See integer/0
See integer/0
Parses a number in text form to an integer.
This function does not exist in Rust's nom library because it uses u8(), i8, u16(), etc. to convert to integers. Elixir doesn't have these types, so this single function can translate a string to an integer of any size.
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non digit character).
iex> integer().("123abc")
{:ok, "abc", 123}
iex> integer().("abc")
{:error, %Error{kind: :digit, buffer: "abc"}}
iex> integer().("")
{:error, %Error{kind: :digit, buffer: ""}}
Recognizes an end of line (both ‘\n’ and ‘\r\n’).
Complete version: Will return an error if there’s not enough input data.
iex> line_ending().("\r\nc")
{:ok, "c", "\r\n"}
iex> line_ending().("ab\r\nc")
{:error, %Error{kind: :cr_lf, buffer: "ab\r\nc"}}
iex> line_ending().("")
{:error, %Error{kind: :cr_lf, buffer: ""}}
Recognizes zero or more spaces, tabs, carriage returns and line feeds.
Complete version: will return the whole input if no terminating token is found (a non space character).
iex> multispace0().(" \t\n\r21c")
{:ok, "21c", " \t\n\r"}
iex> multispace0().("Z21c")
{:ok, "Z21c", ""}
iex> multispace0().("")
{:ok, "", ""}
Recognizes one or more spaces, tabs, carriage returns and line feeds.
Complete version: will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non space character).
iex> multispace1().(" \t\n\r21c")
{:ok, "21c", " \t\n\r"}
iex> multispace1().("Z21c")
{:error, %Error{kind: :multispace, buffer: "Z21c"}}
iex> multispace1().("")
{:error, %Error{kind: :multispace, buffer: ""}}
Matches a newline character ‘\n’.
Complete version: Will return an error if there’s not enough input data.
iex> newline().("\nc")
{:ok, "c", "\n"}
iex> newline().("\r\nc")
{:error, %Error{kind: :newline, buffer: "\r\nc"}}
iex> newline().("")
{:error, %Error{kind: :newline, buffer: ""}}
Recognizes a character that is not in the provided characters.
Complete version: Will return an error if there’s not enough input data.
iex> none_of("abc").("z")
{:ok, "", "z"}
iex> none_of("ab").("a")
{:error, %Error{kind: :none_of, buffer: "a"}}
iex> none_of("a").("")
{:error, %Error{kind: :none_of, buffer: ""}}
Recognizes a string of any char except ‘\r\n’ or ‘\n’.
Complete version: Will return an error if there’s not enough input data.
iex> not_line_ending().("ab\r\nc")
{:ok, "\r\nc", "ab"}
iex> not_line_ending().("ab\nc")
{:ok, "\nc", "ab"}
iex> not_line_ending().("abc")
{:ok, "", "abc"}
iex> not_line_ending().("")
{:ok, "", ""}
iex> not_line_ending().("a\rb\nc")
{:error, %Error{kind: :tag, buffer: "a\rb\nc"}}
Recognizes zero or more octal characters: 0-7
Complete version: Will return the whole input if no terminating token is found (a non octal digit character).
iex> oct_digit0().("21cZ")
{:ok, "cZ", "21"}
iex> oct_digit0().("%1")
{:ok, "%1", ""}
iex> oct_digit0().("")
{:ok, "", ""}
Recognizes one or more octal characters: 0-7
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non octal digit character).
iex> oct_digit1().("218cZ")
{:ok, "8cZ", "21"}
iex> oct_digit1().("%1")
{:error, %Error{kind: :oct_digit, buffer: "%1"}}
iex> oct_digit1().("")
{:error, %Error{kind: :oct_digit, buffer: ""}}
Recognizes one of the provided utf-8 characters.
Complete version: Will return an error if there’s not enough input data.
iex> one_of("abc").("b")
{:ok, "", "b"}
iex> one_of("a").("bc")
{:error, %Error{kind: :one_of, buffer: "bc"}}
iex> one_of("a").("")
{:error, %Error{kind: :one_of, buffer: ""}}
Recognizes one utf-8 character and checks that it satisfies a predicate
Complete version: Will return an error if there’s not enough input data.
iex> parser = satisfy(&(&1 == ?a || &1 == ?b))
iex> parser.("abc")
{:ok, "bc", "a"}
iex> parser.("cd")
{:error, %Error{kind: :satisfy, buffer: "cd"}}
iex> parser.("")
{:error, %Error{kind: :satisfy, buffer: ""}}
Recognizes zero or more spaces and tabs.
Complete version: Will return the whole input if no terminating token is found (a non space character).
iex> space0().(" \t21c")
{:ok, "21c", " \t"}
iex> space0().("Z21c")
{:ok, "Z21c", ""}
iex> space0().("")
{:ok, "", ""}
Recognizes one or more spaces and tabs.
Complete version: Will return an error if there’s not enough input data, or the whole input if no terminating token is found (a non space character).
iex> space1().(" \t21c")
{:ok, "21c", " \t"}
iex> space1().("Z21c")
{:error, %Error{kind: :space, buffer: "Z21c"}}
iex> space1().("")
{:error, %Error{kind: :space, buffer: ""}}
Matches a tab character ‘\t’.
Complete version: Will return an error if there’s not enough input data.
iex> tab().("\tc")
{:ok, "c", "\t"}
iex> tab().("\r\nc")
{:error, %Error{kind: :char, buffer: "\r\nc"}}
iex> tab().("")
{:error, %Error{kind: :char, buffer: ""}}
See integer/0
See integer/0
See integer/0
See integer/0
See integer/0