use thiserror::Error;
use crate::parser::utils::parse_span::{LineSpan, ParseSpan};
#[derive(Error, Debug, Clone)]
pub enum ParserError {
#[error("syntax error:`{0}`")]
Syntax(String),
#[error("sourcing was disabled, but encountered keyword anyway:`{0}`")]
EncounteredDisabledSource(String),
#[error("tried to read file:`{0}` but the operation failed")]
FileRead(String),
#[error("internal parser error:`{0}`")]
Internal(String),
}
impl ParserError {
pub fn syntax(msg: &str) -> Self {
Self::Syntax(msg.to_string())
}
pub fn syntax_in_span_at(msg: &str, span: &ParseSpan, offset: usize) -> Self {
Self::Syntax(
format!(
"{} in {}, line:{}",
msg,
span.get_filename(),
span.get_global_span().0 + offset,
),
)
}
pub fn syntax_in_line_span(msg: &str, span: &LineSpan) -> Self {
Self::Syntax(
format!(
"{} in {}, line:{}",
msg,
span.get_filename(),
span.get_global_at(),
),
)
}
}