Elex.Parser.ErrorFormatter (Elex v0.2.0)

View Source

Translates 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

humanize(expression, rest, byte_offset)

@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 parse
  • rest - the unconsumed remainder reported by the parser at the furthest position it reached
  • byte_offset - the byte offset of that position into expression

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"