sentience_tokenize/
error.rs1use crate::Span;
2use std::fmt;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum LexErrorKind {
6 UnexpectedChar,
7 UnterminatedString,
8 UnterminatedEscape,
9 InvalidNumber,
10 InvalidEscape,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct LexError {
15 pub kind: LexErrorKind,
16 pub span: Span,
17}
18
19impl LexError {
20 pub fn new(kind: LexErrorKind, span: Span) -> Self {
21 Self { kind, span }
22 }
23}
24
25impl LexErrorKind {
26 pub fn as_str(&self) -> &'static str {
27 match self {
28 LexErrorKind::UnexpectedChar => "unexpected character",
29 LexErrorKind::UnterminatedString => "unterminated string",
30 LexErrorKind::UnterminatedEscape => "unterminated escape",
31 LexErrorKind::InvalidNumber => "invalid number",
32 LexErrorKind::InvalidEscape => "invalid escape sequence",
33 }
34 }
35}
36
37impl fmt::Display for LexError {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 let msg = match self.kind {
40 LexErrorKind::UnexpectedChar => "unexpected char",
41 LexErrorKind::UnterminatedString => "unterminated string",
42 LexErrorKind::UnterminatedEscape => "unterminated escape",
43 LexErrorKind::InvalidNumber => "invalid number",
44 LexErrorKind::InvalidEscape => "invalid escape",
45 };
46 write!(f, "{} at {}..{}", msg, self.span.start, self.span.end)
47 }
48}
49
50impl std::error::Error for LexError {}