diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc index 6d5e204ce0..1788e32b23 100644 --- a/src/tint/reader/wgsl/parser_impl.cc +++ b/src/tint/reader/wgsl/parser_impl.cc @@ -594,8 +594,8 @@ Maybe ParserImpl::global_variable_decl(AttributeList& attr } // global_constant_decl : -// | LET (ident | variable_ident_decl) global_const_initializer -// | attribute* override (ident | variable_ident_decl) (equal expression)? +// | LET optionally_typed_ident global_const_initializer +// | attribute* override optionally_typed_ident (equal expression)? // global_const_initializer // : EQUAL const_expr Maybe ParserImpl::global_constant_decl(AttributeList& attrs) { @@ -615,7 +615,7 @@ Maybe ParserImpl::global_constant_decl(AttributeList& attr return Failure::kNoMatch; } - auto decl = expect_ident_or_variable_ident_decl(use); + auto decl = expect_optionally_typed_ident(use); if (decl.errored) { return Failure::kErrored; } @@ -666,7 +666,7 @@ Maybe ParserImpl::global_constant_decl(AttributeList& attr } // variable_decl -// : VAR variable_qualifier? (ident | variable_ident_decl) +// : VAR variable_qualifier? optionally_typed_ident Maybe ParserImpl::variable_decl() { Source source; if (!match(Token::Type::kVar, &source)) { @@ -682,7 +682,7 @@ Maybe ParserImpl::variable_decl() { vq = explicit_vq.value; } - auto decl = expect_ident_or_variable_ident_decl("variable declaration"); + auto decl = expect_optionally_typed_ident("variable declaration"); if (decl.errored) { return Failure::kErrored; } @@ -918,7 +918,7 @@ Expect ParserImpl::expect_texel_format(std::string_view use) { return fmt; } -Expect ParserImpl::expect_ident_or_variable_ident_decl_impl( +Expect ParserImpl::expect_ident_with_optional_type_decl( std::string_view use, bool allow_inferred) { auto ident = expect_ident(use); @@ -946,16 +946,17 @@ Expect ParserImpl::expect_ident_or_variable_ident_d return TypedIdentifier{type.value, ident.value, ident.source}; } -// (ident | variable_ident_decl) -Expect ParserImpl::expect_ident_or_variable_ident_decl( +// optionally_typed_ident +// : ident ( COLON typed_decl ) ? +Expect ParserImpl::expect_optionally_typed_ident( std::string_view use) { - return expect_ident_or_variable_ident_decl_impl(use, true); + return expect_ident_with_optional_type_decl(use, true); } -// variable_ident_decl +// ident_with_type_decl // : IDENT COLON type_decl -Expect ParserImpl::expect_variable_ident_decl(std::string_view use) { - return expect_ident_or_variable_ident_decl_impl(use, false); +Expect ParserImpl::expect_ident_with_type_decl(std::string_view use) { + return expect_ident_with_optional_type_decl(use, false); } // access_mode @@ -1356,14 +1357,14 @@ Expect ParserImpl::expect_struct_body_decl() { } // struct_member -// : attribute* variable_ident_decl +// : attribute* ident_with_type_decl Expect ParserImpl::expect_struct_member() { auto attrs = attribute_list(); if (attrs.errored) { return Failure::kErrored; } - auto decl = expect_variable_ident_decl("struct member"); + auto decl = expect_ident_with_type_decl("struct member"); if (decl.errored) { return Failure::kErrored; } @@ -1519,11 +1520,11 @@ Expect ParserImpl::expect_param_list() { } // param -// : attribute_list* variable_ident_decl +// : attribute_list* ident_with_type_decl Expect ParserImpl::expect_param() { auto attrs = attribute_list(); - auto decl = expect_variable_ident_decl("parameter"); + auto decl = expect_ident_with_type_decl("parameter"); if (decl.errored) { return Failure::kErrored; } @@ -1861,11 +1862,11 @@ Maybe ParserImpl::return_statement() { // variable_statement // : variable_decl // | variable_decl EQUAL expression -// | LET (ident | variable_ident_decl) EQUAL expression -// | CONST (ident | variable_ident_decl) EQUAL expression +// | LET optionally_typed_ident EQUAL expression +// | CONST optionally_typed_ident EQUAL expression Maybe ParserImpl::variable_statement() { if (match(Token::Type::kConst)) { - auto decl = expect_ident_or_variable_ident_decl("'const' declaration"); + auto decl = expect_optionally_typed_ident("'const' declaration"); if (decl.errored) { return Failure::kErrored; } @@ -1892,7 +1893,7 @@ Maybe ParserImpl::variable_statement() { } if (match(Token::Type::kLet)) { - auto decl = expect_ident_or_variable_ident_decl("'let' declaration"); + auto decl = expect_optionally_typed_ident("'let' declaration"); if (decl.errored) { return Failure::kErrored; } diff --git a/src/tint/reader/wgsl/parser_impl.h b/src/tint/reader/wgsl/parser_impl.h index 64bc0ec4d4..244b7f42b0 100644 --- a/src/tint/reader/wgsl/parser_impl.h +++ b/src/tint/reader/wgsl/parser_impl.h @@ -410,21 +410,21 @@ class ParserImpl { /// Parses a `variable_decl` grammar element /// @returns the parsed variable declaration info Maybe variable_decl(); - /// Helper for parsing ident or variable_ident_decl. Should not be called directly, + /// Helper for parsing ident with an optional type declaration. Should not be called directly, /// use the specific version below. /// @param use a description of what was being parsed if an error was raised. /// @param allow_inferred allow the identifier to be parsed without a type /// @returns the parsed identifier, and possibly type, or empty otherwise - Expect expect_ident_or_variable_ident_decl_impl(std::string_view use, - bool allow_inferred); + Expect expect_ident_with_optional_type_decl(std::string_view use, + bool allow_inferred); /// Parses a `ident` or a `variable_ident_decl` grammar element, erroring on parse failure. /// @param use a description of what was being parsed if an error was raised. /// @returns the identifier or empty otherwise. - Expect expect_ident_or_variable_ident_decl(std::string_view use); + Expect expect_optionally_typed_ident(std::string_view use); /// Parses a `variable_ident_decl` grammar element, erroring on parse failure. /// @param use a description of what was being parsed if an error was raised. /// @returns the identifier and type parsed or empty otherwise - Expect expect_variable_ident_decl(std::string_view use); + Expect expect_ident_with_type_decl(std::string_view use); /// Parses a `variable_qualifier` grammar element /// @returns the variable qualifier information Maybe variable_qualifier(); diff --git a/src/tint/reader/wgsl/parser_impl_variable_ident_decl_test.cc b/src/tint/reader/wgsl/parser_impl_variable_ident_decl_test.cc index 267998320f..00dd59b43b 100644 --- a/src/tint/reader/wgsl/parser_impl_variable_ident_decl_test.cc +++ b/src/tint/reader/wgsl/parser_impl_variable_ident_decl_test.cc @@ -19,7 +19,7 @@ namespace { TEST_F(ParserImplTest, VariableIdentDecl_Parses) { auto p = parser("my_var : f32"); - auto decl = p->expect_variable_ident_decl("test"); + auto decl = p->expect_ident_with_type_decl("test"); ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(decl.errored); ASSERT_EQ(decl->name, "my_var"); @@ -32,7 +32,7 @@ TEST_F(ParserImplTest, VariableIdentDecl_Parses) { TEST_F(ParserImplTest, VariableIdentDecl_Parses_AllowInferredType) { auto p = parser("my_var : f32"); - auto decl = p->expect_ident_or_variable_ident_decl("test"); + auto decl = p->expect_optionally_typed_ident("test"); ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(decl.errored); ASSERT_EQ(decl->name, "my_var"); @@ -45,14 +45,14 @@ TEST_F(ParserImplTest, VariableIdentDecl_Parses_AllowInferredType) { TEST_F(ParserImplTest, VariableIdentDecl_Inferred_Parse_Failure) { auto p = parser("my_var = 1.0"); - auto decl = p->expect_variable_ident_decl("test"); + auto decl = p->expect_ident_with_type_decl("test"); ASSERT_TRUE(p->has_error()); ASSERT_EQ(p->error(), "1:8: expected ':' for test"); } TEST_F(ParserImplTest, VariableIdentDecl_Inferred_Parses_AllowInferredType) { auto p = parser("my_var = 1.0"); - auto decl = p->expect_ident_or_variable_ident_decl("test"); + auto decl = p->expect_optionally_typed_ident("test"); ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(decl.errored); ASSERT_EQ(decl->name, "my_var"); @@ -63,7 +63,7 @@ TEST_F(ParserImplTest, VariableIdentDecl_Inferred_Parses_AllowInferredType) { TEST_F(ParserImplTest, VariableIdentDecl_MissingIdent) { auto p = parser(": f32"); - auto decl = p->expect_variable_ident_decl("test"); + auto decl = p->expect_ident_with_type_decl("test"); ASSERT_TRUE(p->has_error()); ASSERT_TRUE(decl.errored); ASSERT_EQ(p->error(), "1:1: expected identifier for test"); @@ -71,7 +71,7 @@ TEST_F(ParserImplTest, VariableIdentDecl_MissingIdent) { TEST_F(ParserImplTest, VariableIdentDecl_MissingIdent_AllowInferredType) { auto p = parser(": f32"); - auto decl = p->expect_ident_or_variable_ident_decl("test"); + auto decl = p->expect_optionally_typed_ident("test"); ASSERT_TRUE(p->has_error()); ASSERT_TRUE(decl.errored); ASSERT_EQ(p->error(), "1:1: expected identifier for test"); @@ -79,7 +79,7 @@ TEST_F(ParserImplTest, VariableIdentDecl_MissingIdent_AllowInferredType) { TEST_F(ParserImplTest, VariableIdentDecl_MissingType) { auto p = parser("my_var :"); - auto decl = p->expect_variable_ident_decl("test"); + auto decl = p->expect_ident_with_type_decl("test"); ASSERT_TRUE(p->has_error()); ASSERT_TRUE(decl.errored); ASSERT_EQ(p->error(), "1:9: invalid type for test"); @@ -87,7 +87,7 @@ TEST_F(ParserImplTest, VariableIdentDecl_MissingType) { TEST_F(ParserImplTest, VariableIdentDecl_MissingType_AllowInferredType) { auto p = parser("my_var :"); - auto decl = p->expect_ident_or_variable_ident_decl("test"); + auto decl = p->expect_optionally_typed_ident("test"); ASSERT_TRUE(p->has_error()); ASSERT_TRUE(decl.errored); ASSERT_EQ(p->error(), "1:9: invalid type for test"); @@ -95,7 +95,7 @@ TEST_F(ParserImplTest, VariableIdentDecl_MissingType_AllowInferredType) { TEST_F(ParserImplTest, VariableIdentDecl_InvalidIdent) { auto p = parser("123 : f32"); - auto decl = p->expect_variable_ident_decl("test"); + auto decl = p->expect_ident_with_type_decl("test"); ASSERT_TRUE(p->has_error()); ASSERT_TRUE(decl.errored); ASSERT_EQ(p->error(), "1:1: expected identifier for test"); @@ -103,7 +103,7 @@ TEST_F(ParserImplTest, VariableIdentDecl_InvalidIdent) { TEST_F(ParserImplTest, VariableIdentDecl_InvalidIdent_AllowInferredType) { auto p = parser("123 : f32"); - auto decl = p->expect_ident_or_variable_ident_decl("test"); + auto decl = p->expect_optionally_typed_ident("test"); ASSERT_TRUE(p->has_error()); ASSERT_TRUE(decl.errored); ASSERT_EQ(p->error(), "1:1: expected identifier for test");