Skip to main content

xur_parser/
error.rs

1use thiserror::Error;
2
3use xur_errors::Diag;
4use xur_span::Span;
5
6/// What went wrong while lexing.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
8pub enum LexErrorKind {
9    #[error("unexpected character `{0}`")]
10    UnexpectedChar(char),
11    #[error("unterminated string literal")]
12    UnterminatedString,
13    #[error("unterminated block comment")]
14    UnterminatedBlockComment,
15}
16
17/// A lexing error and the source range it covers.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
19#[error("{kind}")]
20pub struct LexError {
21    pub kind: LexErrorKind,
22    pub span: Span,
23}
24
25impl From<LexError> for Diag {
26    fn from(error: LexError) -> Self {
27        let label = match error.kind {
28            LexErrorKind::UnexpectedChar(_) => "not valid in Xur source",
29            LexErrorKind::UnterminatedString => "missing closing `\"`",
30            LexErrorKind::UnterminatedBlockComment => "comment is never closed",
31        };
32        Diag::error(error.kind.to_string(), error.span).with_label(error.span, label)
33    }
34}