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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::clarity::{
    diagnostic::{DiagnosableError, Level},
    representations::Span,
};

#[derive(Debug, PartialEq, Clone)]
pub enum LexerError {
    InvalidCharInt(char),
    InvalidCharUint(char),
    InvalidCharBuffer(char),
    InvalidCharIdent(char),
    WarningCharIdent(char),
    InvalidCharTraitIdent(char),
    InvalidCharPrincipal(char),
    InvalidBufferLength(usize),
    UnknownEscapeChar(char),
    IllegalCharString(char),
    IllegalCharUTF8Encoding(char),
    UnterminatedUTF8Encoding,
    ExpectedClosing(char),
    ExpectedSeparator,
    EmptyUTF8Encoding,
    InvalidUTF8Encoding,
    SingleSemiColon,
    UnknownSymbol(char),
    NoteToMatchThis(char),
    UnsupportedLineEnding,
    EditorCRLFMode,
}

#[derive(Debug)]
pub struct PlacedError {
    pub e: LexerError,
    pub span: Span,
}

impl DiagnosableError for LexerError {
    fn message(&self) -> String {
        use self::LexerError::*;
        match self {
            InvalidCharInt(c) => format!("invalid character, '{}', in int literal", c),
            InvalidCharUint(c) => format!("invalid character, '{}', in uint literal", c),
            InvalidCharBuffer(c) => format!("invalid character, '{}', in buffer", c),
            InvalidCharIdent(c) => format!("invalid character, '{}', in identifier", c),
            WarningCharIdent(c) => format!("identifiers containing a '{}' are bad for readability and may be illegal in a future version of Clarity", c),
            InvalidCharTraitIdent(c) => format!("invalid character, '{}', in trait identifier", c),
            InvalidCharPrincipal(c) => format!("invalid character, '{}', in principal literal", c),
            IllegalCharString(c) => format!("invalid character, '{}', in string literal", c),
            IllegalCharUTF8Encoding(c) => format!("invalid character, '{}', in UTF8 encoding", c),
            InvalidUTF8Encoding => "invalid UTF8 encoding".to_string(),
            EmptyUTF8Encoding => "empty UTF8 encoding".to_string(),
            UnterminatedUTF8Encoding => "unterminated UTF8 encoding, missing '}'".to_string(),
            InvalidBufferLength(size) => format!("invalid buffer length, {}", size),
            UnknownEscapeChar(c) => format!("unknown escape character, '{}'", c),
            ExpectedClosing(c) => format!("expected closing '{}'", c),
            ExpectedSeparator => "expected separator".to_string(),
            SingleSemiColon => "unexpected single ';' (comments begin with \";;\"".to_string(),
            UnknownSymbol(c) => format!("unknown symbol, '{}'", c),
            NoteToMatchThis(c) => format!("to match this '{}'", c),
            UnsupportedLineEnding => "unsupported line-ending '\\r', only '\\n' is supported".to_string(),
            EditorCRLFMode => "you may need to change your editor from CRLF mode to LF mode".to_string(),
        }
    }

    fn suggestion(&self) -> Option<String> {
        None
    }

    fn level(&self) -> crate::clarity::diagnostic::Level {
        use self::LexerError::*;
        match self {
            NoteToMatchThis(_) => Level::Note,
            WarningCharIdent(_) => Level::Warning,
            EditorCRLFMode => Level::Note,
            _ => Level::Error,
        }
    }
}