use crate::ast::Span;
use thiserror::Error;
#[derive(Debug, Clone)]
pub struct ParseError {
pub kind: ParseErrorKind,
pub span: Span,
pub context: Option<String>,
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.kind)?;
if let Some(ref ctx) = self.context {
write!(f, " ({})", ctx)?;
}
Ok(())
}
}
impl std::error::Error for ParseError {}
impl ParseError {
pub fn new(kind: ParseErrorKind, span: Span) -> Self {
Self { kind, span, context: None }
}
pub fn with_context(mut self, context: impl Into<String>) -> Self {
self.context = Some(context.into());
self
}
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ParseErrorKind {
#[error("unexpected character")]
UnexpectedChar,
#[error("unexpected token")]
UnexpectedToken,
#[error("expected colon")]
ExpectedColon,
#[error("expected value")]
ExpectedValue,
#[error("expected key")]
ExpectedKey,
#[error("invalid number")]
InvalidNumber,
#[error("unterminated string")]
UnterminatedString,
#[error("invalid escape sequence")]
InvalidEscape,
#[error("invalid indentation")]
InvalidIndent,
#[error("unexpected end of file")]
UnexpectedEof,
#[error("duplicate key")]
DuplicateKey,
#[error("maximum nesting depth exceeded")]
MaxDepthExceeded,
#[error("document too large")]
DocumentTooLarge,
#[error("too many array items")]
TooManyArrayItems,
#[error("too many object entries")]
TooManyObjectEntries,
}