use graphforge_core::Span;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ParseErrorKind {
UnexpectedChar,
UnexpectedToken {
found: String,
expected: Vec<String>,
},
UnterminatedString,
UnterminatedBlockComment,
InvalidNumericLiteral,
InvalidParameter,
UnexpectedEof {
expected: Vec<String>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ParseError {
pub kind: ParseErrorKind,
pub span: Span,
pub message: String,
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let kind_str = match &self.kind {
ParseErrorKind::UnexpectedChar => "unexpected character".to_owned(),
ParseErrorKind::UnexpectedToken { found, .. } => {
format!("unexpected token '{found}'")
}
ParseErrorKind::UnterminatedString => "unterminated string".to_owned(),
ParseErrorKind::UnterminatedBlockComment => "unterminated block comment".to_owned(),
ParseErrorKind::InvalidNumericLiteral => "invalid numeric literal".to_owned(),
ParseErrorKind::InvalidParameter => "invalid parameter".to_owned(),
ParseErrorKind::UnexpectedEof { .. } => "unexpected end of input".to_owned(),
};
write!(f, "{kind_str} at {}", self.span)
}
}
impl std::error::Error for ParseError {}
impl ParseError {
#[must_use]
pub fn new(kind: ParseErrorKind, span: Span, message: impl Into<String>) -> Self {
Self {
kind,
span,
message: message.into(),
}
}
}