Lua.Parser.Error (Lua v1.0.0-rc.3)
View SourceBeautiful error reporting for the Lua parser.
Provides detailed error messages with:
- Source code context with line numbers
- Visual indicators pointing to the error location
- Helpful suggestions for common mistakes
- Multiple error reporting
Summary
Types
Wire-safe representation produced by to_map/2. Mirrors the shape of
Lua.VM.ErrorFormatter.to_map/3 so runtime and parse errors render
through one path. source, call_stack, and error_kind are constant
for parse errors (no file name, stack, or error kind) and exist only for
shape parity.
Functions
Creates an error for expected token.
Formats an error into a beautiful multi-line string with context.
Formats multiple errors together.
Creates a new error.
Returns a wire-safe structured representation of a parse error.
Creates an error for unclosed delimiter.
Creates an error for unexpected end of input.
Creates an error for unexpected token.
Types
@type error_type() ::
:unexpected_token
| :unexpected_end
| :expected_token
| :unclosed_delimiter
| :invalid_syntax
| :lexer_error
| :multiple_errors
@type position() :: Lua.AST.Meta.position()
@type source_context() :: %{ lines: [%{number: pos_integer(), text: String.t(), highlight?: boolean()}], pointer_column: pos_integer() }
@type wire_error() :: %{ type: error_type(), message: String.t() | nil, source: nil, line: pos_integer() | nil, call_stack: [], source_context: source_context() | nil, suggestion: String.t() | nil, error_kind: nil }
Wire-safe representation produced by to_map/2. Mirrors the shape of
Lua.VM.ErrorFormatter.to_map/3 so runtime and parse errors render
through one path. source, call_stack, and error_kind are constant
for parse errors (no file name, stack, or error kind) and exist only for
shape parity.
Functions
Creates an error for expected token.
Formats an error into a beautiful multi-line string with context.
Formats multiple errors together.
@spec new(error_type(), String.t(), position() | nil, keyword()) :: t()
Creates a new error.
@spec to_map(t(), String.t() | nil) :: wire_error()
Returns a wire-safe structured representation of a parse error.
The shape is identical to Lua.VM.ErrorFormatter.to_map/3, so runtime and
parse errors can flow through a single renderer (HTML, JSON, structured
logs). No ANSI escapes appear in any string field, and leading/trailing
whitespace from the internal message/suggestion templates is trimmed.
%{
type: atom(),
message: String.t(),
source: String.t() | nil,
line: pos_integer() | nil,
call_stack: [],
source_context: %{
lines: [%{number: pos_integer(), text: String.t(), highlight?: boolean()}],
pointer_column: pos_integer()
} | nil,
suggestion: String.t() | nil,
error_kind: nil
}Parse errors carry no call stack or error kind, so call_stack is always
[] and error_kind is always nil; they are present for shape parity.
source is nil because the parser does not track a file name.
Pass the original source code as the second argument to populate
source_context. The pointer_column is taken from the error's real
column when a position is known, so the ^ marker lands on the offending
token instead of always pointing at column 1.
iex> {:error, [error]} = Lua.Parser.parse_structured("if x then")
iex> map = Lua.Parser.Error.to_map(error, "if x then")
iex> {map.type, map.source_context.pointer_column}
{:unexpected_token, 10}
Creates an error for unclosed delimiter.
Creates an error for unexpected end of input.
Creates an error for unexpected token.