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:
parent
77f7bb5b00
commit
0371cc4a8d
|
@ -457,16 +457,16 @@ std::unique_ptr<ast::VariableDecoration> ParserImpl::variable_decoration() {
|
||||||
if (!expect("builtin decoration", Token::Type::kParenLeft))
|
if (!expect("builtin decoration", Token::Type::kParenLeft))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
t = next();
|
source = peek().source();
|
||||||
if (!t.IsIdentifier() || t.to_str().empty()) {
|
|
||||||
add_error(t, "expected identifier for builtin");
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
if (builtin == ast::Builtin::kNone) {
|
||||||
add_error(t, "invalid value for builtin decoration");
|
add_error(source, "invalid value for builtin decoration");
|
||||||
return {};
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!expect("builtin decoration", Token::Type::kParenRight))
|
if (!expect("builtin decoration", Token::Type::kParenRight))
|
||||||
|
@ -1035,18 +1035,14 @@ ast::type::Type* ParserImpl::type_alias() {
|
||||||
|
|
||||||
next(); // Consume the peek
|
next(); // Consume the peek
|
||||||
|
|
||||||
t = next();
|
const char* use = "type alias";
|
||||||
if (!t.IsIdentifier()) {
|
|
||||||
add_error(t, "missing identifier for type alias");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
auto name = t.to_str();
|
|
||||||
|
|
||||||
t = next();
|
std::string name;
|
||||||
if (!t.IsEqual()) {
|
if (!expect_ident(use, &name))
|
||||||
add_error(t, "missing = for type alias");
|
return nullptr;
|
||||||
|
|
||||||
|
if (!expect(use, Token::Type::kEqual))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
|
||||||
|
|
||||||
auto* type = type_decl();
|
auto* type = type_decl();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
|
@ -1433,12 +1429,9 @@ std::unique_ptr<ast::type::StructType> ParserImpl::struct_decl() {
|
||||||
}
|
}
|
||||||
next(); // Consume the peek
|
next(); // Consume the peek
|
||||||
|
|
||||||
t = next();
|
std::string name;
|
||||||
if (!t.IsIdentifier()) {
|
if (!expect_ident("struct declaration", &name))
|
||||||
add_error(t, "missing identifier for struct declaration");
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
|
||||||
auto name = t.to_str();
|
|
||||||
|
|
||||||
auto body = struct_body_decl();
|
auto body = struct_body_decl();
|
||||||
if (has_error()) {
|
if (has_error()) {
|
||||||
|
@ -1791,24 +1784,23 @@ std::unique_ptr<ast::Function> ParserImpl::function_header() {
|
||||||
if (!match(Token::Type::kFn))
|
if (!match(Token::Type::kFn))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto t = next();
|
const char* use = "function declaration";
|
||||||
if (!t.IsIdentifier()) {
|
|
||||||
add_error(t, "missing identifier for function");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
auto name = t.to_str();
|
|
||||||
|
|
||||||
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;
|
return nullptr;
|
||||||
|
|
||||||
auto params = param_list();
|
auto params = param_list();
|
||||||
if (has_error())
|
if (has_error())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
if (!expect("function declaration", Token::Type::kParenRight))
|
if (!expect(use, Token::Type::kParenRight))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
t = next();
|
auto t = next();
|
||||||
if (!t.IsArrow()) {
|
if (!t.IsArrow()) {
|
||||||
add_error(t, "missing -> for function declaration");
|
add_error(t, "missing -> for function declaration");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -2768,15 +2760,15 @@ std::unique_ptr<ast::Expression> ParserImpl::postfix_expr(
|
||||||
} else if (t.IsPeriod()) {
|
} else if (t.IsPeriod()) {
|
||||||
next(); // Consume the peek
|
next(); // Consume the peek
|
||||||
|
|
||||||
t = next();
|
source = peek().source();
|
||||||
if (!t.IsIdentifier()) {
|
|
||||||
add_error(t, "missing identifier for member accessor");
|
std::string ident;
|
||||||
|
if (!expect_ident("member accessor", &ident))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
|
||||||
|
|
||||||
expr = std::make_unique<ast::MemberAccessorExpression>(
|
expr = std::make_unique<ast::MemberAccessorExpression>(
|
||||||
source, std::move(prefix),
|
source, std::move(prefix),
|
||||||
std::make_unique<ast::IdentifierExpression>(t.source(), t.to_str()));
|
std::make_unique<ast::IdentifierExpression>(source, ident));
|
||||||
} else {
|
} else {
|
||||||
return prefix;
|
return prefix;
|
||||||
}
|
}
|
||||||
|
|
|
@ -382,7 +382,7 @@ TEST_F(ParserImplErrorTest, FunctionDeclDecoWorkgroupSizeZZero) {
|
||||||
|
|
||||||
TEST_F(ParserImplErrorTest, FunctionDeclMissingIdentifier) {
|
TEST_F(ParserImplErrorTest, FunctionDeclMissingIdentifier) {
|
||||||
EXPECT("fn () -> void {}",
|
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"
|
"fn () -> void {}\n"
|
||||||
" ^\n");
|
" ^\n");
|
||||||
}
|
}
|
||||||
|
@ -612,7 +612,7 @@ TEST_F(ParserImplErrorTest, GlobalDeclStructDecoMissingEnd) {
|
||||||
|
|
||||||
TEST_F(ParserImplErrorTest, GlobalDeclStructDeclMissingIdentifier) {
|
TEST_F(ParserImplErrorTest, GlobalDeclStructDeclMissingIdentifier) {
|
||||||
EXPECT("struct {};",
|
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"
|
"struct {};\n"
|
||||||
" ^\n");
|
" ^\n");
|
||||||
}
|
}
|
||||||
|
@ -697,7 +697,7 @@ TEST_F(ParserImplErrorTest, GlobalDeclStructMemberOffsetNegativeValue) {
|
||||||
|
|
||||||
TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasMissingIdentifier) {
|
TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasMissingIdentifier) {
|
||||||
EXPECT("type 1 = f32;",
|
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"
|
"type 1 = f32;\n"
|
||||||
" ^\n");
|
" ^\n");
|
||||||
}
|
}
|
||||||
|
@ -711,7 +711,7 @@ TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasInvalidType) {
|
||||||
|
|
||||||
TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasMissingAssignment) {
|
TEST_F(ParserImplErrorTest, GlobalDeclTypeAliasMissingAssignment) {
|
||||||
EXPECT("type meow f32",
|
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"
|
"type meow f32\n"
|
||||||
" ^^^\n");
|
" ^^^\n");
|
||||||
}
|
}
|
||||||
|
@ -1094,7 +1094,7 @@ TEST_F(ParserImplErrorTest, LoopMissingRBrace) {
|
||||||
|
|
||||||
TEST_F(ParserImplErrorTest, MemberExprMissingIdentifier) {
|
TEST_F(ParserImplErrorTest, MemberExprMissingIdentifier) {
|
||||||
EXPECT("fn f() -> void { x = a.; }",
|
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"
|
"fn f() -> void { x = a.; }\n"
|
||||||
" ^\n");
|
" ^\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ TEST_F(ParserImplTest, FunctionHeader_MissingIdent) {
|
||||||
auto f = p->function_header();
|
auto f = p->function_header();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(f, nullptr);
|
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) {
|
TEST_F(ParserImplTest, FunctionHeader_InvalidIdent) {
|
||||||
|
@ -49,7 +49,7 @@ TEST_F(ParserImplTest, FunctionHeader_InvalidIdent) {
|
||||||
auto f = p->function_header();
|
auto f = p->function_header();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(f, nullptr);
|
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) {
|
TEST_F(ParserImplTest, FunctionHeader_MissingParenLeft) {
|
||||||
|
|
|
@ -167,7 +167,7 @@ TEST_F(ParserImplTest, PostfixExpression_MemberAccesssor_InvalidIdent) {
|
||||||
auto e = p->postfix_expression();
|
auto e = p->postfix_expression();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(e, nullptr);
|
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) {
|
TEST_F(ParserImplTest, PostfixExpression_MemberAccessor_MissingIdent) {
|
||||||
|
@ -175,7 +175,7 @@ TEST_F(ParserImplTest, PostfixExpression_MemberAccessor_MissingIdent) {
|
||||||
auto e = p->postfix_expression();
|
auto e = p->postfix_expression();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(e, nullptr);
|
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) {
|
TEST_F(ParserImplTest, PostfixExpression_NonMatch_returnLHS) {
|
||||||
|
|
|
@ -86,7 +86,7 @@ TEST_F(ParserImplTest, StructDecl_MissingIdent) {
|
||||||
auto s = p->struct_decl();
|
auto s = p->struct_decl();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(s, nullptr);
|
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) {
|
TEST_F(ParserImplTest, StructDecl_MissingBracketLeft) {
|
||||||
|
|
|
@ -62,7 +62,7 @@ TEST_F(ParserImplTest, TypeDecl_MissingIdent) {
|
||||||
auto* t = p->type_alias();
|
auto* t = p->type_alias();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(t, nullptr);
|
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) {
|
TEST_F(ParserImplTest, TypeDecl_InvalidIdent) {
|
||||||
|
@ -70,7 +70,7 @@ TEST_F(ParserImplTest, TypeDecl_InvalidIdent) {
|
||||||
auto* t = p->type_alias();
|
auto* t = p->type_alias();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(t, nullptr);
|
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) {
|
TEST_F(ParserImplTest, TypeDecl_MissingEqual) {
|
||||||
|
@ -78,7 +78,7 @@ TEST_F(ParserImplTest, TypeDecl_MissingEqual) {
|
||||||
auto* t = p->type_alias();
|
auto* t = p->type_alias();
|
||||||
ASSERT_TRUE(p->has_error());
|
ASSERT_TRUE(p->has_error());
|
||||||
ASSERT_EQ(t, nullptr);
|
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) {
|
TEST_F(ParserImplTest, TypeDecl_InvalidType) {
|
||||||
|
|
Loading…
Reference in New Issue