Skip to content

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

@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're 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's positives and negatives lists
  • Maps grammar rule names to user-friendly terms (e.g., package_declaration → "package declaration")
  • 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 (e.g., 'part', 'port', 'package')"
Expected([identifier]) "Expected(identifier)" "P003: Expected an identifier"
Expected([";"]) "Expected(";")` "P003: Missing semicolon at end of statement"
Unexpected(";") "Unexpected(;)" "P002: Unexpected ';' - check for missing expression"

4. Contextual Help

For common errors, provide hints:

  • Missing semicolon → "Did you forget a ';' at the end?"
  • Unmatched brace → "Unmatched '{' - check for missing '}'"
  • Reserved word as identifier → "'part' is a reserved keyword and cannot be used as a name"

5. Implementation Plan

  1. Phase 1: Create parser/error_translator.rs module

    • Rule name → friendly name mapping
    • Basic error message templates
  2. Phase 2: Integrate with ParseError

    • Add code field to ParseError
    • Add hint field for suggestions
    • Update LSP diagnostics to use new format
  3. Phase 3: Context-aware improvements

    • Analyze surrounding tokens for better suggestions
    • Add "did you mean?" for near-matches
    • Track common error patterns for tailored hints

Success Criteria

  • All parser errors display with error codes (P001-P007)
  • Error messages are in plain English, not grammar rule names
  • Common errors have helpful hints
  • LSP diagnostics show improved messages

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