From 54861b712c2d54c732952d700e313e700bcaf969 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Fri, 18 Oct 2024 17:19:19 -0400 Subject: [PATCH] refactor: add `Parser::directive_ident_list` helper --- naga/src/front/wgsl/error.rs | 8 ++++++++ naga/src/front/wgsl/parse/mod.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/naga/src/front/wgsl/error.rs b/naga/src/front/wgsl/error.rs index 64fdc3de9..44b370963 100644 --- a/naga/src/front/wgsl/error.rs +++ b/naga/src/front/wgsl/error.rs @@ -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 @@ -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!( diff --git a/naga/src/front/wgsl/parse/mod.rs b/naga/src/front/wgsl/parse/mod.rs index e7097345b..5c768fe70 100644 --- a/naga/src/front/wgsl/parse/mod.rs +++ b/naga/src/front/wgsl/parse/mod.rs @@ -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>,