[wgsl-reader] Update to create BlockStatements

This CL updates the WGSL Reader to create BlockStatements instead of
StatementLists.

Bug: tint:135
Change-Id: Ifda394023553a625dad67be7d4e0dc815292282a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25724
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
dan sinclair 2020-07-27 15:25:00 +00:00 committed by Sarah Mashayekhi
parent 37c0970d51
commit f751501c35
6 changed files with 35 additions and 35 deletions

View File

@ -1351,7 +1351,7 @@ ast::PipelineStage ParserImpl::pipeline_stage() {
// body_stmt // body_stmt
// : BRACKET_LEFT statements BRACKET_RIGHT // : BRACKET_LEFT statements BRACKET_RIGHT
ast::StatementList ParserImpl::body_stmt() { std::unique_ptr<ast::BlockStatement> ParserImpl::body_stmt() {
auto t = peek(); auto t = peek();
if (!t.IsBraceLeft()) if (!t.IsBraceLeft())
return {}; return {};
@ -1400,8 +1400,8 @@ std::unique_ptr<ast::Expression> ParserImpl::paren_rhs_stmt() {
// statements // statements
// : statement* // : statement*
ast::StatementList ParserImpl::statements() { std::unique_ptr<ast::BlockStatement> ParserImpl::statements() {
ast::StatementList ret; auto ret = std::make_unique<ast::BlockStatement>();
for (;;) { for (;;) {
auto stmt = statement(); auto stmt = statement();
@ -1410,7 +1410,7 @@ ast::StatementList ParserImpl::statements() {
if (stmt == nullptr) if (stmt == nullptr)
break; break;
ret.push_back(std::move(stmt)); ret->append(std::move(stmt));
} }
return ret; return ret;
@ -1857,8 +1857,8 @@ ast::CaseSelectorList ParserImpl::case_selectors() {
// : // :
// | statement case_body // | statement case_body
// | FALLTHROUGH SEMICOLON // | FALLTHROUGH SEMICOLON
ast::StatementList ParserImpl::case_body() { std::unique_ptr<ast::BlockStatement> ParserImpl::case_body() {
ast::StatementList ret; auto ret = std::make_unique<ast::BlockStatement>();
for (;;) { for (;;) {
auto t = peek(); auto t = peek();
if (t.IsFallthrough()) { if (t.IsFallthrough()) {
@ -1871,7 +1871,7 @@ ast::StatementList ParserImpl::case_body() {
return {}; return {};
} }
ret.push_back(std::make_unique<ast::FallthroughStatement>(source)); ret->append(std::make_unique<ast::FallthroughStatement>(source));
break; break;
} }
@ -1881,7 +1881,7 @@ ast::StatementList ParserImpl::case_body() {
if (stmt == nullptr) if (stmt == nullptr)
break; break;
ret.push_back(std::move(stmt)); ret->append(std::move(stmt));
} }
return ret; return ret;
@ -1980,10 +1980,11 @@ std::unique_ptr<ast::ContinueStatement> ParserImpl::continue_stmt() {
// continuing_stmt // continuing_stmt
// : CONTINUING body_stmt // : CONTINUING body_stmt
ast::StatementList ParserImpl::continuing_stmt() { std::unique_ptr<ast::BlockStatement> ParserImpl::continuing_stmt() {
auto t = peek(); auto t = peek();
if (!t.IsContinuing()) if (!t.IsContinuing()) {
return {}; return std::make_unique<ast::BlockStatement>();
}
next(); // Consume the peek next(); // Consume the peek
return body_stmt(); return body_stmt();

View File

@ -177,13 +177,13 @@ class ParserImpl {
ast::PipelineStage pipeline_stage(); ast::PipelineStage pipeline_stage();
/// Parses a `body_stmt` grammar element /// Parses a `body_stmt` grammar element
/// @returns the parsed statements /// @returns the parsed statements
ast::StatementList body_stmt(); std::unique_ptr<ast::BlockStatement> body_stmt();
/// Parses a `paren_rhs_stmt` grammar element /// Parses a `paren_rhs_stmt` grammar element
/// @returns the parsed element or nullptr /// @returns the parsed element or nullptr
std::unique_ptr<ast::Expression> paren_rhs_stmt(); std::unique_ptr<ast::Expression> paren_rhs_stmt();
/// Parses a `statements` grammar element /// Parses a `statements` grammar element
/// @returns the statements parsed /// @returns the statements parsed
ast::StatementList statements(); std::unique_ptr<ast::BlockStatement> statements();
/// Parses a `statement` grammar element /// Parses a `statement` grammar element
/// @returns the parsed statement or nullptr /// @returns the parsed statement or nullptr
std::unique_ptr<ast::Statement> statement(); std::unique_ptr<ast::Statement> statement();
@ -219,7 +219,7 @@ class ParserImpl {
ast::CaseSelectorList case_selectors(); ast::CaseSelectorList case_selectors();
/// Parses a `case_body` grammar element /// Parses a `case_body` grammar element
/// @returns the parsed statements /// @returns the parsed statements
ast::StatementList case_body(); std::unique_ptr<ast::BlockStatement> case_body();
/// Parses a `func_call_stmt` grammar element /// Parses a `func_call_stmt` grammar element
/// @returns the parsed function call or nullptr /// @returns the parsed function call or nullptr
std::unique_ptr<ast::CallStatement> func_call_stmt(); std::unique_ptr<ast::CallStatement> func_call_stmt();
@ -228,7 +228,7 @@ class ParserImpl {
std::unique_ptr<ast::LoopStatement> loop_stmt(); std::unique_ptr<ast::LoopStatement> loop_stmt();
/// Parses a `continuing_stmt` grammar element /// Parses a `continuing_stmt` grammar element
/// @returns the parsed statements /// @returns the parsed statements
ast::StatementList continuing_stmt(); std::unique_ptr<ast::BlockStatement> continuing_stmt();
/// Parses a `const_literal` grammar element /// Parses a `const_literal` grammar element
/// @returns the const literal parsed or nullptr if none found /// @returns the const literal parsed or nullptr if none found
std::unique_ptr<ast::Literal> const_literal(); std::unique_ptr<ast::Literal> const_literal();

View File

@ -28,16 +28,16 @@ TEST_F(ParserImplTest, BodyStmt) {
})"); })");
auto e = p->body_stmt(); auto e = p->body_stmt();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_EQ(e.size(), 2u); ASSERT_EQ(e->size(), 2u);
EXPECT_TRUE(e[0]->IsDiscard()); EXPECT_TRUE(e->get(0)->IsDiscard());
EXPECT_TRUE(e[1]->IsReturn()); EXPECT_TRUE(e->get(1)->IsReturn());
} }
TEST_F(ParserImplTest, BodyStmt_Empty) { TEST_F(ParserImplTest, BodyStmt_Empty) {
auto* p = parser("{}"); auto* p = parser("{}");
auto e = p->body_stmt(); auto e = p->body_stmt();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_EQ(e.size(), 0u); EXPECT_EQ(e->size(), 0u);
} }
TEST_F(ParserImplTest, BodyStmt_InvalidStmt) { TEST_F(ParserImplTest, BodyStmt_InvalidStmt) {

View File

@ -25,7 +25,7 @@ TEST_F(ParserImplTest, CaseBody_Empty) {
auto* p = parser(""); auto* p = parser("");
auto e = p->case_body(); auto e = p->case_body();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
EXPECT_EQ(e.size(), 0u); EXPECT_EQ(e->size(), 0u);
} }
TEST_F(ParserImplTest, CaseBody_Statements) { TEST_F(ParserImplTest, CaseBody_Statements) {
@ -35,32 +35,31 @@ TEST_F(ParserImplTest, CaseBody_Statements) {
auto e = p->case_body(); auto e = p->case_body();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_EQ(e.size(), 2u); ASSERT_EQ(e->size(), 2u);
EXPECT_TRUE(e[0]->IsVariableDecl()); EXPECT_TRUE(e->get(0)->IsVariableDecl());
EXPECT_TRUE(e[1]->IsAssign()); EXPECT_TRUE(e->get(1)->IsAssign());
} }
TEST_F(ParserImplTest, CaseBody_InvalidStatement) { TEST_F(ParserImplTest, CaseBody_InvalidStatement) {
auto* p = parser("a ="); auto* p = parser("a =");
auto e = p->case_body(); auto e = p->case_body();
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
EXPECT_EQ(e.size(), 0u); EXPECT_EQ(e, nullptr);
EXPECT_EQ(p->error(), "1:4: unable to parse right side of assignment");
} }
TEST_F(ParserImplTest, CaseBody_Fallthrough) { TEST_F(ParserImplTest, CaseBody_Fallthrough) {
auto* p = parser("fallthrough;"); auto* p = parser("fallthrough;");
auto e = p->case_body(); auto e = p->case_body();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_EQ(e.size(), 1u); ASSERT_EQ(e->size(), 1u);
EXPECT_TRUE(e[0]->IsFallthrough()); EXPECT_TRUE(e->get(0)->IsFallthrough());
} }
TEST_F(ParserImplTest, CaseBody_Fallthrough_MissingSemicolon) { TEST_F(ParserImplTest, CaseBody_Fallthrough_MissingSemicolon) {
auto* p = parser("fallthrough"); auto* p = parser("fallthrough");
auto e = p->case_body(); auto e = p->case_body();
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
EXPECT_EQ(e.size(), 0u); EXPECT_EQ(e, nullptr);
EXPECT_EQ(p->error(), "1:12: missing ;"); EXPECT_EQ(p->error(), "1:12: missing ;");
} }

View File

@ -25,15 +25,15 @@ TEST_F(ParserImplTest, ContinuingStmt) {
auto* p = parser("continuing { discard; }"); auto* p = parser("continuing { discard; }");
auto e = p->continuing_stmt(); auto e = p->continuing_stmt();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_EQ(e.size(), 1u); ASSERT_EQ(e->size(), 1u);
ASSERT_TRUE(e[0]->IsDiscard()); ASSERT_TRUE(e->get(0)->IsDiscard());
} }
TEST_F(ParserImplTest, ContinuingStmt_InvalidBody) { TEST_F(ParserImplTest, ContinuingStmt_InvalidBody) {
auto* p = parser("continuing { discard }"); auto* p = parser("continuing { discard }");
auto e = p->continuing_stmt(); auto e = p->continuing_stmt();
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
ASSERT_EQ(e.size(), 0u); ASSERT_EQ(e, nullptr);
EXPECT_EQ(p->error(), "1:22: missing ;"); EXPECT_EQ(p->error(), "1:22: missing ;");
} }

View File

@ -26,16 +26,16 @@ TEST_F(ParserImplTest, Statements) {
auto* p = parser("discard; return;"); auto* p = parser("discard; return;");
auto e = p->statements(); auto e = p->statements();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_EQ(e.size(), 2u); ASSERT_EQ(e->size(), 2u);
EXPECT_TRUE(e[0]->IsDiscard()); EXPECT_TRUE(e->get(0)->IsDiscard());
EXPECT_TRUE(e[1]->IsReturn()); EXPECT_TRUE(e->get(1)->IsReturn());
} }
TEST_F(ParserImplTest, Statements_Empty) { TEST_F(ParserImplTest, Statements_Empty) {
auto* p = parser(""); auto* p = parser("");
auto e = p->statements(); auto e = p->statements();
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_EQ(e.size(), 0u); ASSERT_EQ(e->size(), 0u);
} }
} // namespace } // namespace