1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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(),
            ),
        )
    }
}