mago_lexer/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use serde::Deserialize;
use serde::Serialize;

use mago_reporting::Annotation;
use mago_reporting::Issue;
use mago_span::HasPosition;
use mago_span::HasSpan;
use mago_span::Position;
use mago_span::Span;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub enum SyntaxError {
    UnexpectedToken(u8, Position),
    UnrecognizedToken(u8, Position),
}

impl HasSpan for SyntaxError {
    fn span(&self) -> Span {
        let position = match self {
            Self::UnexpectedToken(_, p) => *p,
            Self::UnrecognizedToken(_, p) => *p,
        };

        Span::new(position, Position { offset: position.offset + 1, ..position })
    }
}

impl std::fmt::Display for SyntaxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            Self::UnexpectedToken(token, _) => &format!("unexpected token `{}` (0x{:02X})", *token as char, token),
            Self::UnrecognizedToken(token, _) => &format!("unrecognised token `{}` (0x{:02X})", *token as char, token),
        };

        write!(f, "syntax error: {}", message)
    }
}

impl std::error::Error for SyntaxError {}

impl From<SyntaxError> for Issue {
    fn from(error: SyntaxError) -> Issue {
        let position = error.position();
        let span = Span::new(position, Position { offset: position.offset + 1, ..position });

        Issue::error(error.to_string()).with_annotation(Annotation::primary(span))
    }
}