[wgsl] pass expected token to functions that might not match any tokens

This improves errors making them more general where appropriate.
This commit is contained in:
teoxoy authored and Teodor Tanasoaia committed 2026-01-28 12:00:35 +01:00
1 parent ac18c06ba6
commit 94cc5187dd
3 files changed
+48 -19

No files matched your search

+11 -1
View File
@@ -135,6 +135,7 @@ pub enum ExpectedToken<'a> {
Identifier,
AfterIdentListComma,
AfterIdentListArg,
/// LHS expression (identifier component_or_swizzle_specifier?, (`lhs_expression`) component_or_swizzle_specifier?, &`lhs_expression`, *`lhs_expression`)
LhsExpression,
/// Expected: constant, parenthesized expression, identifier
PrimaryExpression,
@@ -152,6 +153,12 @@ pub enum ExpectedToken<'a> {
Function,
/// The `diagnostic` identifier of the `@diagnostic(…)` attribute.
DiagnosticAttribute,
/// statement
Statement,
/// for loop init statement (variable_or_value_statement, variable_updating_statement, func_call_statement)
ForInit,
/// for loop update statement (variable_updating_statement, func_call_statement)
ForUpdate,
}
#[derive(Clone, Copy, Debug, Error, PartialEq)]
@@ -504,7 +511,7 @@ impl<'a> Error<'a> {
Token::End => "end".to_string(),
},
ExpectedToken::Identifier => "identifier".to_string(),
ExpectedToken::LhsExpression => "assignment destination".to_string(),
ExpectedToken::LhsExpression => "LHS expression (identifier component_or_swizzle_specifier?, (`lhs_expression`) component_or_swizzle_specifier?, &`lhs_expression`, *`lhs_expression`)".to_string(),
ExpectedToken::PrimaryExpression => "expression".to_string(),
ExpectedToken::Assignment => "assignment or increment/decrement".to_string(),
ExpectedToken::SwitchItem => concat!(
@@ -532,6 +539,9 @@ impl<'a> Error<'a> {
ExpectedToken::DiagnosticAttribute => {
"the `diagnostic` attribute identifier".to_string()
}
ExpectedToken::Statement => "statement".to_string(),
ExpectedToken::ForInit => "for loop initializer statement (`var`/`let`/`const` declaration, assignment, `i++`/`i--` statement, function call)".to_string(),
ExpectedToken::ForUpdate => "for loop update statement (assignment, `i++`/`i--` statement, function call)".to_string(),
};
ParseError {
message: format!(
+31 -12
View File
@@ -626,25 +626,29 @@ impl Parser {
lexer: &mut Lexer<'a>,
ctx: &mut ExpressionContext<'a, '_, '_>,
token: Option<TokenSpan<'a>>,
expected_token: ExpectedToken<'a>,
) -> Result<'a, Handle<ast::Expression<'a>>> {
self.track_recursion(|this| {
this.push_rule_span(Rule::LhsExpr, lexer);
let token = token.unwrap_or_else(|| lexer.next());
let expr = match token {
(Token::Operation('*'), _) => {
let expr = this.lhs_expression(lexer, ctx, None)?;
let expr =
this.lhs_expression(lexer, ctx, None, ExpectedToken::LhsExpression)?;
let expr = ast::Expression::Deref(expr);
let span = this.peek_rule_span(lexer);
ctx.expressions.append(expr, span)
}
(Token::Operation('&'), _) => {
let expr = this.lhs_expression(lexer, ctx, None)?;
let expr =
this.lhs_expression(lexer, ctx, None, ExpectedToken::LhsExpression)?;
let expr = ast::Expression::AddrOf(expr);
let span = this.peek_rule_span(lexer);
ctx.expressions.append(expr, span)
}
(Token::Paren('('), span) => {
let expr = this.lhs_expression(lexer, ctx, None)?;
let expr =
this.lhs_expression(lexer, ctx, None, ExpectedToken::LhsExpression)?;
lexer.expect(Token::Paren(')'))?;
this.component_or_swizzle_specifier(span, lexer, ctx, expr)?
}
@@ -660,10 +664,7 @@ impl Parser {
this.component_or_swizzle_specifier(span, lexer, ctx, ident)?
}
(_, span) => {
return Err(Box::new(Error::Unexpected(
span,
ExpectedToken::LhsExpression,
)));
return Err(Box::new(Error::Unexpected(span, expected_token)));
}
};
@@ -1017,6 +1018,7 @@ impl Parser {
ctx: &mut ExpressionContext<'a, '_, '_>,
block: &mut ast::Block<'a>,
token: TokenSpan<'a>,
expected_token: ExpectedToken<'a>,
) -> Result<'a, ()> {
match token {
(Token::Word("_"), span) => {
@@ -1031,7 +1033,7 @@ impl Parser {
}
_ => {}
}
let target = self.lhs_expression(lexer, ctx, Some(token))?;
let target = self.lhs_expression(lexer, ctx, Some(token), expected_token)?;
let (op, value) = match lexer.next() {
(Token::Operation('='), _) => {
@@ -1131,9 +1133,10 @@ impl Parser {
context: &mut ExpressionContext<'a, '_, '_>,
block: &mut ast::Block<'a>,
token: TokenSpan<'a>,
expected_token: ExpectedToken<'a>,
) -> Result<'a, ()> {
if !self.maybe_func_call_statement(lexer, context, block, token)? {
self.variable_updating_statement(lexer, context, block, token)?;
self.variable_updating_statement(lexer, context, block, token, expected_token)?;
}
Ok(())
}
@@ -1149,6 +1152,7 @@ impl Parser {
ctx: &mut ExpressionContext<'a, '_, '_>,
block: &mut ast::Block<'a>,
token: TokenSpan<'a>,
expected_token: ExpectedToken<'a>,
) -> Result<'a, ()> {
let local_decl = match token {
(Token::Word("let"), _) => {
@@ -1206,7 +1210,13 @@ impl Parser {
})
}
token => {
return self.func_call_or_variable_updating_statement(lexer, ctx, block, token);
return self.func_call_or_variable_updating_statement(
lexer,
ctx,
block,
token,
expected_token,
);
}
};
@@ -1413,7 +1423,11 @@ impl Parser {
if !lexer.next_if(Token::Separator(';')) {
let token = lexer.next();
this.variable_or_value_or_func_call_or_variable_updating_statement(
lexer, ctx, block, token,
lexer,
ctx,
block,
token,
ExpectedToken::ForInit,
)?;
lexer.expect(Token::Separator(';'))?;
};
@@ -1448,6 +1462,7 @@ impl Parser {
ctx,
&mut continuing,
token,
ExpectedToken::ForUpdate,
)?;
lexer.expect(Token::Paren(')'))?;
}
@@ -1501,7 +1516,11 @@ impl Parser {
}
token => {
this.variable_or_value_or_func_call_or_variable_updating_statement(
lexer, ctx, block, token,
lexer,
ctx,
block,
token,
ExpectedToken::Statement,
)?;
lexer.expect(Token::Separator(';'))?;
this.pop_rule_span(lexer);
+6 -6
View File
@@ -417,11 +417,11 @@ fn bad_for_initializer() {
for ({};;) {}
}
"#,
r#"error: expected assignment destination, found "{"
r#"error: expected for loop initializer statement (`var`/`let`/`const` declaration, assignment, `i++`/`i--` statement, function call), found "{"
┌─ wgsl:3:22
│
3 │ for ({};;) {}
│ ^ expected assignment destination
│ ^ expected for loop initializer statement (`var`/`let`/`const` declaration, assignment, `i++`/`i--` statement, function call)
"#,
);
@@ -2615,11 +2615,11 @@ fn binary_statement() {
3 + 5;
}
",
r###"error: expected assignment destination, found "3"
r###"error: expected statement, found "3"
┌─ wgsl:3:13
│
3 │ 3 + 5;
│ ^ expected assignment destination
│ ^ expected statement
"###,
);
@@ -2633,11 +2633,11 @@ fn assign_to_expr() {
3 + 5 = 10;
}
",
r###"error: expected assignment destination, found "3"
r###"error: expected statement, found "3"
┌─ wgsl:3:13
│
3 │ 3 + 5 = 10;
│ ^ expected assignment destination
│ ^ expected statement
"###,
);