glsl_lang_lexer/
full.rs

1//! glsl-lang-pp/full based lexer
2
3use glsl_lang_pp::{processor, types};
4
5use lang_util::{
6    located::Located,
7    position::{LexerPosition, NodeSpan},
8    TextSize,
9};
10
11use super::Token;
12
13mod core;
14
15mod directives;
16pub use directives::*;
17
18pub mod fs;
19pub mod str;
20
21/// Lexical analysis error
22#[derive(Debug)]
23pub enum LexicalError<E: std::error::Error + 'static> {
24    /// Invalid token in lexical analysis
25    Token {
26        /// Type of invalid token error
27        kind: types::token::ErrorKind,
28        /// Location of the error
29        pos: NodeSpan,
30    },
31    /// Preprocessor error
32    Processor(processor::event::Error),
33    /// i/o error
34    Io(Located<E>),
35}
36
37impl<E: std::error::Error + 'static> std::cmp::PartialEq for LexicalError<E> {
38    fn eq(&self, other: &Self) -> bool {
39        match self {
40            LexicalError::Token { kind, pos } => match other {
41                LexicalError::Token {
42                    kind: other_kind,
43                    pos: other_pos,
44                } => kind == other_kind && pos == other_pos,
45                _ => false,
46            },
47            LexicalError::Processor(p) => match other {
48                LexicalError::Processor(other_p) => p == other_p,
49                _ => false,
50            },
51            LexicalError::Io(_) => false,
52        }
53    }
54}
55
56impl<E: std::error::Error + 'static> std::fmt::Display for LexicalError<E> {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            LexicalError::Token { kind, .. } => write!(f, "{}", kind),
60            LexicalError::Processor(error) => write!(f, "{}", error.inner()),
61            LexicalError::Io(io) => write!(f, "{}", io.inner()),
62        }
63    }
64}
65
66impl<E: std::error::Error + 'static> std::error::Error for LexicalError<E> {}
67
68impl<E: std::error::Error + 'static> lang_util::error::LexicalError for LexicalError<E> {
69    fn location(&self) -> (LexerPosition, TextSize) {
70        match self {
71            LexicalError::Token { pos, .. } => (pos.start(), pos.len()),
72            LexicalError::Processor(err) => (
73                LexerPosition::new(err.current_file().unwrap(), err.pos().start()),
74                err.pos().len(),
75            ),
76            LexicalError::Io(io) => (
77                LexerPosition::new(io.current_file().unwrap(), io.pos().start()),
78                io.pos().len(),
79            ),
80        }
81    }
82}
83
84impl<E: std::error::Error + 'static> From<processor::event::Error> for LexicalError<E> {
85    fn from(error: processor::event::Error) -> Self {
86        Self::Processor(error)
87    }
88}