mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-26 09:21:04 +01:00
[glsl-in] include information on what was expected in InvalidToken
This commit is contained in:
1 parent
e3827f8e08
commit
c2cfcbf84b
3 files changed
+155
-26
No files matched your search
+46
-4
@@ -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 {
|
||||
@@ -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}")]
|
||||
@@ -43,7 +85,7 @@ impl ErrorKind {
|
||||
| ErrorKind::UnknownLayoutQualifier(metadata, _)
|
||||
| ErrorKind::SemanticError(metadata, _)
|
||||
| ErrorKind::UnknownField(metadata, _) => Some(metadata),
|
||||
ErrorKind::InvalidToken(ref token) => Some(token.meta),
|
||||
ErrorKind::InvalidToken(ref token, _) => Some(token.meta),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+101
-18
@@ -11,9 +11,9 @@ use super::{
|
||||
Program,
|
||||
};
|
||||
use crate::{
|
||||
arena::Handle, Arena, ArraySize, BinaryOperator, Block, Constant, ConstantInner, Expression,
|
||||
Function, FunctionResult, ResourceBinding, ScalarValue, Statement, StorageClass, StructMember,
|
||||
SwitchCase, Type, TypeInner, UnaryOperator,
|
||||
arena::Handle, front::glsl::error::ExpectedToken, Arena, ArraySize, BinaryOperator, Block,
|
||||
Constant, ConstantInner, Expression, Function, FunctionResult, ResourceBinding, ScalarValue,
|
||||
Statement, StorageClass, StructMember, SwitchCase, Type, TypeInner, UnaryOperator,
|
||||
};
|
||||
use core::convert::TryFrom;
|
||||
use std::{iter::Peekable, mem};
|
||||
@@ -38,7 +38,10 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
|
||||
match token.value {
|
||||
TokenValue::Identifier(name) => Ok((name, token.meta)),
|
||||
_ => Err(ErrorKind::InvalidToken(token)),
|
||||
_ => Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![ExpectedToken::Identifier],
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +49,7 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
let token = self.bump()?;
|
||||
|
||||
if token.value != value {
|
||||
Err(ErrorKind::InvalidToken(token))
|
||||
Err(ErrorKind::InvalidToken(token, vec![value.into()]))
|
||||
} else {
|
||||
Ok(token)
|
||||
}
|
||||
@@ -90,7 +93,12 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
440 | 450 | 460 => self.program.version = i.value as u16,
|
||||
_ => return Err(ErrorKind::InvalidVersion(version.meta, i.value)),
|
||||
},
|
||||
_ => return Err(ErrorKind::InvalidToken(version)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
version,
|
||||
vec![ExpectedToken::IntLiteral],
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
let profile = self.lexer.peek();
|
||||
@@ -155,7 +163,16 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
Some(ty) => Some(*ty),
|
||||
None => return Err(ErrorKind::UnknownType(token.meta, ident)),
|
||||
},
|
||||
_ => return Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![
|
||||
TokenValue::Void.into(),
|
||||
TokenValue::Struct.into(),
|
||||
ExpectedToken::TypeName,
|
||||
],
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
let size = self.parse_array_specifier()?;
|
||||
@@ -363,7 +380,10 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
Ok(())
|
||||
}
|
||||
// TODO: handle Shared?
|
||||
_ => Err(ErrorKind::InvalidToken(token)),
|
||||
_ => Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![ExpectedToken::Identifier],
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,7 +425,13 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
let token = self.bump()?;
|
||||
match token.value {
|
||||
TokenValue::Semicolon if self.program.version == 460 => Ok(()),
|
||||
_ => Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
let expected = match self.program.version {
|
||||
460 => vec![TokenValue::Semicolon.into(), ExpectedToken::EOF],
|
||||
_ => vec![ExpectedToken::EOF],
|
||||
};
|
||||
Err(ErrorKind::InvalidToken(token, expected))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
@@ -479,7 +505,12 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
meta = meta.union(&token.meta);
|
||||
break;
|
||||
}
|
||||
_ => return Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![TokenValue::Comma.into(), TokenValue::RightBrace.into()],
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -534,7 +565,12 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
let name = match token.value {
|
||||
TokenValue::Semicolon => break,
|
||||
TokenValue::Identifier(name) => name,
|
||||
_ => return Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![ExpectedToken::Identifier, TokenValue::Semicolon.into()],
|
||||
))
|
||||
}
|
||||
};
|
||||
let mut meta = token.meta;
|
||||
|
||||
@@ -581,7 +617,12 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
match token.value {
|
||||
TokenValue::Semicolon => break,
|
||||
TokenValue::Comma => {}
|
||||
_ => return Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![TokenValue::Comma.into(), TokenValue::Semicolon.into()],
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -697,7 +738,17 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
_ => Err(ErrorKind::InvalidToken(token)),
|
||||
_ if external => Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![
|
||||
TokenValue::LeftBrace.into(),
|
||||
TokenValue::Semicolon.into(),
|
||||
],
|
||||
)),
|
||||
_ => Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![TokenValue::Semicolon.into()],
|
||||
)),
|
||||
};
|
||||
}
|
||||
// Pass the token to the init_declator_list parser
|
||||
@@ -772,7 +823,10 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
_ => Err(ErrorKind::InvalidToken(token)),
|
||||
_ => Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![ExpectedToken::Identifier, TokenValue::Semicolon.into()],
|
||||
)),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -820,7 +874,12 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
|
||||
Some(name)
|
||||
}
|
||||
_ => return Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![ExpectedToken::Identifier, TokenValue::Semicolon.into()],
|
||||
))
|
||||
}
|
||||
};
|
||||
meta = meta.union(&token.meta);
|
||||
|
||||
@@ -914,7 +973,17 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
|
||||
return Ok(expr);
|
||||
}
|
||||
_ => return Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![
|
||||
TokenValue::LeftParen.into(),
|
||||
ExpectedToken::IntLiteral,
|
||||
ExpectedToken::FloatLiteral,
|
||||
ExpectedToken::BoolLiteral,
|
||||
],
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
let handle = self.program.module.constants.append(Constant {
|
||||
@@ -949,7 +1018,12 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
*meta = meta.union(&token.meta);
|
||||
break;
|
||||
}
|
||||
_ => return Err(ErrorKind::InvalidToken(token)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
token,
|
||||
vec![TokenValue::Comma.into(), TokenValue::RightParen.into()],
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1420,7 +1494,16 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> {
|
||||
self.bump()?;
|
||||
break;
|
||||
}
|
||||
_ => return Err(ErrorKind::InvalidToken(self.bump()?)),
|
||||
_ => {
|
||||
return Err(ErrorKind::InvalidToken(
|
||||
self.bump()?,
|
||||
vec![
|
||||
TokenValue::Case.into(),
|
||||
TokenValue::Default.into(),
|
||||
TokenValue::RightBrace.into(),
|
||||
],
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ use super::lex::Lexer;
|
||||
use super::parser;
|
||||
use super::{ast::Profile, error::ErrorKind};
|
||||
use super::{ast::Program, SourceMetadata};
|
||||
use crate::front::glsl::error::ExpectedToken;
|
||||
use crate::{
|
||||
front::glsl::{token::TokenValue, Token},
|
||||
ShaderStage,
|
||||
@@ -50,10 +51,13 @@ fn version() {
|
||||
parse_program("#version 450\nvoid f(){} #version 450", &entry_points)
|
||||
.err()
|
||||
.unwrap(),
|
||||
ErrorKind::InvalidToken(Token {
|
||||
value: TokenValue::Unknown(PreprocessorError::UnexpectedHash),
|
||||
meta: SourceMetadata { start: 24, end: 25 }
|
||||
})
|
||||
ErrorKind::InvalidToken(
|
||||
Token {
|
||||
value: TokenValue::Unknown(PreprocessorError::UnexpectedHash),
|
||||
meta: SourceMetadata { start: 24, end: 25 }
|
||||
},
|
||||
vec![ExpectedToken::EOF]
|
||||
)
|
||||
);
|
||||
|
||||
// valid versions
|
||||
|
||||
Reference in new issue
Block a user