[tint] Add explicit error for double leading underscores on identifiers.

The old error was super confusing.

Change-Id: Ic288570158e1162ebff90822d4e8747e86eed500
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/177661
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/lang/wgsl/reader/parser/lexer.cc b/src/tint/lang/wgsl/reader/parser/lexer.cc
index 117f30a..b12c0ec 100644
--- a/src/tint/lang/wgsl/reader/parser/lexer.cc
+++ b/src/tint/lang/wgsl/reader/parser/lexer.cc
@@ -1012,19 +1012,17 @@
 
         // Consume continuing codepoint
         advance(static_cast<uint32_t>(n));
-
-        if (pos() - start == 2 && substr(start, 2) == "__") {
-            // Identifiers prefixed with two or more underscores are not allowed.
-            // We check for these in the loop to bail early and prevent quadratic parse time for
-            // long sequences of ____.
-            set_pos(start);
-            return {};
-        }
     }
 
     auto str = substr(start, pos() - start);
     end_source(source);
 
+    if (str.length() > 1 && substr(start, 2) == "__") {
+        // Identifiers prefixed with two or more underscores are not allowed.
+        return Token{Token::Type::kError, source,
+                     "identifiers must not start with two or more underscores"};
+    }
+
     if (auto t = parse_keyword(str); t.has_value()) {
         return Token{t.value(), source, str};
     }
diff --git a/src/tint/lang/wgsl/reader/parser/lexer_test.cc b/src/tint/lang/wgsl/reader/parser/lexer_test.cc
index b2b593b..38cbd46 100644
--- a/src/tint/lang/wgsl/reader/parser/lexer_test.cc
+++ b/src/tint/lang/wgsl/reader/parser/lexer_test.cc
@@ -769,7 +769,8 @@
     ASSERT_FALSE(list.empty());
 
     auto& t = list[0];
-    EXPECT_FALSE(t.IsIdentifier());
+    EXPECT_TRUE(t.IsError());
+    EXPECT_EQ(t.to_str(), "identifiers must not start with two or more underscores");
 }
 
 TEST_F(LexerTest, IdentifierTest_DoesNotStartWithNumber) {