Elex.Parser.ErrorFormatter (Elex v0.2.1)
View SourceTranslates low-level parser failures into human-readable error messages.
The underlying NimbleParsec parser reports failures in terms of the grammar
combinators it tried (for example "expected end of string"). Those messages
are meaningless to someone writing an expression, so this module inspects the
original input together with the unconsumed remainder and produces a short,
plain-English description of what went wrong.
It is used by Elex.Parser.parse/3; most callers do
not need to use it directly.
Summary
Functions
Builds a human-readable message for a parser failure.
Functions
@spec humanize(String.t(), String.t(), non_neg_integer()) :: String.t()
Builds a human-readable message for a parser failure.
Parameters
expression- the original expression string that failed to parserest- the unconsumed remainder reported by the parser at the furthest position it reachedbyte_offset- the byte offset of that position intoexpression
Examples
iex> Elex.Parser.ErrorFormatter.humanize("( 1 + 2", "( 1 + 2", 0)
"closing parenthesis is missing"
iex> Elex.Parser.ErrorFormatter.humanize("1 +", "+", 2)
"a value is missing after '+'"
iex> Elex.Parser.ErrorFormatter.humanize("max(1 2)", "2)", 6)
"arguments must be separated by commas"