Skip to content

Parse errors carry line/column + context snippet - #5

Merged
conorbronsdon merged 1 commit into
mainfrom
error-positions
Jul 8, 2026
Merged

Parse errors carry line/column + context snippet#5
conorbronsdon merged 1 commit into
mainfrom
error-positions

Conversation

@conorbronsdon

Copy link
Copy Markdown
Owner

Adopts the suite-wide error-reporting pattern from conorbronsdon/mojo-xml#3: parse errors now carry a 1-based line/column position and a snippet of the offending line.

What changed

  • New html.errors module — a verbatim copy of mojo-xml's errors.mojo. line_col(source, offset) maps a byte offset to a 1-based (line, column) pair (column = byte offset within the line; LF-only terminator, CRLF-safe), and parse_error(msg, source, offset) builds an Error reading <msg> at line <L>, column <C>: '<snippet>' — snippet is the whitespace-trimmed offending line, windowed to ~30 bytes centered on the offset, ... on cut sides, never split mid-UTF-8-sequence, never multi-line. Both exported from the package.
  • All strict-mode tokenizer errors wired through it. mojo-html's liberal mode never raises on malformed markup by design; every raise site lives behind strict=True and already had a byte offset, so all of them now carry position + snippet (previously a bare (line L, column C) suffix with no snippet):
    • mismatched end tag / stray end tag — at the offending end tag
    • unclosed element at EOF — now points at the unclosed start tag instead of the useless EOF position (new _open_pos bookkeeping, strict mode only)
    • unterminated comment / CDATA — at the construct opener
    • unknown entity, bare &, malformed numeric reference — now point at the offending & (in text, attribute values, and escapable raw text) instead of the tokenizer's position after the run
  • Mechanism unchanged: still a plain raise Error(...); the repo-specific mojo-html [strict]: prefix stays in the caller; existing contains=-style assertions keep matching.
  • Tests: new test/test_errors.mojo (31 tests) — the full line_col/parse_error edge-case suite ported from mojo-xml, plus 11 integration tests asserting hand-verified line/column/snippet output for every wired raise site. Registered in the pixi test task and the CI workflow. Full suite: 99 tests green.

🤖 Generated with Claude Code

New html.errors module (line_col + parse_error), following the shared
mojo-* suite pattern. Strict-mode tokenizer errors now read
"mojo-html [strict]: <msg> at line <L>, column <C>: <snippet>" with
more useful offsets: unclosed-element errors point at the unclosed
start tag instead of EOF, and entity errors point at the offending
& in text, attribute values, and escapable raw text.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@conorbronsdon
conorbronsdon merged commit f2fb05d into main Jul 8, 2026
1 check passed
@conorbronsdon
conorbronsdon deleted the error-positions branch July 8, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant