glsl_lang_pp/parser/
error.rs

1use lang_util::SmolStr;
2
3use crate::lexer;
4
5pub type Error = lang_util::located::Located<ErrorKind>;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum ErrorKind {
9    UnknownPreprocessorDirective {
10        name: SmolStr,
11    },
12    ExtraTokensInPreprocessorDirective {
13        name: SmolStr,
14    },
15    UnexpectedTokensInDefineArgs,
16    Unexpected {
17        actual: lexer::Token,
18        expected: Box<[lexer::Token]>,
19    },
20    EndOfInput {
21        expected: Box<[lexer::Token]>,
22    },
23}
24
25impl std::error::Error for ErrorKind {}
26
27impl std::fmt::Display for ErrorKind {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            ErrorKind::UnknownPreprocessorDirective { name } => {
31                write!(f, "unknown preprocessor directive `#{}`", name)
32            }
33            ErrorKind::ExtraTokensInPreprocessorDirective { name } => {
34                write!(f, "'#{}' : extra tokens in preprocessor directive", name)
35            }
36            ErrorKind::UnexpectedTokensInDefineArgs => {
37                write!(f, "unexpected tokens in #define function arguments")
38            }
39            ErrorKind::EndOfInput { expected } => {
40                if expected.len() == 0 {
41                    write!(f, "unexpected end of input")
42                } else {
43                    // TODO: Proper display
44                    write!(f, "unexpected end of input: {:?}", expected)
45                }
46            }
47            ErrorKind::Unexpected { actual, expected } => {
48                if expected.len() == 0 {
49                    write!(f, "unexpected {:?}", actual)
50                } else {
51                    // TODO: Proper display
52                    write!(f, "unexpected {:?}, expected {:?}", actual, expected)
53                }
54            }
55        }
56    }
57}