diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 526ca779a1..248d78c97b 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -452,16 +452,11 @@ std::unique_ptr ParserImpl::variable_decoration() { if (!expect("builtin decoration", Token::Type::kParenLeft)) return nullptr; - std::string ident; - if (!expect_ident("builtin", &ident, &source)) + ast::Builtin builtin; + std::tie(builtin, source) = expect_builtin(); + if (builtin == ast::Builtin::kNone) return nullptr; - ast::Builtin builtin = ident_to_builtin(ident); - if (builtin == ast::Builtin::kNone) { - add_error(source, "invalid value for builtin decoration"); - return nullptr; - } - if (!expect("builtin decoration", Token::Type::kParenRight)) return nullptr; @@ -1796,6 +1791,20 @@ std::pair ParserImpl::expect_pipeline_stage() { return {ast::PipelineStage::kNone, t.source()}; } +std::pair ParserImpl::expect_builtin() { + Source source; + std::string ident; + + if (!expect_ident("builtin", &ident, &source)) + return {ast::Builtin::kNone, source}; + + ast::Builtin builtin = ident_to_builtin(ident); + if (builtin == ast::Builtin::kNone) + add_error(source, "invalid value for builtin decoration"); + + return {builtin, source}; +} + // body_stmt // : BRACKET_LEFT statements BRACKET_RIGHT std::unique_ptr ParserImpl::body_stmt() { diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index 221c36d7d3..91c54fa14a 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h @@ -262,6 +262,11 @@ class ParserImpl { /// @returns the pipeline stage or PipelineStage::kNone if none matched, along /// with the source location for the stage. std::pair expect_pipeline_stage(); + /// Parses a builtin identifier, erroring if the next token does not match a + /// valid builtin name. + /// @returns the builtin or Builtin::kNone if none matched, along with the + /// source location for the stage. + std::pair expect_builtin(); /// Parses a `body_stmt` grammar element /// @returns the parsed statements std::unique_ptr body_stmt();