Enhance Parser Errors with Source Location Context #7

Closed
opened 2026-03-11 18:04:33 +03:00 by NiXTheDev · 0 comments
NiXTheDev commented 2026-03-11 18:04:33 +03:00 (Migrated from github.com)

Goal: Provide rich error messages with line/column information using spans.

After adding spans to tokens (Issue #5), the parser should produce SpannedError instances instead of plain ParseError.

Current State:
parser::parse returns Result<Expr, ParseError>. ParseError lacks location info.

Proposed Change:

  • Change parse to return Result<Expr, SpannedError>.
  • Inside the parser, when an error occurs (e.g., unexpected token, duplicate group name), create a SpannedError using the span of the offending token or the current position.
  • Update all call sites (e.g., Regex::new, transpile) to handle SpannedError.

Example Error Display:

error: duplicate group name 'name'
--> (regex):1:6
|
1 | (name:abc)(name:def)
| ---- ---- note: first definition here

Implementation Steps:

  1. Modify error.rs to possibly include multiple spans for context (e.g., for duplicate names, include the first definition's span).
  2. In parser.rs, track the current token's span and use it when constructing errors.
  3. Update RegexError to include SpannedError variant or integrate spans into existing variants.
**Goal**: Provide rich error messages with line/column information using spans. After adding spans to tokens (Issue #5), the parser should produce `SpannedError` instances instead of plain `ParseError`. **Current State**: `parser::parse` returns `Result<Expr, ParseError>`. `ParseError` lacks location info. **Proposed Change**: - Change `parse` to return `Result<Expr, SpannedError>`. - Inside the parser, when an error occurs (e.g., unexpected token, duplicate group name), create a `SpannedError` using the span of the offending token or the current position. - Update all call sites (e.g., `Regex::new`, `transpile`) to handle `SpannedError`. **Example Error Display**: ``` error: duplicate group name 'name' --> (regex):1:6 | 1 | (name:abc)(name:def) | ---- ---- note: first definition here ``` **Implementation Steps**: 1. Modify `error.rs` to possibly include multiple spans for context (e.g., for duplicate names, include the first definition's span). 2. In `parser.rs`, track the current token's span and use it when constructing errors. 3. Update `RegexError` to include `SpannedError` variant or integrate spans into existing variants.
Sign in to join this conversation.
No description provided.