devalang_core/core/error/
mod.rs1use crate::core::parser::{ statement::{ Statement, StatementKind }, driver::Parser };
2use serde::{Serialize, Deserialize};
3
4pub struct ErrorHandler {
5 errors: Vec<Error>,
6}
7
8#[derive(Serialize, Deserialize, Clone)]
9pub struct ErrorResult {
10 pub message: String,
11 pub line: usize,
12 pub column: usize,
13}
14
15#[derive(Clone)]
16pub struct Error {
17 pub message: String,
18 pub line: usize,
19 pub column: usize,
20}
21
22impl ErrorHandler {
23 pub fn new() -> Self {
24 Self { errors: Vec::new() }
25 }
26
27 pub fn add_error(&mut self, message: String, line: usize, column: usize) {
28 let error_statement = Error {
29 message,
30 line,
31 column,
32 };
33 self.errors.push(error_statement);
34 }
35
36 pub fn has_errors(&self) -> bool {
37 !self.errors.is_empty()
38 }
39
40 pub fn get_errors(&self) -> &Vec<Error> {
41 &self.errors
42 }
43
44 pub fn clear_errors(&mut self) {
45 self.errors.clear();
46 }
47
48 pub fn detect_from_statements(&mut self, _parser: &mut Parser, statements: &[Statement]) {
49 for stmt in statements {
50 match &stmt.kind {
51 StatementKind::Unknown => {
52 self.add_error(
53 "Unknown statement".to_string(),
54 stmt.line,
55 stmt.column
56 );
57 }
58 StatementKind::Error { message } => {
59 self.add_error(
60 message.clone(),
61 stmt.line,
62 stmt.column
63 );
64 }
65 _ => {}
66 }
67 }
68 }
69}