wsgl parser: Use expect_ident()

Keeps error message consistent. Reduces code.

Bug: tint:282
Change-Id: Id6e219222a5967bb4b6d67e54816f669c38d0c19
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31731
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2020-11-04 16:34:10 +00:00 committed by Commit Bot service account
parent 77f7bb5b00
commit 0371cc4a8d
6 changed files with 42 additions and 50 deletions

View File

@ -457,16 +457,16 @@ std::unique_ptr<ast::VariableDecoration> 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<ast::type::StructType> 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<ast::Function> 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<ast::Expression> 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<ast::MemberAccessorExpression>(
source, std::move(prefix),
std::make_unique<ast::IdentifierExpression>(t.source(), t.to_str()));
std::make_unique<ast::IdentifierExpression>(source, ident));
} else {
return prefix;
}

View File

@ -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");
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {