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
79
80
81
82
83
84
85
86
87
88
//! glsl-lang-pp/full based lexer

use glsl_lang_pp::{processor, types};

use lang_util::{
    located::Located,
    position::{LexerPosition, NodeSpan},
    TextSize,
};

use super::Token;

mod core;

mod directives;
pub use directives::*;

pub mod fs;
pub mod str;

/// Lexical analysis error
#[derive(Debug)]
pub enum LexicalError<E: std::error::Error + 'static> {
    /// Invalid token in lexical analysis
    Token {
        /// Type of invalid token error
        kind: types::token::ErrorKind,
        /// Location of the error
        pos: NodeSpan,
    },
    /// Preprocessor error
    Processor(processor::event::Error),
    /// i/o error
    Io(Located<E>),
}

impl<E: std::error::Error + 'static> std::cmp::PartialEq for LexicalError<E> {
    fn eq(&self, other: &Self) -> bool {
        match self {
            LexicalError::Token { kind, pos } => match other {
                LexicalError::Token {
                    kind: other_kind,
                    pos: other_pos,
                } => kind == other_kind && pos == other_pos,
                _ => false,
            },
            LexicalError::Processor(p) => match other {
                LexicalError::Processor(other_p) => p == other_p,
                _ => false,
            },
            LexicalError::Io(_) => false,
        }
    }
}

impl<E: std::error::Error + 'static> std::fmt::Display for LexicalError<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LexicalError::Token { kind, .. } => write!(f, "{}", kind),
            LexicalError::Processor(error) => write!(f, "{}", error.inner()),
            LexicalError::Io(io) => write!(f, "{}", io.inner()),
        }
    }
}

impl<E: std::error::Error + 'static> std::error::Error for LexicalError<E> {}

impl<E: std::error::Error + 'static> lang_util::error::LexicalError for LexicalError<E> {
    fn location(&self) -> (LexerPosition, TextSize) {
        match self {
            LexicalError::Token { pos, .. } => (pos.start(), pos.len()),
            LexicalError::Processor(err) => (
                LexerPosition::new(err.current_file().unwrap(), err.pos().start()),
                err.pos().len(),
            ),
            LexicalError::Io(io) => (
                LexerPosition::new(io.current_file().unwrap(), io.pos().start()),
                io.pos().len(),
            ),
        }
    }
}

impl<E: std::error::Error + 'static> From<processor::event::Error> for LexicalError<E> {
    fn from(error: processor::event::Error) -> Self {
        Self::Processor(error)
    }
}