slang_frontend/
parse_error.rs

1use slang_error::{CompilerError, ErrorCode, LineInfo};
2
3/// Error that occurs during parsing
4#[derive(Debug)]
5pub struct ParseError {
6    /// The structured error code for this error
7    error_code: ErrorCode,
8    /// Error message describing the problem
9    message: String,
10    /// Position in the source code where the error occurred
11    position: usize,
12    /// Length of the underlined part
13    underline_length: usize,
14}
15
16impl ParseError {
17    /// Creates a new parse error with the given error code, message and position
18    pub fn new(
19        error_code: ErrorCode,
20        message: &str,
21        position: usize,
22        underline_length: usize,
23    ) -> Self {
24        ParseError {
25            error_code,
26            message: message.to_string(),
27            position,
28            underline_length,
29        }
30    }
31
32    pub fn to_compiler_error(&self, line_info: &LineInfo) -> CompilerError {
33        let line_pos = line_info.get_line_col(self.position);
34        CompilerError::new(
35            self.error_code,
36            self.message.clone(),
37            line_pos.0,
38            line_pos.1,
39            self.position,
40            Some(self.underline_length),
41        )
42    }
43}
44
45impl std::fmt::Display for ParseError {
46    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
47        write!(f, "{}", self.message)
48    }
49}
50
51impl std::error::Error for ParseError {}
52