Skip to content

Commit

Permalink
refactor: add Parser::directive_ident_list helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Oct 22, 2024
1 parent 4f1a9b5 commit a60ff21
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions naga/src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ impl std::error::Error for ParseError {
pub enum ExpectedToken<'a> {
Token(Token<'a>),
Identifier,
AfterIdentListComma,
AfterIdentListArg,
/// Expected: constant, parenthesized expression, identifier
PrimaryExpression,
/// Expected: assignment, increment/decrement expression
Expand Down Expand Up @@ -345,6 +347,12 @@ impl<'a> Error<'a> {
ExpectedToken::Type => "type".to_string(),
ExpectedToken::Variable => "variable access".to_string(),
ExpectedToken::Function => "function name".to_string(),
ExpectedToken::AfterIdentListArg => {
"next argument, trailing comma, or end of list (',' or ';')".to_string()
}
ExpectedToken::AfterIdentListComma => {
"next argument or end of list (';')".to_string()
}
};
ParseError {
message: format!(
Expand Down
30 changes: 30 additions & 0 deletions naga/src/front/wgsl/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2258,6 +2258,36 @@ impl Parser {
Ok(fun)
}

#[allow(unused)]
fn directive_ident_list<'a>(
&self,
lexer: &mut Lexer<'a>,
handler: impl FnMut(&'a str, Span) -> Result<(), Error<'a>>,
) -> Result<(), Error<'a>> {
let mut handler = handler;
'next_arg: loop {
let (ident, span) = lexer.next_ident_with_span()?;
handler(ident, span)?;

let expected_token = match lexer.peek().0 {
Token::Separator(',') => {
let _ = lexer.next();
if matches!(lexer.peek().0, Token::Word(..)) {
continue 'next_arg;
}
ExpectedToken::AfterIdentListComma
}
_ => ExpectedToken::AfterIdentListArg,
};

if !matches!(lexer.next().0, Token::Separator(';')) {
return Err(Error::Unexpected(span, expected_token));
}

break Ok(());
}
}

fn global_decl<'a>(
&mut self,
lexer: &mut Lexer<'a>,
Expand Down

0 comments on commit a60ff21

Please sign in to comment.