librashader_naga/front/glsl/
error.rs1use super::token::TokenValue;
2use crate::{proc::ConstantEvaluatorError, Span};
3use pp_rs::token::PreprocessorError;
4use std::borrow::Cow;
5use thiserror::Error;
6
7fn join_with_comma(list: &[ExpectedToken]) -> String {
8 let mut string = "".to_string();
9 for (i, val) in list.iter().enumerate() {
10 string.push_str(&val.to_string());
11 match i {
12 i if i == list.len() - 1 => {}
13 i if i == list.len() - 2 => string.push_str(" or "),
14 _ => string.push_str(", "),
15 }
16 }
17 string
18}
19
20#[derive(Debug, PartialEq)]
22pub enum ExpectedToken {
23 Token(TokenValue),
25 TypeName,
27 Identifier,
29 IntLiteral,
31 FloatLiteral,
33 BoolLiteral,
35 Eof,
37}
38impl From<TokenValue> for ExpectedToken {
39 fn from(token: TokenValue) -> Self {
40 ExpectedToken::Token(token)
41 }
42}
43impl std::fmt::Display for ExpectedToken {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 match *self {
46 ExpectedToken::Token(ref token) => write!(f, "{token:?}"),
47 ExpectedToken::TypeName => write!(f, "a type"),
48 ExpectedToken::Identifier => write!(f, "identifier"),
49 ExpectedToken::IntLiteral => write!(f, "integer literal"),
50 ExpectedToken::FloatLiteral => write!(f, "float literal"),
51 ExpectedToken::BoolLiteral => write!(f, "bool literal"),
52 ExpectedToken::Eof => write!(f, "end of file"),
53 }
54 }
55}
56
57#[derive(Debug, Error)]
59#[cfg_attr(test, derive(PartialEq))]
60pub enum ErrorKind {
61 #[error("Unexpected end of file")]
63 EndOfFile,
64 #[error("Invalid profile: {0}")]
66 InvalidProfile(String),
67 #[error("Invalid version: {0}")]
69 InvalidVersion(u64),
70 #[error("Expected {}, found {0:?}", join_with_comma(.1))]
74 InvalidToken(TokenValue, Vec<ExpectedToken>),
75 #[error("Not implemented: {0}")]
80 NotImplemented(&'static str),
81 #[error("Unknown variable: {0}")]
83 UnknownVariable(String),
84 #[error("Unknown type: {0}")]
86 UnknownType(String),
87 #[error("Unknown field: {0}")]
89 UnknownField(String),
90 #[error("Unknown layout qualifier: {0}")]
96 UnknownLayoutQualifier(String),
97 #[error("unsupported matrix of the form matCx2 in std140 block layout")]
102 UnsupportedMatrixTypeInStd140,
103 #[error("Variable already declared: {0}")]
105 VariableAlreadyDeclared(String),
106 #[error("{0}")]
108 SemanticError(Cow<'static, str>),
109 #[error("{0:?}")]
111 PreprocessorError(PreprocessorError),
112 #[error("Internal error: {0}")]
116 InternalError(&'static str),
117}
118
119impl From<ConstantEvaluatorError> for ErrorKind {
120 fn from(err: ConstantEvaluatorError) -> Self {
121 ErrorKind::SemanticError(err.to_string().into())
122 }
123}
124
125#[derive(Debug, Error)]
127#[error("{kind}")]
128#[cfg_attr(test, derive(PartialEq))]
129pub struct Error {
130 pub kind: ErrorKind,
132 pub meta: Span,
134}