1use thiserror::Error;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct Span {
5 pub offset: usize,
6 pub line: usize,
7 pub column: usize,
8}
9
10impl std::fmt::Display for Span {
11 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12 write!(f, "line {}, column {}", self.line, self.column)
13 }
14}
15
16#[derive(Debug, Error)]
17pub enum CompileError {
18 #[error("{span}: {message}")]
19 Syntax { span: Span, message: String },
20
21 #[error("{span}: {message}")]
22 Semantic { span: Span, message: String },
23
24 #[error("{0}")]
25 Other(String),
26}
27
28impl CompileError {
29 pub fn syntax(span: Span, message: impl Into<String>) -> Self {
30 Self::Syntax {
31 span,
32 message: message.into(),
33 }
34 }
35
36 pub fn semantic(span: Span, message: impl Into<String>) -> Self {
37 Self::Semantic {
38 span,
39 message: message.into(),
40 }
41 }
42}
43
44pub type CompileResult<T> = Result<T, CompileError>;