wsgl parser: Migrate more code to use expect & match Reduces code. Keeps things more consistent. Bug: tint:282 Change-Id: Iff280880eb033fbcee4c6095c2da2d4af06835b5 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32103 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index a312d1d..52aba79 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc
@@ -375,25 +375,21 @@ auto dim = sampled_texture_type(); if (dim != ast::type::TextureDimension::kNone) { - auto t = next(); - if (!t.IsLessThan()) { - add_error(peek(), "missing '<' for sampled texture type"); + const char* use = "sampled texture type"; + + if (!expect(use, Token::Type::kLessThan)) return nullptr; - } auto* subtype = type_decl(); if (has_error()) return nullptr; if (subtype == nullptr) { - add_error(peek(), "invalid subtype for sampled texture type"); + add_error(peek().source(), "invalid subtype", use); return nullptr; } - t = next(); - if (!t.IsGreaterThan()) { - add_error(peek(), "missing '>' for sampled texture type"); + if (!expect(use, Token::Type::kGreaterThan)) return nullptr; - } return ctx_.type_mgr().Get( std::make_unique<ast::type::SampledTextureType>(dim, subtype)); @@ -401,25 +397,21 @@ dim = multisampled_texture_type(); if (dim != ast::type::TextureDimension::kNone) { - auto t = next(); - if (!t.IsLessThan()) { - add_error(peek(), "missing '<' for multisampled texture type"); + const char* use = "multisampled texture type"; + + if (!expect(use, Token::Type::kLessThan)) return nullptr; - } auto* subtype = type_decl(); if (has_error()) return nullptr; if (subtype == nullptr) { - add_error(peek(), "invalid subtype for multisampled texture type"); + add_error(peek().source(), "invalid subtype", use); return nullptr; } - t = next(); - if (!t.IsGreaterThan()) { - add_error(peek(), "missing '>' for multisampled texture type"); + if (!expect(use, Token::Type::kGreaterThan)) return nullptr; - } return ctx_.type_mgr().Get( std::make_unique<ast::type::MultisampledTextureType>(dim, subtype)); @@ -429,25 +421,21 @@ ast::AccessControl access; std::tie(storage_dim, access) = storage_texture_type(); if (storage_dim != ast::type::TextureDimension::kNone) { - auto t = next(); - if (!t.IsLessThan()) { - add_error(peek(), "missing '<' for storage texture type"); + const char* use = "storage texture type"; + + if (!expect(use, Token::Type::kLessThan)) return nullptr; - } auto format = image_storage_type(); if (has_error()) return nullptr; if (format == ast::type::ImageFormat::kNone) { - add_error(peek(), "invalid format for storage texture type"); + add_error(peek().source(), "invalid format", use); return nullptr; } - t = next(); - if (!t.IsGreaterThan()) { - add_error(peek(), "missing '>' for storage texture type"); + if (!expect(use, Token::Type::kGreaterThan)) return nullptr; - } return ctx_.type_mgr().Get(std::make_unique<ast::type::StorageTextureType>( storage_dim, access, format)); @@ -779,11 +767,8 @@ if (has_error()) return sc; - auto t = next(); - if (!t.IsGreaterThan()) { - add_error(t, "missing > for variable decoration"); + if (!expect(use, Token::Type::kGreaterThan)) return ast::StorageClass::kNone; - } return sc; } @@ -847,8 +832,7 @@ // | texture_sampler_types ast::type::Type* ParserImpl::type_decl() { auto t = peek(); - if (t.IsIdentifier()) { - next(); // Consume the peek + if (match(Token::Type::kIdentifier)) { auto* ty = get_constructed(t.to_str()); if (ty == nullptr) { add_error(t, "unknown constructed type '" + t.to_str() + "'"); @@ -856,28 +840,24 @@ } return ty; } - if (t.IsBool()) { - next(); // Consume the peek + + if (match(Token::Type::kBool)) return ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>()); - } - if (t.IsF32()) { - next(); // Consume the peek + + if (match(Token::Type::kF32)) return ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()); - } - if (t.IsI32()) { - next(); // Consume the peek + + if (match(Token::Type::kI32)) return ctx_.type_mgr().Get(std::make_unique<ast::type::I32Type>()); - } - if (t.IsU32()) { - next(); // Consume the peek + + if (match(Token::Type::kU32)) return ctx_.type_mgr().Get(std::make_unique<ast::type::U32Type>()); - } - if (t.IsVec2() || t.IsVec3() || t.IsVec4()) { + + if (t.IsVec2() || t.IsVec3() || t.IsVec4()) return expect_type_decl_vector(t); - } - if (t.IsPtr()) { - return expect_type_decl_pointer(t); - } + + if (match(Token::Type::kPtr)) + return expect_type_decl_pointer(); auto decos = decoration_list(); if (has_error()) @@ -907,26 +887,18 @@ return nullptr; } -ast::type::Type* ParserImpl::expect_type_decl_pointer(Token t) { - next(); // Consume the peek - +ast::type::Type* ParserImpl::expect_type_decl_pointer() { const char* use = "ptr declaration"; - t = next(); - if (!t.IsLessThan()) { - add_error(t, "missing < for ptr declaration"); + if (!expect(use, Token::Type::kLessThan)) return nullptr; - } auto sc = expect_storage_class(use); if (has_error()) return nullptr; - t = next(); - if (!t.IsComma()) { - add_error(t, "missing , for ptr declaration"); + if (!expect(use, Token::Type::kComma)) return nullptr; - } auto* subtype = type_decl(); if (has_error()) @@ -936,11 +908,8 @@ return nullptr; } - t = next(); - if (!t.IsGreaterThan()) { - add_error(t, "missing > for ptr declaration"); + if (!expect(use, Token::Type::kGreaterThan)) return nullptr; - } return ctx_.type_mgr().Get( std::make_unique<ast::type::PointerType>(subtype, sc)); @@ -955,25 +924,21 @@ else if (t.IsVec4()) count = 4; - t = next(); - if (!t.IsLessThan()) { - add_error(t, "missing < for vector"); + const char* use = "vector"; + + if (!expect(use, Token::Type::kLessThan)) return nullptr; - } auto* subtype = type_decl(); if (has_error()) return nullptr; if (subtype == nullptr) { - add_error(peek(), "unable to determine subtype for vector"); + add_error(peek().source(), "unable to determine subtype", use); return nullptr; } - t = next(); - if (!t.IsGreaterThan()) { - add_error(t, "missing > for vector"); + if (!expect(use, Token::Type::kGreaterThan)) return nullptr; - } return ctx_.type_mgr().Get( std::make_unique<ast::type::VectorType>(subtype, count)); @@ -2128,13 +2093,7 @@ ast::ExpressionList ret; ret.push_back(std::move(arg)); - for (;;) { - auto t = peek(); - if (!t.IsComma()) - break; - - next(); // Consume the peek - + while (match(Token::Type::kComma)) { arg = logical_or_expression(); if (has_error()) return {}; @@ -2618,45 +2577,24 @@ // | FALSE std::unique_ptr<ast::Literal> ParserImpl::const_literal() { auto t = peek(); - if (t.IsTrue()) { - next(); // Consume the peek - + if (match(Token::Type::kTrue)) { auto* type = ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>()); - if (!type) { - return nullptr; - } return std::make_unique<ast::BoolLiteral>(type, true); } - if (t.IsFalse()) { - next(); // Consume the peek + if (match(Token::Type::kFalse)) { auto* type = ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>()); - if (!type) { - return nullptr; - } return std::make_unique<ast::BoolLiteral>(type, false); } - if (t.IsSintLiteral()) { - next(); // Consume the peek + if (match(Token::Type::kSintLiteral)) { auto* type = ctx_.type_mgr().Get(std::make_unique<ast::type::I32Type>()); - if (!type) { - return nullptr; - } return std::make_unique<ast::SintLiteral>(type, t.to_i32()); } - if (t.IsUintLiteral()) { - next(); // Consume the peek + if (match(Token::Type::kUintLiteral)) { auto* type = ctx_.type_mgr().Get(std::make_unique<ast::type::U32Type>()); - if (!type) { - return nullptr; - } return std::make_unique<ast::UintLiteral>(type, t.to_u32()); } - if (t.IsFloatLiteral()) { - next(); // Consume the peek + if (match(Token::Type::kFloatLiteral)) { auto* type = ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()); - if (!type) { - return nullptr; - } return std::make_unique<ast::FloatLiteral>(type, t.to_f32()); } return nullptr;
diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index ba008bb..5e1b941 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h
@@ -544,7 +544,7 @@ /// Used to ensure that all decorations are consumed. bool expect_decorations_consumed(const ast::DecorationList& list); - ast::type::Type* expect_type_decl_pointer(Token t); + ast::type::Type* expect_type_decl_pointer(); ast::type::Type* expect_type_decl_vector(Token t); ast::type::Type* expect_type_decl_array(ast::ArrayDecorationList decos); ast::type::Type* expect_type_decl_matrix(Token t);
diff --git a/src/reader/wgsl/parser_impl_error_msg_test.cc b/src/reader/wgsl/parser_impl_error_msg_test.cc index 163219c..222134e 100644 --- a/src/reader/wgsl/parser_impl_error_msg_test.cc +++ b/src/reader/wgsl/parser_impl_error_msg_test.cc
@@ -534,16 +534,16 @@ TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureMissingLessThan_Old) { EXPECT("var x : texture_sampled_1d;", - "test.wgsl:1:28 error: missing '<' for sampled texture type\n" + "test.wgsl:1:27 error: expected '<' for sampled texture type\n" "var x : texture_sampled_1d;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureMissingGreaterThan_Old) { EXPECT("var x : texture_sampled_1d<f32;", - "test.wgsl:1:32 error: missing '>' for sampled texture type\n" + "test.wgsl:1:31 error: expected '>' for sampled texture type\n" "var x : texture_sampled_1d<f32;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureInvalidSubtype_Old) { @@ -555,16 +555,16 @@ TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureMissingLessThan) { EXPECT("var x : texture_1d;", - "test.wgsl:1:20 error: missing '<' for sampled texture type\n" + "test.wgsl:1:19 error: expected '<' for sampled texture type\n" "var x : texture_1d;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureMissingGreaterThan) { EXPECT("var x : texture_1d<f32;", - "test.wgsl:1:24 error: missing '>' for sampled texture type\n" + "test.wgsl:1:23 error: expected '>' for sampled texture type\n" "var x : texture_1d<f32;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureInvalidSubtype) { @@ -576,16 +576,16 @@ TEST_F(ParserImplErrorTest, GlobalDeclMultisampledTextureMissingLessThan) { EXPECT("var x : texture_multisampled_2d;", - "test.wgsl:1:33 error: missing '<' for multisampled texture type\n" + "test.wgsl:1:32 error: expected '<' for multisampled texture type\n" "var x : texture_multisampled_2d;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclMultisampledTextureMissingGreaterThan) { EXPECT("var x : texture_multisampled_2d<f32;", - "test.wgsl:1:37 error: missing '>' for multisampled texture type\n" + "test.wgsl:1:36 error: expected '>' for multisampled texture type\n" "var x : texture_multisampled_2d<f32;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclMultisampledTextureInvalidSubtype) { @@ -597,16 +597,16 @@ TEST_F(ParserImplErrorTest, GlobalDeclStorageTextureMissingLessThan_Old) { EXPECT("var x : texture_ro_2d;", - "test.wgsl:1:23 error: missing '<' for storage texture type\n" + "test.wgsl:1:22 error: expected '<' for storage texture type\n" "var x : texture_ro_2d;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclStorageTextureMissingGreaterThan_Old) { EXPECT("var x : texture_ro_2d<r8uint;", - "test.wgsl:1:30 error: missing '>' for storage texture type\n" + "test.wgsl:1:29 error: expected '>' for storage texture type\n" "var x : texture_ro_2d<r8uint;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclStorageTextureMissingInvalidSubtype_Old) { @@ -618,16 +618,16 @@ TEST_F(ParserImplErrorTest, GlobalDeclStorageTextureMissingLessThan) { EXPECT("var x : texture_storage_ro_2d;", - "test.wgsl:1:31 error: missing '<' for storage texture type\n" + "test.wgsl:1:30 error: expected '<' for storage texture type\n" "var x : texture_storage_ro_2d;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclStorageTextureMissingGreaterThan) { EXPECT("var x : texture_storage_ro_2d<r8uint;", - "test.wgsl:1:38 error: missing '>' for storage texture type\n" + "test.wgsl:1:37 error: expected '>' for storage texture type\n" "var x : texture_storage_ro_2d<r8uint;\n" - " ^\n"); + " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclStorageTextureMissingSubtype) { @@ -1023,21 +1023,21 @@ TEST_F(ParserImplErrorTest, GlobalDeclVarPtrMissingLessThan) { EXPECT("var i : ptr;", - "test.wgsl:1:12 error: missing < for ptr declaration\n" + "test.wgsl:1:12 error: expected '<' for ptr declaration\n" "var i : ptr;\n" " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclVarPtrMissingGreaterThan) { EXPECT("var i : ptr<in, u32;", - "test.wgsl:1:20 error: missing > for ptr declaration\n" + "test.wgsl:1:20 error: expected '>' for ptr declaration\n" "var i : ptr<in, u32;\n" " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclVarPtrMissingComma) { EXPECT("var i : ptr<in u32>;", - "test.wgsl:1:16 error: missing , for ptr declaration\n" + "test.wgsl:1:16 error: expected ',' for ptr declaration\n" "var i : ptr<in u32>;\n" " ^^^\n"); } @@ -1065,21 +1065,21 @@ TEST_F(ParserImplErrorTest, GlobalDeclVarStorageDeclMissingGThan) { EXPECT("var<in i : i32", - "test.wgsl:1:8 error: missing > for variable decoration\n" + "test.wgsl:1:8 error: expected '>' for variable decoration\n" "var<in i : i32\n" " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclVarVectorMissingLessThan) { EXPECT("var i : vec3;", - "test.wgsl:1:13 error: missing < for vector\n" + "test.wgsl:1:13 error: expected '<' for vector\n" "var i : vec3;\n" " ^\n"); } TEST_F(ParserImplErrorTest, GlobalDeclVarVectorMissingGreaterThan) { EXPECT("var i : vec3<u32;", - "test.wgsl:1:17 error: missing > for vector\n" + "test.wgsl:1:17 error: expected '>' for vector\n" "var i : vec3<u32;\n" " ^\n"); }
diff --git a/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc index ea04f55..0e82af0 100644 --- a/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc +++ b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc
@@ -113,7 +113,7 @@ auto* t = p->texture_sampler_types(); ASSERT_TRUE(p->has_error()); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:19: missing '<' for sampled texture type"); + EXPECT_EQ(p->error(), "1:19: expected '<' for sampled texture type"); } TEST_F(ParserImplTest, @@ -122,7 +122,7 @@ auto* t = p->texture_sampler_types(); ASSERT_TRUE(p->has_error()); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:23: missing '>' for sampled texture type"); + EXPECT_EQ(p->error(), "1:23: expected '>' for sampled texture type"); } TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_F32) { @@ -179,7 +179,7 @@ auto* t = p->texture_sampler_types(); ASSERT_TRUE(p->has_error()); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:11: missing '<' for sampled texture type"); + EXPECT_EQ(p->error(), "1:11: expected '<' for sampled texture type"); } TEST_F(ParserImplTest, TextureSamplerTypes_SampledTexture_MissingGreaterThan) { @@ -187,7 +187,7 @@ auto* t = p->texture_sampler_types(); ASSERT_TRUE(p->has_error()); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:15: missing '>' for sampled texture type"); + EXPECT_EQ(p->error(), "1:15: expected '>' for sampled texture type"); } TEST_F(ParserImplTest, TextureSamplerTypes_MultisampledTexture_I32) { @@ -222,7 +222,7 @@ auto* p = parser("texture_multisampled_2d"); auto* t = p->texture_sampler_types(); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:24: missing '<' for multisampled texture type"); + EXPECT_EQ(p->error(), "1:24: expected '<' for multisampled texture type"); } TEST_F(ParserImplTest, @@ -230,7 +230,7 @@ auto* p = parser("texture_multisampled_2d<u32"); auto* t = p->texture_sampler_types(); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:28: missing '>' for multisampled texture type"); + EXPECT_EQ(p->error(), "1:28: expected '>' for multisampled texture type"); } TEST_F(ParserImplTest, @@ -281,7 +281,7 @@ auto* p = parser("texture_ro_1d"); auto* t = p->texture_sampler_types(); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:14: missing '<' for storage texture type"); + EXPECT_EQ(p->error(), "1:14: expected '<' for storage texture type"); } TEST_F(ParserImplTest, @@ -289,7 +289,7 @@ auto* p = parser("texture_wo_1d<r8unorm"); auto* t = p->texture_sampler_types(); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:22: missing '>' for storage texture type"); + EXPECT_EQ(p->error(), "1:22: expected '>' for storage texture type"); } TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_Readonly1dR8Unorm) { @@ -338,14 +338,14 @@ auto* p = parser("texture_storage_ro_1d"); auto* t = p->texture_sampler_types(); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:22: missing '<' for storage texture type"); + EXPECT_EQ(p->error(), "1:22: expected '<' for storage texture type"); } TEST_F(ParserImplTest, TextureSamplerTypes_StorageTexture_MissingGreaterThan) { auto* p = parser("texture_storage_ro_1d<r8unorm"); auto* t = p->texture_sampler_types(); EXPECT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:30: missing '>' for storage texture type"); + EXPECT_EQ(p->error(), "1:30: expected '>' for storage texture type"); } } // namespace
diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index e3c56c7..20e6b2f 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc
@@ -149,7 +149,7 @@ auto* t = p->type_decl(); ASSERT_EQ(t, nullptr); ASSERT_TRUE(p->has_error()); - ASSERT_EQ(p->error(), "1:9: missing > for vector"); + ASSERT_EQ(p->error(), "1:9: expected '>' for vector"); } INSTANTIATE_TEST_SUITE_P(ParserImplTest, VecMissingGreaterThanTest, @@ -165,7 +165,7 @@ auto* t = p->type_decl(); ASSERT_EQ(t, nullptr); ASSERT_TRUE(p->has_error()); - ASSERT_EQ(p->error(), "1:5: missing < for vector"); + ASSERT_EQ(p->error(), "1:5: expected '<' for vector"); } INSTANTIATE_TEST_SUITE_P(ParserImplTest, VecMissingLessThanTest, @@ -238,7 +238,7 @@ auto* t = p->type_decl(); ASSERT_EQ(t, nullptr); ASSERT_TRUE(p->has_error()); - ASSERT_EQ(p->error(), "1:5: missing < for ptr declaration"); + ASSERT_EQ(p->error(), "1:5: expected '<' for ptr declaration"); } TEST_F(ParserImplTest, TypeDecl_Ptr_MissingGreaterThan) { @@ -246,7 +246,7 @@ auto* t = p->type_decl(); ASSERT_EQ(t, nullptr); ASSERT_TRUE(p->has_error()); - ASSERT_EQ(p->error(), "1:18: missing > for ptr declaration"); + ASSERT_EQ(p->error(), "1:18: expected '>' for ptr declaration"); } TEST_F(ParserImplTest, TypeDecl_Ptr_MissingComma) { @@ -254,7 +254,7 @@ auto* t = p->type_decl(); ASSERT_EQ(t, nullptr); ASSERT_TRUE(p->has_error()); - ASSERT_EQ(p->error(), "1:14: missing , for ptr declaration"); + ASSERT_EQ(p->error(), "1:14: expected ',' for ptr declaration"); } TEST_F(ParserImplTest, TypeDecl_Ptr_MissingStorageClass) {
diff --git a/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc b/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc index fa2b40f..70e7a63 100644 --- a/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc
@@ -92,7 +92,7 @@ auto sc = p->variable_storage_decoration(); ASSERT_EQ(sc, ast::StorageClass::kNone); ASSERT_TRUE(p->has_error()); - ASSERT_EQ(p->error(), "1:4: missing > for variable decoration"); + ASSERT_EQ(p->error(), "1:4: expected '>' for variable decoration"); } } // namespace