Skip to main content

runmat_parser/
error.rs

1#[derive(Debug)]
2pub struct SyntaxError {
3    pub message: String,
4    pub position: usize,
5    pub found_token: Option<String>,
6    pub expected: Option<String>,
7}
8
9impl std::fmt::Display for SyntaxError {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        write!(
12            f,
13            "Syntax error at position {}: {}",
14            self.position, self.message
15        )?;
16        if let Some(found) = &self.found_token {
17            write!(f, " (found: '{found}')")?;
18        }
19        if let Some(expected) = &self.expected {
20            write!(f, " (expected: {expected})")?;
21        }
22        Ok(())
23    }
24}
25
26impl std::error::Error for SyntaxError {}
27
28impl From<String> for SyntaxError {
29    fn from(value: String) -> Self {
30        SyntaxError {
31            message: value,
32            position: 0,
33            found_token: None,
34            expected: None,
35        }
36    }
37}
38
39impl From<SyntaxError> for String {
40    fn from(error: SyntaxError) -> Self {
41        error.to_string()
42    }
43}