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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::error::{Annotation, Error};
use crate::Span;

#[derive(Debug, PartialEq, Clone, Default)]
pub enum LexError {
    #[default]
    UnrecognizedToken,
    IntegerValueTooLarge,
    FloatValueTooLarge,
    StringValueInvalid(Vec<StringValueLexError>),
}

impl From<Vec<StringValueLexError>> for LexError {
    fn from(errors: Vec<StringValueLexError>) -> Self {
        Self::StringValueInvalid(errors)
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum StringValueLexError {
    InvalidUnicodeEscapeSequence(Span),
    InvalidCharacters(Span),
}

impl From<(LexError, Span)> for Error {
    fn from((error, span): (LexError, Span)) -> Self {
        match error {
            LexError::UnrecognizedToken => Self::new(
                "Unrecognized token",
                Some(Annotation::new("Unable to parse", span)),
                Vec::new(),
            ),
            LexError::IntegerValueTooLarge => Self::new(
                "Value too large to fit in a 32-bit signed integer",
                Some(Annotation::new("Integer too large", span)),
                Vec::new(),
            ),
            LexError::FloatValueTooLarge => Self::new(
                "Value too large to fit in a 64-bit float",
                Some(Annotation::new("Float too large", span)),
                Vec::new(),
            ),
            LexError::StringValueInvalid(errors) => Self::new(
                "String value invalid",
                Some(Annotation::new("String value", span)),
                errors
                    .into_iter()
                    .map(|error| {
                        let (message, span) = match error {
                            StringValueLexError::InvalidUnicodeEscapeSequence(span) => {
                                ("Invalid unicode escape sequence", span)
                            }
                            StringValueLexError::InvalidCharacters(span) => {
                                ("Invalid characters", span)
                            }
                        };
                        Annotation::new(message, span)
                    })
                    .collect(),
            ),
        }
    }
}