slang_frontend/
parse_error.rs1use slang_error::{CompilerError, ErrorCode, LineInfo};
2
3#[derive(Debug)]
5pub struct ParseError {
6 error_code: ErrorCode,
8 message: String,
10 position: usize,
12 underline_length: usize,
14}
15
16impl ParseError {
17 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