Localize.ParseError exception (Localize v0.25.0)

Copy Markdown View Source

Exception raised when a language tag, unit identifier, or MF2 message cannot be parsed.

For MF2 message parse errors, the exception carries structured source location information (:offset, :line, :column) describing where in the input the parser failed. :line and :column are 1-indexed and :offset is a 0-indexed byte offset into the input string. This information is intended for tooling — editor integrations, language servers, and CLI diagnostics — that need to map errors back to source positions.

For other uses (language tag / unit identifier parsing) the location fields may be nil.

Summary

Functions

Computes 1-indexed line and column for a byte offset into input.

Types

t()

@type t() :: %Localize.ParseError{
  __exception__: true,
  column: pos_integer() | nil,
  input: String.t() | nil,
  line: pos_integer() | nil,
  offset: non_neg_integer() | nil,
  reason: String.t() | nil,
  rest: String.t() | nil
}

Functions

line_column(input, offset)

@spec line_column(String.t(), non_neg_integer()) :: {pos_integer(), pos_integer()}

Computes 1-indexed line and column for a byte offset into input.

Arguments

  • input is the source string.

  • offset is a 0-indexed byte offset into input.

Returns

  • {line, column} where both are 1-indexed positive integers.

  • If offset is out of bounds, returns the position of the last character (or {1, 1} for an empty input).

Examples

iex> Localize.ParseError.line_column("Hello\nworld", 0)
{1, 1}

iex> Localize.ParseError.line_column("Hello\nworld", 6)
{2, 1}

iex> Localize.ParseError.line_column("Hello\nworld", 9)
{2, 4}