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
-
Phase 1: Create parser/error_translator.rs module
- Rule name → friendly name mapping
- Basic error message templates
-
Phase 2: Integrate with ParseError
- Add
code field to ParseError
- Add
hint field for suggestions
- Update LSP diagnostics to use new format
-
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
Problem
Currently, pest parsing errors are displayed directly to users, which can be cryptic and unhelpful. For example:
This tells the user almost nothing about what went wrong or how to fix it.
Current State
ParseErrorstruct incore/parse_result.rscaptures pest errorserror_codes.rsdefines error code constants (P001-P007) but they're not used for parser errorsparse_error.variantdirectly as the message (seesyntax/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 errorsP002(PARSER_UNEXPECTED_TOKEN) - When pest reports unexpected inputP003(PARSER_EXPECTED_TOKEN) - When pest reports expected tokensP006(PARSER_UNTERMINATED) - Unterminated strings/comments2. Human-Readable Error Messages
Create a
PestErrorTranslatorthat:positivesandnegativeslistspackage_declaration→ "package declaration")3. Example Transformations
Expected([keyword])Expected([identifier])Expected([";"])Unexpected(";")4. Contextual Help
For common errors, provide hints:
5. Implementation Plan
Phase 1: Create
parser/error_translator.rsmodulePhase 2: Integrate with ParseError
codefield to ParseErrorhintfield for suggestionsPhase 3: Context-aware improvements
Success Criteria
Related
core/error_codes.rssyntax/sysml/parser.rs,syntax/kerml/parser.rssyster-lsp/src/server/diagnostics.rs