Add optionally_typed_ident to WGSL parser.
This CL adds the `optionally_typed_ident` construct into the WGSL
parser and uses it where the conditional was used previously.
Bug: tint:1633
Change-Id: I15eaf838792208f56b4ddebd950086f14c8962b3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99382
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc
index 6d5e204..1788e32 100644
--- a/src/tint/reader/wgsl/parser_impl.cc
+++ b/src/tint/reader/wgsl/parser_impl.cc
@@ -594,8 +594,8 @@
}
// 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<const ast::Variable*> ParserImpl::global_constant_decl(AttributeList& attrs) {
@@ -615,7 +615,7 @@
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 @@
}
// variable_decl
-// : VAR variable_qualifier? (ident | variable_ident_decl)
+// : VAR variable_qualifier? optionally_typed_ident
Maybe<ParserImpl::VarDeclInfo> ParserImpl::variable_decl() {
Source source;
if (!match(Token::Type::kVar, &source)) {
@@ -682,7 +682,7 @@
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 @@
return fmt;
}
-Expect<ParserImpl::TypedIdentifier> ParserImpl::expect_ident_or_variable_ident_decl_impl(
+Expect<ParserImpl::TypedIdentifier> ParserImpl::expect_ident_with_optional_type_decl(
std::string_view use,
bool allow_inferred) {
auto ident = expect_ident(use);
@@ -946,16 +946,17 @@
return TypedIdentifier{type.value, ident.value, ident.source};
}
-// (ident | variable_ident_decl)
-Expect<ParserImpl::TypedIdentifier> ParserImpl::expect_ident_or_variable_ident_decl(
+// optionally_typed_ident
+// : ident ( COLON typed_decl ) ?
+Expect<ParserImpl::TypedIdentifier> 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::TypedIdentifier> ParserImpl::expect_variable_ident_decl(std::string_view use) {
- return expect_ident_or_variable_ident_decl_impl(use, false);
+Expect<ParserImpl::TypedIdentifier> 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 @@
}
// struct_member
-// : attribute* variable_ident_decl
+// : attribute* ident_with_type_decl
Expect<ast::StructMember*> 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 @@
}
// param
-// : attribute_list* variable_ident_decl
+// : attribute_list* ident_with_type_decl
Expect<ast::Parameter*> 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 @@
// 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<const ast::VariableDeclStatement*> 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 @@
}
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 64bc0ec..244b7f4 100644
--- a/src/tint/reader/wgsl/parser_impl.h
+++ b/src/tint/reader/wgsl/parser_impl.h
@@ -410,21 +410,21 @@
/// Parses a `variable_decl` grammar element
/// @returns the parsed variable declaration info
Maybe<VarDeclInfo> 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<TypedIdentifier> expect_ident_or_variable_ident_decl_impl(std::string_view use,
- bool allow_inferred);
+ Expect<TypedIdentifier> 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<TypedIdentifier> expect_ident_or_variable_ident_decl(std::string_view use);
+ Expect<TypedIdentifier> 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<TypedIdentifier> expect_variable_ident_decl(std::string_view use);
+ Expect<TypedIdentifier> expect_ident_with_type_decl(std::string_view use);
/// Parses a `variable_qualifier` grammar element
/// @returns the variable qualifier information
Maybe<VariableQualifier> 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 2679983..00dd59b 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 @@
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_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_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_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_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_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_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_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_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");