ola_parser/
diagnostics.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use crate::program;
4use crate::program::Loc;
5
6#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
7pub enum Level {
8    Debug,
9    Info,
10    Warning,
11    Error,
12}
13
14impl Level {
15    pub fn to_string(&self) -> &'static str {
16        match self {
17            Level::Debug => "debug",
18            Level::Info => "info",
19            Level::Warning => "warning",
20            Level::Error => "error",
21        }
22    }
23}
24
25#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
26pub enum ErrorType {
27    None,
28    ParserError,
29    SyntaxError,
30    DeclarationError,
31    CastError,
32    TypeError,
33    Warning,
34}
35
36#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
37pub struct Note {
38    pub loc: program::Loc,
39    pub message: String,
40}
41
42#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
43pub struct Diagnostic {
44    pub loc: program::Loc,
45    pub level: Level,
46    pub ty: ErrorType,
47    pub message: String,
48    pub notes: Vec<Note>,
49}
50
51impl Diagnostic {
52    pub fn debug(loc: Loc, message: String) -> Self {
53        Diagnostic {
54            level: Level::Debug,
55            ty: ErrorType::None,
56            loc,
57            message,
58            notes: Vec::new(),
59        }
60    }
61
62    pub fn info(loc: Loc, message: String) -> Self {
63        Diagnostic {
64            level: Level::Info,
65            ty: ErrorType::None,
66            loc,
67            message,
68            notes: Vec::new(),
69        }
70    }
71
72    pub fn parser_error(loc: Loc, message: String) -> Self {
73        Diagnostic {
74            level: Level::Error,
75            ty: ErrorType::ParserError,
76            loc,
77            message,
78            notes: Vec::new(),
79        }
80    }
81
82    pub fn error(loc: Loc, message: String) -> Self {
83        Diagnostic {
84            level: Level::Error,
85            ty: ErrorType::SyntaxError,
86            loc,
87            message,
88            notes: Vec::new(),
89        }
90    }
91
92    pub fn decl_error(loc: Loc, message: String) -> Self {
93        Diagnostic {
94            level: Level::Error,
95            ty: ErrorType::DeclarationError,
96            loc,
97            message,
98            notes: Vec::new(),
99        }
100    }
101
102    pub fn cast_error(loc: Loc, message: String) -> Self {
103        Diagnostic {
104            level: Level::Error,
105            ty: ErrorType::CastError,
106            loc,
107            message,
108            notes: Vec::new(),
109        }
110    }
111
112    pub fn cast_error_with_note(loc: Loc, message: String, note_loc: Loc, note: String) -> Self {
113        Diagnostic {
114            level: Level::Error,
115            ty: ErrorType::CastError,
116            loc,
117            message,
118            notes: vec![Note {
119                loc: note_loc,
120                message: note,
121            }],
122        }
123    }
124
125    pub fn type_error(loc: Loc, message: String) -> Self {
126        Diagnostic {
127            level: Level::Error,
128            ty: ErrorType::TypeError,
129            loc,
130            message,
131            notes: Vec::new(),
132        }
133    }
134
135    pub fn cast_warning(loc: Loc, message: String) -> Self {
136        Diagnostic {
137            level: Level::Warning,
138            ty: ErrorType::CastError,
139            loc,
140            message,
141            notes: Vec::new(),
142        }
143    }
144
145    pub fn warning(loc: Loc, message: String) -> Self {
146        Diagnostic {
147            level: Level::Warning,
148            ty: ErrorType::Warning,
149            loc,
150            message,
151            notes: Vec::new(),
152        }
153    }
154
155    pub fn warning_with_note(loc: Loc, message: String, note_loc: Loc, note: String) -> Self {
156        Diagnostic {
157            level: Level::Warning,
158            ty: ErrorType::Warning,
159            loc,
160            message,
161            notes: vec![Note {
162                loc: note_loc,
163                message: note,
164            }],
165        }
166    }
167
168    pub fn warning_with_notes(loc: Loc, message: String, notes: Vec<Note>) -> Self {
169        Diagnostic {
170            level: Level::Warning,
171            ty: ErrorType::Warning,
172            loc,
173            message,
174            notes,
175        }
176    }
177
178    pub fn error_with_note(loc: Loc, message: String, note_loc: Loc, note: String) -> Self {
179        Diagnostic {
180            level: Level::Error,
181            ty: ErrorType::None,
182            loc,
183            message,
184            notes: vec![Note {
185                loc: note_loc,
186                message: note,
187            }],
188        }
189    }
190
191    pub fn error_with_notes(loc: Loc, message: String, notes: Vec<Note>) -> Self {
192        Diagnostic {
193            level: Level::Error,
194            ty: ErrorType::None,
195            loc,
196            message,
197            notes,
198        }
199    }
200}