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>
diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc
index 0176c83..d3e7536 100644
--- a/src/reader/wgsl/parser_impl.cc
+++ b/src/reader/wgsl/parser_impl.cc
@@ -457,16 +457,16 @@
     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 @@
 
   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 @@
   }
   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 @@
   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 @@
   } 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;
   }
diff --git a/src/reader/wgsl/parser_impl_error_msg_test.cc b/src/reader/wgsl/parser_impl_error_msg_test.cc
index e035f9f..366f839 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, 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, 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, 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, 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, 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 469dafe..dfd053c 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 @@
   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 @@
   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 ab16e53..31ca9c6 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 @@
   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 @@
   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 c217341..884fea8 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 @@
   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 72d5012..c580729 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 @@
   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 @@
   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 @@
   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) {