Overview
Parserator provides a rich error handling system with detailed location information, context tracking, and support for multiple error formats. The error system is built around three main types:- ParseError: Union type representing different kinds of parsing errors
- ParseErrorBundle: Collection of errors with formatting capabilities
- Span: Location information for errors in source code
Span
Represents a location span in source code with position and size information.Creating Spans
Use theSpan() factory function to create spans from parser state:
ParseError Types
Parserator defines four types of errors, each with a discriminanttag field for pattern matching:
Expected Error
Used when specific tokens or constructs were expected but not found.Unexpected Error
Used when an unexpected token is encountered.Custom Error
Used for custom error messages specific to your parser.Fatal Error
Used for unrecoverable errors that should stop parsing immediately.ParseError Union Type
TheParseError type is a discriminated union of all error types:
Pattern Matching
Use thetag field to handle different error types:
ParseErrorBundle
A collection of parsing errors with formatting and analysis capabilities.Creating Error Bundles
Primary Error
The error bundle automatically identifies the “primary” error - the one that occurred furthest in the input. This is typically the most relevant error to show users.Primary Errors
Get all errors that occurred at the same position as the primary error:String Representation
Formatted Output
See Error Formatter for detailed formatting options:Error Context
All error types include acontext field that tracks the parser state stack. This helps users understand where in the parsing process the error occurred:
Best Practices
- Use Expected errors for missing required elements
- Use Unexpected errors for invalid tokens (especially with hints for typos)
- Use Custom errors for domain-specific validation failures
- Use Fatal errors only for unrecoverable situations
- Always include context to help users locate errors in complex grammars
- Add hints when you can detect common typos or mistakes
Related
- Error Formatter - Format errors for display
- Hints - Generate typo suggestions