[wgsl-parser] Consume empty struct closing brace.

This CL updates the WGSL parser to consume the closing } of an empty
struct.

Bug: tint:218
Change-Id: I0b17d2178b1b4b7f44fcf007da867db07dc2a6ae
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/27600
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2020-08-27 18:17:10 +00:00 committed by Commit Bot service account
parent 5f915f1f92
commit 46252ec3ba
2 changed files with 24 additions and 9 deletions

View File

@ -1021,12 +1021,6 @@ std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl() {
return nullptr;
}
t = peek();
if (!t.IsBraceLeft()) {
set_error(t, "missing { for struct declaration");
return nullptr;
}
auto body = struct_body_decl();
if (has_error()) {
return nullptr;
@ -1075,14 +1069,17 @@ ast::StructDecoration ParserImpl::struct_decoration(Token t) {
// : BRACKET_LEFT struct_member* BRACKET_RIGHT
ast::StructMemberList ParserImpl::struct_body_decl() {
auto t = peek();
if (!t.IsBraceLeft())
if (!t.IsBraceLeft()) {
set_error(t, "missing { for struct declaration");
return {};
}
next(); // Consume the peek
t = peek();
if (t.IsBraceRight())
if (t.IsBraceRight()) {
next(); // Consume the peek
return {};
}
ast::StructMemberList members;
for (;;) {

View File

@ -109,6 +109,24 @@ TEST_F(ParserImplTest, TypeDecl_Struct_WithStride) {
EXPECT_EQ(arr->array_stride(), 4u);
}
// This was failing due to not finding the missing ;. https://crbug.com/tint/218
TEST_F(ParserImplTest, TypeDecl_Struct_Empty) {
auto* p = parser("type str = struct {};");
p->global_decl();
ASSERT_FALSE(p->has_error()) << p->error();
auto module = p->module();
ASSERT_EQ(module.alias_types().size(), 1u);
auto* t = module.alias_types()[0];
ASSERT_NE(t, nullptr);
EXPECT_EQ(t->name(), "str");
ASSERT_TRUE(t->type()->IsStruct());
auto* s = t->type()->AsStruct();
EXPECT_EQ(s->impl()->members().size(), 0u);
}
} // namespace
} // namespace wgsl
} // namespace reader