diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 0176c83ba0..d3e7536d23 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -457,16 +457,16 @@ std::unique_ptr ParserImpl::variable_decoration() { if (!expect("builtin decoration", Token::Type::kParenLeft)) return nullptr; - t = next(); - if (!t.IsIdentifier() || t.to_str().empty()) { - add_error(t, "expected identifier for builtin"); - return {}; - } + source = peek().source(); - ast::Builtin builtin = ident_to_builtin(t.to_str()); + std::string ident; + if (!expect_ident("builtin", &ident)) + return nullptr; + + ast::Builtin builtin = ident_to_builtin(ident); if (builtin == ast::Builtin::kNone) { - add_error(t, "invalid value for builtin decoration"); - return {}; + add_error(source, "invalid value for builtin decoration"); + return nullptr; } if (!expect("builtin decoration", Token::Type::kParenRight)) @@ -1035,18 +1035,14 @@ ast::type::Type* ParserImpl::type_alias() { next(); // Consume the peek - t = next(); - if (!t.IsIdentifier()) { - add_error(t, "missing identifier for type alias"); - return nullptr; - } - auto name = t.to_str(); + const char* use = "type alias"; - t = next(); - if (!t.IsEqual()) { - add_error(t, "missing = for type alias"); + std::string name; + if (!expect_ident(use, &name)) + return nullptr; + + if (!expect(use, Token::Type::kEqual)) return nullptr; - } auto* type = type_decl(); if (has_error()) @@ -1433,12 +1429,9 @@ std::unique_ptr ParserImpl::struct_decl() { } next(); // Consume the peek - t = next(); - if (!t.IsIdentifier()) { - add_error(t, "missing identifier for struct declaration"); + std::string name; + if (!expect_ident("struct declaration", &name)) return nullptr; - } - auto name = t.to_str(); auto body = struct_body_decl(); if (has_error()) { @@ -1791,24 +1784,23 @@ std::unique_ptr ParserImpl::function_header() { if (!match(Token::Type::kFn)) return nullptr; - auto t = next(); - if (!t.IsIdentifier()) { - add_error(t, "missing identifier for function"); - return nullptr; - } - auto name = t.to_str(); + const char* use = "function declaration"; - if (!expect("function declaration", Token::Type::kParenLeft)) + std::string name; + if (!expect_ident(use, &name)) + return nullptr; + + if (!expect(use, Token::Type::kParenLeft)) return nullptr; auto params = param_list(); if (has_error()) return nullptr; - if (!expect("function declaration", Token::Type::kParenRight)) + if (!expect(use, Token::Type::kParenRight)) return nullptr; - t = next(); + auto t = next(); if (!t.IsArrow()) { add_error(t, "missing -> for function declaration"); return nullptr; @@ -2768,15 +2760,15 @@ std::unique_ptr ParserImpl::postfix_expr( } else if (t.IsPeriod()) { next(); // Consume the peek - t = next(); - if (!t.IsIdentifier()) { - add_error(t, "missing identifier for member accessor"); + source = peek().source(); + + std::string ident; + if (!expect_ident("member accessor", &ident)) return nullptr; - } expr = std::make_unique( source, std::move(prefix), - std::make_unique(t.source(), t.to_str())); + std::make_unique(source, ident)); } else { return prefix; } diff --git a/src/reader/wgsl/parser_impl_error_msg_test.cc b/src/reader/wgsl/parser_impl_error_msg_test.cc index e035f9fb9e..366f8392b7 100644 --- a/src/reader/wgsl/parser_impl_error_msg_test.cc +++ b/src/reader/wgsl/parser_impl_error_msg_test.cc @@ -382,7 +382,7 @@ TEST_F(ParserImplErrorTest, FunctionDeclDecoWorkgroupSizeZZero) { TEST_F(ParserImplErrorTest, FunctionDeclMissingIdentifier) { EXPECT("fn () -> void {}", - "test.wgsl:1:4 error: missing identifier for function\n" + "test.wgsl:1:4 error: expected identifier for function declaration\n" "fn () -> void {}\n" " ^\n"); } @@ -612,7 +612,7 @@ TEST_F(ParserImplErrorTest, GlobalDeclStructDecoMissingEnd) { TEST_F(ParserImplErrorTest, GlobalDeclStructDeclMissingIdentifier) { EXPECT("struct {};", - "test.wgsl:1:8 error: missing identifier for struct declaration\n" + "test.wgsl:1:8 error: expected identifier for struct declaration\n" "struct {};\n" " ^\n"); } @@ -697,7 +697,7 @@ TEST_F(ParserImplErrorTest, GlobalDeclStructMemberOffsetNegativeValue) { TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasMissingIdentifier) { EXPECT("type 1 = f32;", - "test.wgsl:1:6 error: missing identifier for type alias\n" + "test.wgsl:1:6 error: expected identifier for type alias\n" "type 1 = f32;\n" " ^\n"); } @@ -711,7 +711,7 @@ TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasInvalidType) { TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasMissingAssignment) { EXPECT("type meow f32", - "test.wgsl:1:11 error: missing = for type alias\n" + "test.wgsl:1:11 error: expected '=' for type alias\n" "type meow f32\n" " ^^^\n"); } @@ -1094,7 +1094,7 @@ TEST_F(ParserImplErrorTest, LoopMissingRBrace) { TEST_F(ParserImplErrorTest, MemberExprMissingIdentifier) { EXPECT("fn f() -> void { x = a.; }", - "test.wgsl:1:24 error: missing identifier for member accessor\n" + "test.wgsl:1:24 error: expected identifier for member accessor\n" "fn f() -> void { x = a.; }\n" " ^\n"); } diff --git a/src/reader/wgsl/parser_impl_function_header_test.cc b/src/reader/wgsl/parser_impl_function_header_test.cc index 469dafef3c..dfd053ccfb 100644 --- a/src/reader/wgsl/parser_impl_function_header_test.cc +++ b/src/reader/wgsl/parser_impl_function_header_test.cc @@ -41,7 +41,7 @@ TEST_F(ParserImplTest, FunctionHeader_MissingIdent) { auto f = p->function_header(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(f, nullptr); - EXPECT_EQ(p->error(), "1:4: missing identifier for function"); + EXPECT_EQ(p->error(), "1:4: expected identifier for function declaration"); } TEST_F(ParserImplTest, FunctionHeader_InvalidIdent) { @@ -49,7 +49,7 @@ TEST_F(ParserImplTest, FunctionHeader_InvalidIdent) { auto f = p->function_header(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(f, nullptr); - EXPECT_EQ(p->error(), "1:4: missing identifier for function"); + EXPECT_EQ(p->error(), "1:4: expected identifier for function declaration"); } TEST_F(ParserImplTest, FunctionHeader_MissingParenLeft) { diff --git a/src/reader/wgsl/parser_impl_postfix_expression_test.cc b/src/reader/wgsl/parser_impl_postfix_expression_test.cc index ab16e53c22..31ca9c6be6 100644 --- a/src/reader/wgsl/parser_impl_postfix_expression_test.cc +++ b/src/reader/wgsl/parser_impl_postfix_expression_test.cc @@ -167,7 +167,7 @@ TEST_F(ParserImplTest, PostfixExpression_MemberAccesssor_InvalidIdent) { auto e = p->postfix_expression(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(e, nullptr); - EXPECT_EQ(p->error(), "1:3: missing identifier for member accessor"); + EXPECT_EQ(p->error(), "1:3: expected identifier for member accessor"); } TEST_F(ParserImplTest, PostfixExpression_MemberAccessor_MissingIdent) { @@ -175,7 +175,7 @@ TEST_F(ParserImplTest, PostfixExpression_MemberAccessor_MissingIdent) { auto e = p->postfix_expression(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(e, nullptr); - EXPECT_EQ(p->error(), "1:3: missing identifier for member accessor"); + EXPECT_EQ(p->error(), "1:3: expected identifier for member accessor"); } TEST_F(ParserImplTest, PostfixExpression_NonMatch_returnLHS) { diff --git a/src/reader/wgsl/parser_impl_struct_decl_test.cc b/src/reader/wgsl/parser_impl_struct_decl_test.cc index c217341239..884fea88c9 100644 --- a/src/reader/wgsl/parser_impl_struct_decl_test.cc +++ b/src/reader/wgsl/parser_impl_struct_decl_test.cc @@ -86,7 +86,7 @@ TEST_F(ParserImplTest, StructDecl_MissingIdent) { auto s = p->struct_decl(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(s, nullptr); - EXPECT_EQ(p->error(), "1:8: missing identifier for struct declaration"); + EXPECT_EQ(p->error(), "1:8: expected identifier for struct declaration"); } TEST_F(ParserImplTest, StructDecl_MissingBracketLeft) { diff --git a/src/reader/wgsl/parser_impl_type_alias_test.cc b/src/reader/wgsl/parser_impl_type_alias_test.cc index 72d50126c5..c580729076 100644 --- a/src/reader/wgsl/parser_impl_type_alias_test.cc +++ b/src/reader/wgsl/parser_impl_type_alias_test.cc @@ -62,7 +62,7 @@ TEST_F(ParserImplTest, TypeDecl_MissingIdent) { auto* t = p->type_alias(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:6: missing identifier for type alias"); + EXPECT_EQ(p->error(), "1:6: expected identifier for type alias"); } TEST_F(ParserImplTest, TypeDecl_InvalidIdent) { @@ -70,7 +70,7 @@ TEST_F(ParserImplTest, TypeDecl_InvalidIdent) { auto* t = p->type_alias(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:6: missing identifier for type alias"); + EXPECT_EQ(p->error(), "1:6: expected identifier for type alias"); } TEST_F(ParserImplTest, TypeDecl_MissingEqual) { @@ -78,7 +78,7 @@ TEST_F(ParserImplTest, TypeDecl_MissingEqual) { auto* t = p->type_alias(); ASSERT_TRUE(p->has_error()); ASSERT_EQ(t, nullptr); - EXPECT_EQ(p->error(), "1:8: missing = for type alias"); + EXPECT_EQ(p->error(), "1:8: expected '=' for type alias"); } TEST_F(ParserImplTest, TypeDecl_InvalidType) {