Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Glsl error message improvements #934

Merged
merged 3 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ trait PrettyResult {
}

fn print_err(error: impl Error) {
eprintln!("{}:", error);
eprint!("{}", error);

let mut e = error.source();
if e.is_some() {
eprintln!(": ");
} else {
eprintln!();
}

while let Some(source) = e {
eprintln!("\t{}", source);
e = source.source();
Expand Down Expand Up @@ -92,7 +99,7 @@ fn main() {
}

let input_path = match input_path {
Some(ref string) => string,
Some(ref string) => Path::new(string),
None => {
println!("Call with <input> <output> [<options>]");
return;
Expand Down Expand Up @@ -135,7 +142,11 @@ fn main() {
defines: Default::default(),
},
)
.unwrap_pretty()
.unwrap_or_else(|err| {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_glsl_parser_error(err, filename.unwrap_or("glsl"), &input);
std::process::exit(1);
})
}
"frag" => {
let input = fs::read_to_string(input_path).unwrap();
Expand All @@ -148,7 +159,11 @@ fn main() {
defines: Default::default(),
},
)
.unwrap_pretty()
.unwrap_or_else(|err| {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_glsl_parser_error(err, filename.unwrap_or("glsl"), &input);
std::process::exit(1);
})
}
"comp" => {
let input = fs::read_to_string(input_path).unwrap();
Expand All @@ -161,7 +176,11 @@ fn main() {
defines: Default::default(),
},
)
.unwrap_pretty()
.unwrap_or_else(|err| {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_glsl_parser_error(err, filename.unwrap_or("glsl"), &input);
std::process::exit(1);
})
}
other => panic!("Unknown input extension: {}", other),
};
Expand Down Expand Up @@ -274,3 +293,26 @@ fn main() {
}
}
}

use codespan_reporting::{
diagnostic::{Diagnostic, Label},
files::SimpleFile,
term::{
self,
termcolor::{ColorChoice, StandardStream},
},
};

pub fn emit_glsl_parser_error(err: naga::front::glsl::ParseError, filename: &str, source: &str) {
let diagnostic = match err.kind.metadata() {
Some(metadata) => Diagnostic::error()
.with_message(err.kind.to_string())
.with_labels(vec![Label::primary((), metadata.start..metadata.end)]),
None => Diagnostic::error().with_message(err.kind.to_string()),
};

let files = SimpleFile::new(filename, source);
let config = codespan_reporting::term::Config::default();
let writer = StandardStream::stderr(ColorChoice::Auto);
term::emit(&mut writer.lock(), &config, &files, &diagnostic).expect("cannot write error");
}
56 changes: 50 additions & 6 deletions src/front/glsl/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
use super::{
constants::ConstantSolvingError,
token::{SourceMetadata, Token},
token::{SourceMetadata, Token, TokenValue},
};
use std::borrow::Cow;
use thiserror::Error;

fn join_with_comma(list: &[ExpectedToken]) -> String {
let mut string = "".to_string();
for (i, val) in list.iter().enumerate() {
string.push_str(&val.to_string());
match i {
i if i == list.len() - 1 => {}
i if i == list.len() - 2 => string.push_str(" or "),
_ => string.push_str(", "),
}
}
string
}

#[derive(Debug, PartialEq)]
pub enum ExpectedToken {
Token(TokenValue),
TypeName,
Identifier,
IntLiteral,
FloatLiteral,
BoolLiteral,
EOF,
}
impl From<TokenValue> for ExpectedToken {
fn from(token: TokenValue) -> Self {
ExpectedToken::Token(token)
}
}
impl std::fmt::Display for ExpectedToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
ExpectedToken::Token(ref token) => write!(f, "{:?}", token),
ExpectedToken::TypeName => write!(f, "a type"),
ExpectedToken::Identifier => write!(f, "identifier"),
ExpectedToken::IntLiteral => write!(f, "integer literal"),
ExpectedToken::FloatLiteral => write!(f, "float literal"),
ExpectedToken::BoolLiteral => write!(f, "bool literal"),
ExpectedToken::EOF => write!(f, "end of file"),
}
}
}

#[derive(Debug, Error)]
#[cfg_attr(test, derive(PartialEq))]
pub enum ErrorKind {
Expand All @@ -14,8 +56,8 @@ pub enum ErrorKind {
InvalidProfile(SourceMetadata, String),
#[error("Invalid version: {1}")]
InvalidVersion(SourceMetadata, u64),
#[error("Unexpected token: {0}")]
InvalidToken(Token),
#[error("Expected {}, found {0}", join_with_comma(.1))]
InvalidToken(Token, Vec<ExpectedToken>),
#[error("Not implemented {0}")]
NotImplemented(&'static str),
#[error("Unknown variable: {1}")]
Expand All @@ -27,8 +69,8 @@ pub enum ErrorKind {
#[error("Unknown layout qualifier: {1}")]
UnknownLayoutQualifier(SourceMetadata, String),
#[cfg(feature = "glsl-validate")]
#[error("Variable already declared: {0}")]
VariableAlreadyDeclared(String),
#[error("Variable already declared: {1}")]
VariableAlreadyDeclared(SourceMetadata, String),
#[error("{1}")]
SemanticError(SourceMetadata, Cow<'static, str>),
}
Expand All @@ -43,7 +85,9 @@ impl ErrorKind {
| ErrorKind::UnknownLayoutQualifier(metadata, _)
| ErrorKind::SemanticError(metadata, _)
| ErrorKind::UnknownField(metadata, _) => Some(metadata),
ErrorKind::InvalidToken(ref token) => Some(token.meta),
#[cfg(feature = "glsl-validate")]
ErrorKind::VariableAlreadyDeclared(metadata, _) => Some(metadata),
ErrorKind::InvalidToken(ref token, _) => Some(token.meta),
_ => None,
}
}
Expand Down
Loading