Error Handling Overview
Sipha provides comprehensive error handling with configurable error recovery strategies. This section covers error types, recovery mechanisms, and best practices for handling errors in your parsers.
Overview
Sipha’s error handling system is designed to:
- Provide detailed error information with precise source locations
- Recover from errors gracefully to continue parsing
- Support rich diagnostics with context and suggestions (via
diagnosticsfeature) - Distinguish between errors and warnings for different severity levels
Error Handling Flow
flowchart TD
Start[Parse Input] --> Parse[Parser Processes Tokens]
Parse --> Error{Error<br/>Encountered?}
Error -->|No| Success[Parse Success]
Error -->|Yes| Recover{Recovery<br/>Enabled?}
Recover -->|Yes| Attempt[Attempt Recovery]
Recover -->|No| Fail[Report Error]
Attempt --> Success2[Continue Parsing]
Attempt --> Fail2[Recovery Failed]
Success --> Result[Return ParseResult]
Success2 --> Result
Fail --> Result
Fail2 --> Result
style Success fill:#c8e6c9,color:#000000
style Success2 fill:#c8e6c9,color:#000000
style Fail fill:#ffccbc,color:#000000
style Fail2 fill:#ffccbc,color:#000000
style Result fill:#e1f5ff,color:#000000
Key Concepts
Errors vs Warnings
- Errors: Prevent successful parsing or indicate syntax violations
- Warnings: Non-fatal issues that don’t prevent parsing
Error Recovery
Sipha parsers can automatically recover from errors to continue parsing, which is essential for:
- IDEs: Providing real-time feedback as users type
- Language Servers: Maintaining parse state despite errors
- Interactive Tools: Allowing partial parsing results
Rich Diagnostics
With the diagnostics feature enabled, errors include:
- Source code snippets
- Highlighted error locations
- Suggestions and hints
- Related code locations
What’s Covered
This section covers:
- Error Types: Understanding ParseError, LexerError, and ParseWarning
- Working with Errors: Checking, reporting, and extracting error information
- Error Recovery: Configuring and understanding error recovery strategies
- Rich Diagnostics: Using miette for beautiful error reporting
- Best Practices: Guidelines and common patterns
Quick Start
The most basic error handling:
use sipha::backend::ll::{LlParser, LlConfig};
use sipha::backend::ParserBackend;
let result = parser.parse(&tokens, entry_point);
// Always check for errors
if !result.errors.is_empty() {
for error in &result.errors {
eprintln!("Error: {}", error);
}
return Err("Parsing failed".into());
}
// Check for warnings
for warning in &result.warnings {
eprintln!("Warning: {}", warning.message);
}
Next Steps
- Learn about Error Types to understand what errors you might encounter
- See Working with Errors for practical error handling patterns
- Explore Error Recovery to configure recovery strategies