[wgsl] include the expected string into the error

This commit is contained in:
Dzmitry Malyshau
2021-01-27 10:19:14 -05:00
committed by Dzmitry Malyshau
parent 3e4cb21b90
commit 4f517f4af3
2 changed files with 48 additions and 27 deletions

View File

@@ -178,12 +178,28 @@ impl<'a> Lexer<'a> {
self.clone().next()
}
pub(super) fn expect(&mut self, expected: Token<'_>) -> Result<(), Error<'a>> {
pub(super) fn expect(&mut self, expected: Token<'a>) -> Result<(), Error<'a>> {
let token = self.next();
if token == expected {
Ok(())
} else {
token.unexpected(expected)
let description = match expected {
Token::Separator(_) => "separator",
Token::DoubleColon => "::",
Token::Paren(_) => "paren",
Token::DoubleParen(_) => "double paren",
Token::Number { .. } => "number",
Token::String(string) => string,
Token::Word(word) => word,
Token::Operation(_) => "operation",
Token::LogicalOperation(_) => "logical op",
Token::ShiftOperation(_) => "shift op",
Token::Arrow => "->",
Token::Unknown(_) => "unknown",
Token::UnterminatedString => "string",
Token::End => "",
};
Err(Error::Unexpected(token, description))
}
}
@@ -200,14 +216,14 @@ impl<'a> Lexer<'a> {
pub(super) fn next_ident(&mut self) -> Result<&'a str, Error<'a>> {
match self.next() {
Token::Word(word) => Ok(word),
other => other.unexpected("ident"),
other => Err(Error::Unexpected(other, "ident")),
}
}
fn _next_float_literal(&mut self) -> Result<f32, Error<'a>> {
match self.next() {
Token::Number { value, .. } => value.parse().map_err(|err| Error::BadFloat(value, err)),
other => other.unexpected("float literal"),
other => Err(Error::Unexpected(other, "float literal")),
}
}
@@ -216,7 +232,7 @@ impl<'a> Lexer<'a> {
Token::Number { value, .. } => {
value.parse().map_err(|err| Error::BadInteger(value, err))
}
other => other.unexpected("uint literal"),
other => Err(Error::Unexpected(other, "uint literal")),
}
}
@@ -225,7 +241,7 @@ impl<'a> Lexer<'a> {
Token::Number { value, .. } => {
value.parse().map_err(|err| Error::BadInteger(value, err))
}
other => other.unexpected("sint literal"),
other => Err(Error::Unexpected(other, "sint literal")),
}
}

View File

@@ -39,17 +39,10 @@ pub enum Token<'a> {
End,
}
impl<'a> Token<'a> {
fn unexpected<T, E: std::fmt::Debug>(self, expected: E) -> Result<T, Error<'a>> {
log::error!("Unmet expectation for {:?}", expected);
Err(Error::Unexpected(self))
}
}
#[derive(Clone, Debug, Error)]
pub enum Error<'a> {
#[error("unexpected token: {0:?}")]
Unexpected(Token<'a>),
#[error("unexpected token {0:?}, expected {1}")]
Unexpected(Token<'a>, &'a str),
#[error("unable to parse `{0}` as integer: {1}")]
BadInteger(&'a str, std::num::ParseIntError),
#[error("unable to parse `{1}` as float: {1}")]
@@ -691,7 +684,12 @@ impl Parser {
match lexer.next() {
Token::Paren(')') => break,
Token::Separator(',') => (),
other => return other.unexpected("argument list separator"),
other => {
return Err(Error::Unexpected(
other,
"argument list separator",
))
}
}
}
}
@@ -850,7 +848,7 @@ impl Parser {
}
}
}
other => return other.unexpected("primary expression"),
other => return Err(Error::Unexpected(other, "primary expression")),
};
self.scopes.pop();
Ok(ctx.expressions.append(expression))
@@ -1183,7 +1181,7 @@ impl Parser {
lexer.expect(Token::Paren(')'))?;
ready = false;
}
other => return other.unexpected("decoration separator"),
other => return Err(Error::Unexpected(other, "decoration separator")),
}
}
self.scopes.pop();
@@ -1191,7 +1189,7 @@ impl Parser {
let name = match lexer.next() {
Token::Word(word) => word,
Token::Paren('}') => return Ok(members),
other => return other.unexpected("field name"),
other => return Err(Error::Unexpected(other, "field name")),
};
lexer.expect(Token::Separator(':'))?;
let (ty, _access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
@@ -1347,7 +1345,7 @@ impl Parser {
crate::ArraySize::Constant(const_handle)
}
Token::Paren('>') => crate::ArraySize::Dynamic,
other => return other.unexpected("generic separator"),
other => return Err(Error::Unexpected(other, "generic separator")),
};
crate::TypeInner::Array {
@@ -1526,7 +1524,7 @@ impl Parser {
lexer.expect(Token::Paren(')'))?;
}
Token::DoubleParen(']') => break,
other => return other.unexpected("type decoration"),
other => return Err(Error::Unexpected(other, "type decoration")),
}
}
self.scopes.pop();
@@ -1569,7 +1567,7 @@ impl Parser {
return Ok(Some(block));
}
Token::Word(word) => word,
other => return other.unexpected("statement"),
other => return Err(Error::Unexpected(other, "statement")),
};
self.scopes.push(Scope::Statement);
@@ -1678,7 +1676,9 @@ impl Parser {
});
}
Token::Separator(':') => break value,
other => return other.unexpected("case separator"),
other => {
return Err(Error::Unexpected(other, "case separator"))
}
}
};
@@ -1709,7 +1709,7 @@ impl Parser {
default = self.parse_block(lexer, context.reborrow())?;
}
Token::Paren('}') => break,
other => return other.unexpected("switch item"),
other => return Err(Error::Unexpected(other, "switch item")),
}
}
@@ -1923,7 +1923,12 @@ impl Parser {
match lexer.next() {
Token::Paren(')') => break,
Token::Separator(',') if i != 2 => (),
other => return other.unexpected("workgroup size separator"),
other => {
return Err(Error::Unexpected(
other,
"workgroup size separator",
))
}
}
}
for size in workgroup_size.iter_mut() {
@@ -1939,7 +1944,7 @@ impl Parser {
break;
}
Token::Separator(',') => {}
other => return other.unexpected("decoration separator"),
other => return Err(Error::Unexpected(other, "decoration separator")),
}
}
if let (Some(group), Some(index)) = (bind_group, bind_index) {
@@ -2058,7 +2063,7 @@ impl Parser {
}
}
Token::End => return Ok(false),
other => return other.unexpected("global item"),
other => return Err(Error::Unexpected(other, "global item")),
}
match binding {