use std::error;
use std::fmt;
use std::io;
use crate::lexer::scan::{Pos, ScanError};
use crate::parser::ParserError;
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
Io(io::Error),
UnrecognizedToken(Option<Pos>),
UnterminatedLiteral(Option<Pos>),
UnterminatedBracket(Option<Pos>),
UnterminatedBlockComment(Option<Pos>),
BadVariableName(Option<Pos>),
BadNumber(Option<Pos>),
ExpectedEqualsSign(Option<Pos>),
MalformedBlobLiteral(Option<Pos>),
MalformedHexInteger(Option<Pos>),
ParserError(ParserError, Option<Pos>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(ref err) => err.fmt(f),
Self::UnrecognizedToken(pos) => write!(f, "unrecognized token at {pos:?}"),
Self::UnterminatedLiteral(pos) => {
write!(f, "non-terminated literal at {pos:?}")
}
Self::UnterminatedBracket(pos) => {
write!(f, "non-terminated bracket at {pos:?}")
}
Self::UnterminatedBlockComment(pos) => {
write!(f, "non-terminated block comment at {pos:?}")
}
Self::BadVariableName(pos) => write!(f, "bad variable name at {pos:?}"),
Self::BadNumber(pos) => write!(f, "bad number at {pos:?}"),
Self::ExpectedEqualsSign(pos) => write!(f, "expected = sign at {pos:?}"),
Self::MalformedBlobLiteral(pos) => {
write!(f, "malformed blob literal at {pos:?}")
}
Self::MalformedHexInteger(pos) => {
write!(f, "malformed hex integer at {pos:?}")
}
Self::ParserError(ref msg, Some(pos)) => write!(f, "{msg} at {pos}"),
Self::ParserError(ref msg, _) => write!(f, "{msg}"),
}
}
}
impl error::Error for Error {}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}
impl From<ParserError> for Error {
fn from(err: ParserError) -> Self {
Self::ParserError(err, None)
}
}
impl ScanError for Error {
fn position(&mut self, p: Pos) {
match *self {
Self::Io(_) => {}
Self::UnrecognizedToken(ref mut pos) => *pos = Some(p),
Self::UnterminatedLiteral(ref mut pos) => *pos = Some(p),
Self::UnterminatedBracket(ref mut pos) => *pos = Some(p),
Self::UnterminatedBlockComment(ref mut pos) => *pos = Some(p),
Self::BadVariableName(ref mut pos) => *pos = Some(p),
Self::BadNumber(ref mut pos) => *pos = Some(p),
Self::ExpectedEqualsSign(ref mut pos) => *pos = Some(p),
Self::MalformedBlobLiteral(ref mut pos) => *pos = Some(p),
Self::MalformedHexInteger(ref mut pos) => *pos = Some(p),
Self::ParserError(_, ref mut pos) => *pos = Some(p),
}
}
}