Skip to content

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

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

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

Conversation

@conorbronsdon

Copy link
Copy Markdown
Owner

Adds position info to every parse-time error, following the shared errors.mojo pattern established in conorbronsdon/mojo-xml#3.

  • New template.errors module, copied verbatim from mojo-xml: line_col(source, offset) (1-based line; column = 1-based byte offset in the line, LF-only terminators, clamped) and parse_error(msg, source, offset) producing <msg> at line <L>, column <C>: '<snippet>' with a ~30-byte, UTF-8-safe, single-line snippet of the offending line. Both exported from the package.
  • Lexer Tokens carry byte offsets (tag opener + inner-content start) instead of line numbers; expression tokens carry absolute byte offsets. Every lexer/parser raise site now reports an exact position: unclosed tags and unclosed {% if %} / {% for %} point at the opening tag (not EOF), expression errors (unexpected character, unterminated string literal, trailing tokens, missing ]/)/names, depth-cap guards) point at the offending byte. Guard conditions untouched — only error construction changed.
  • Render-time errors (undefined variables, eval-depth cap) are unchanged: the AST carries no source offsets, and threading them through was out of scope.
  • 26 new tests in test/test_errors.mojo (ported edge-case suite + 6 integration tests with hand-verified line/column), wired into the pixi test task and CI as a separate step. Full suite: 104 tests green; mojo format clean.

🤖 Generated with Claude Code

New template.errors module (line_col + parse_error), copied from the
suite-wide pattern established in mojo-xml. Lexer tokens and expression
tokens now carry byte offsets into the template source; every lexer and
parser raise site reports "<msg> at line L, column C: <snippet>" —
unclosed constructs point at their opening tag, expression errors at the
offending byte. Render-time errors are unchanged (the AST carries no
source positions). 26 new tests in test/test_errors.mojo, wired into the
pixi test task and CI.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@conorbronsdon
conorbronsdon merged commit d7a7839 into main Jul 8, 2026
1 check passed
@conorbronsdon
conorbronsdon deleted the error-positions branch July 8, 2026 05:01
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