Skip to main content

nova_lexer/
error.rs

1//! Lexer error types.
2
3use nova_diagnostics::Span;
4use thiserror::Error;
5
6/// A non-fatal error produced by the lexer.
7///
8/// Lexing continues after any of these; all errors are collected and returned
9/// alongside the token stream.
10#[derive(Debug, Clone, Error)]
11pub enum LexError {
12    #[error("unterminated string literal")]
13    UnterminatedString(Span),
14
15    #[error("unterminated string interpolation: this `${{` has no matching `}}`")]
16    UnterminatedInterpolation(Span),
17
18    #[error("unterminated block comment")]
19    UnterminatedBlockComment(Span),
20
21    #[error("invalid escape sequence `\\{0}`")]
22    InvalidEscapeSequence(char, Span),
23
24    #[error("invalid unicode escape sequence")]
25    InvalidUnicodeEscape(Span),
26
27    #[error("invalid numeric literal")]
28    InvalidNumberLiteral(Span),
29
30    #[error("unexpected character `{0}`")]
31    UnexpectedCharacter(char, Span),
32}
33
34impl LexError {
35    pub fn span(&self) -> Span {
36        match self {
37            LexError::UnterminatedString(s) => *s,
38            LexError::UnterminatedInterpolation(s) => *s,
39            LexError::UnterminatedBlockComment(s) => *s,
40            LexError::InvalidEscapeSequence(_, s) => *s,
41            LexError::InvalidUnicodeEscape(s) => *s,
42            LexError::InvalidNumberLiteral(s) => *s,
43            LexError::UnexpectedCharacter(_, s) => *s,
44        }
45    }
46}