onihime/onihime/src/parser/error.rs
2024-12-24 09:02:18 +01:00

64 lines
2.0 KiB
Rust

use crate::{lexer::LexerError, span::Span};
/// Kinds of errors that can occur during parsing.
#[derive(Debug, Clone, PartialEq)]
pub enum ParserErrorKind {
/// An error which ocurred during lexical analysis.
Lexer(LexerError),
/// Key in map is missing its corresponding value.
MissingValueInMap,
/// Opening delimiter does not have a matching closing delimiter.
UnclosedSequence,
/// An unexpected closing delimiter was found.
UnexpectedClosingDelimiter,
/// Unexpectedly reached end of input.
UnexpectedEof,
/// An unmatched closing delimiter was found.
UnmatchedClosingDelimiter,
}
/// Parser error, with a start and end location.
#[derive(Debug, Clone, PartialEq)]
pub struct ParserError {
/// The type of error encountered.
pub kind: ParserErrorKind,
/// The span in which the error occurred.
pub span: Span,
}
impl ParserError {
/// Construct a new instance of `ParserError`.
#[must_use]
pub const fn new(kind: ParserErrorKind, span: Span) -> Self {
Self { kind, span }
}
}
impl From<LexerError> for ParserError {
fn from(err: LexerError) -> Self {
let span = err.span.clone();
Self::new(ParserErrorKind::Lexer(err), span)
}
}
impl std::error::Error for ParserError {}
#[cfg(not(tarpaulin_include))]
impl std::fmt::Display for ParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use ParserErrorKind::*;
match &self.kind {
Lexer(err) => write!(f, "{err}"),
MissingValueInMap => write!(f, "Key in map is missing its corresponding value"),
UnclosedSequence => write!(
f,
"Opening delimiter does not have a matching closing delimiter"
),
UnexpectedClosingDelimiter => write!(f, "An unexpected closing delimiter was found"),
UnexpectedEof => write!(f, "Unexpectedly reached end of input"),
UnmatchedClosingDelimiter => write!(f, "An unmatched closing delimiter was found"),
}
}
}