Skip to content

feat: Improve parser error messages for better user experience #648

@jade-codes

Description

@jade-codes

Problem

Currently, pest parsing errors are displayed directly to users, which can be cryptic and unhelpful. For example:

ParseError { variant: Expected(keyword), positives: [keyword], negatives: [] }

This tells the user almost nothing about what went wrong or how to fix it.

Current State

  • ParseError struct in core/parse_result.rs captures pest errors
  • error_codes.rs defines error code constants (P001-P007) but they are not used for parser errors
  • The LSP displays parse_error.variant directly as the message (see syntax/sysml/parser.rs:63)

Proposed Solution

1. Error Code Integration

Map pest error patterns to our defined error codes:

  • P001 (PARSER_SYNTAX_ERROR) - General syntax errors
  • P002 (PARSER_UNEXPECTED_TOKEN) - When pest reports unexpected input
  • P003 (PARSER_EXPECTED_TOKEN) - When pest reports expected tokens
  • P006 (PARSER_UNTERMINATED) - Unterminated strings/comments

2. Human-Readable Error Messages

Create a PestErrorTranslator that:

  • Analyzes pest positives and negatives lists
  • Maps grammar rule names to user-friendly terms
  • Provides context-aware suggestions based on error location

3. Example Transformations

Pest Error Current Display Improved Display
Expected([keyword]) Expected(keyword) P003: Expected a keyword
Expected([identifier]) Expected(identifier) P003: Expected an identifier
Expected([;]) Expected(;) P003: Missing semicolon at end of statement

4. Contextual Help

For common errors, provide hints:

  • Missing semicolon: Did you forget a semicolon at the end?
  • Unmatched brace: Check for missing closing brace
  • Reserved word as identifier: keyword cannot be used as a name

5. Implementation Plan

  1. Phase 1: Create parser/error_translator.rs module
  2. Phase 2: Integrate with ParseError (add code and hint fields)
  3. Phase 3: Context-aware improvements

Related

  • Error codes defined in core/error_codes.rs
  • Current parsing: syntax/sysml/parser.rs, syntax/kerml/parser.rs
  • LSP diagnostics: syster-lsp/src/server/diagnostics.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions