Title: [291004] trunk/Source/WebGPU
Revision
291004
Author
[email protected]
Date
2022-03-08 12:15:09 -0800 (Tue, 08 Mar 2022)

Log Message

[WGSL] The lexer should use the functions from wtf/ASCIICType.h
https://bugs.webkit.org/show_bug.cgi?id=236783

Reviewed by Darin Adler.

I was not aware of these functions when I wrote the lexer, and I ended up reimplementing them.
Reusing them is both simpler, and slightly more efficient as some of them (especially isASCIISpace) have been optimized.

* WGSL/Lexer.cpp:
(WGSL::Lexer<T>::lex):
(WGSL::Lexer<T>::skipWhitespace):
(WGSL::Lexer<T>::parseDecimalInteger):
(WGSL::Lexer<T>::parseDecimalFloatExponent):
(WGSL::Lexer<T>::isWhiteSpace): Deleted.
(WGSL::Lexer<T>::isIdentifierStart): Deleted.
(WGSL::Lexer<T>::isValidIdentifierCharacter): Deleted.
(WGSL::Lexer<T>::isDecimal): Deleted.
(WGSL::Lexer<T>::isHexadecimal): Deleted.
(WGSL::Lexer<T>::readDecimal): Deleted.
(WGSL::Lexer<T>::readHexadecimal): Deleted.
* WGSL/Lexer.h:

Modified Paths

Diff

Modified: trunk/Source/WebGPU/ChangeLog (291003 => 291004)


--- trunk/Source/WebGPU/ChangeLog	2022-03-08 20:05:36 UTC (rev 291003)
+++ trunk/Source/WebGPU/ChangeLog	2022-03-08 20:15:09 UTC (rev 291004)
@@ -1,3 +1,27 @@
+2022-03-08  Robin Morisset  <[email protected]>
+
+        [WGSL] The lexer should use the functions from wtf/ASCIICType.h
+        https://bugs.webkit.org/show_bug.cgi?id=236783
+
+        Reviewed by Darin Adler.
+
+        I was not aware of these functions when I wrote the lexer, and I ended up reimplementing them.
+        Reusing them is both simpler, and slightly more efficient as some of them (especially isASCIISpace) have been optimized.
+
+        * WGSL/Lexer.cpp:
+        (WGSL::Lexer<T>::lex):
+        (WGSL::Lexer<T>::skipWhitespace):
+        (WGSL::Lexer<T>::parseDecimalInteger):
+        (WGSL::Lexer<T>::parseDecimalFloatExponent):
+        (WGSL::Lexer<T>::isWhiteSpace): Deleted.
+        (WGSL::Lexer<T>::isIdentifierStart): Deleted.
+        (WGSL::Lexer<T>::isValidIdentifierCharacter): Deleted.
+        (WGSL::Lexer<T>::isDecimal): Deleted.
+        (WGSL::Lexer<T>::isHexadecimal): Deleted.
+        (WGSL::Lexer<T>::readDecimal): Deleted.
+        (WGSL::Lexer<T>::readHexadecimal): Deleted.
+        * WGSL/Lexer.h:
+
 2022-03-07  Michael Saboff  <[email protected]>
 
         Catalyst build failure in WebGPU when building with SYSTEM_CONTENT_PATH

Modified: trunk/Source/WebGPU/WGSL/Lexer.cpp (291003 => 291004)


--- trunk/Source/WebGPU/WGSL/Lexer.cpp	2022-03-08 20:05:36 UTC (rev 291003)
+++ trunk/Source/WebGPU/WGSL/Lexer.cpp	2022-03-08 20:15:09 UTC (rev 291004)
@@ -110,9 +110,9 @@
             // FIXME: add support for hexadecimal floating point literals
             shift();
             bool hexNumberIsEmpty = true;
-            while (isHexadecimal(m_current)) {
+            while (isASCIIHexDigit(m_current)) {
                 literalValue *= 16;
-                literalValue += readHexadecimal(m_current);
+                literalValue += toASCIIHexValue(m_current);
                 shift();
                 hexNumberIsEmpty = false;
             }
@@ -122,7 +122,7 @@
         }
 
         bool isFloatingPoint = false;
-        if (isDecimal(m_current) || m_current == '.' || m_current == 'e' || m_current == 'E') {
+        if (isASCIIDigit(m_current) || m_current == '.' || m_current == 'e' || m_current == 'E') {
             std::optional<uint64_t> integerPart = parseDecimalInteger();
             if (integerPart)
                 literalValue = integerPart.value();
@@ -159,7 +159,7 @@
         return parseIntegerLiteralSuffix(literalValue);
     }
     default:
-        if (isDecimal (m_current)) {
+        if (isASCIIDigit(m_current)) {
             std::optional<uint64_t> value = parseDecimalInteger();
             if (!value)
                 return makeToken(TokenType::Invalid);
@@ -267,7 +267,7 @@
 template <typename T>
 void Lexer<T>::skipWhitespace()
 {
-    while (isWhiteSpace(m_current)) {
+    while (isASCIISpace(m_current)) {
         if (m_current == '\n') {
             shift();
             ++m_currentPosition.m_line;
@@ -291,11 +291,11 @@
 template <typename T>
 std::optional<uint64_t> Lexer<T>::parseDecimalInteger()
 {
-    if (!isDecimal(m_current))
+    if (!isASCIIDigit(m_current))
         return std::nullopt;
 
     CheckedUint64 value = 0;
-    while (isDecimal(m_current)) {
+    while (isASCIIDigit(m_current)) {
         value *= 10ull;
         value += readDecimal(m_current);
         shift();
@@ -315,9 +315,9 @@
     if (m_current != 'e' && m_current != 'E')
         return std::nullopt;
     if (char1 == '+' || char1 == '-') {
-        if (!isDecimal(char2))
+        if (!isASCIIDigit(char2))
             return std::nullopt;
-    } else if (!isDecimal(char1))
+    } else if (!isASCIIDigit(char1))
         return std::nullopt;
     shift();
 
@@ -353,65 +353,6 @@
     return makeLiteralToken(TokenType::IntegerLiteral, literalValue);
 };
 
-template <typename T>
-ALWAYS_INLINE bool Lexer<T>::isWhiteSpace(T ch)
-{
-    switch (ch) {
-    case WTF::Unicode::space:
-    case WTF::Unicode::tabCharacter:
-    case WTF::Unicode::carriageReturn:
-    case WTF::Unicode::newlineCharacter:
-    case WTF::Unicode::verticalTabulation:
-    case WTF::Unicode::formFeed:
-        return true;
-    default:
-        return false;
-    }
-}
-
-template <typename T>
-ALWAYS_INLINE bool Lexer<T>::isIdentifierStart(T ch)
-{
-    return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
-}
-
-template <typename T>
-ALWAYS_INLINE bool Lexer<T>::isValidIdentifierCharacter(T ch)
-{
-    return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_';
-}
-
-template <typename T>
-ALWAYS_INLINE bool Lexer<T>::isDecimal(T ch)
-{
-    return (ch >= '0' && ch <= '9');
-}
-
-template <typename T>
-ALWAYS_INLINE bool Lexer<T>::isHexadecimal(T ch)
-{
-    return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
-}
-
-template <typename T>
-ALWAYS_INLINE uint64_t Lexer<T>::readDecimal(T ch)
-{
-    ASSERT(isDecimal(ch));
-    return ch - '0';
-}
-
-template <typename T>
-ALWAYS_INLINE uint64_t Lexer<T>::readHexadecimal(T ch)
-{
-    ASSERT(isHexadecimal(ch));
-    if (ch >= '0' && ch <= '9')
-        return ch - '0';
-    if (ch >= 'a' && ch <= 'f')
-        return ch - 'a';
-    ASSERT(ch >= 'A' && ch <= 'F');
-    return ch - 'A';
-}
-
 template class Lexer<LChar>;
 template class Lexer<UChar>;
 

Modified: trunk/Source/WebGPU/WGSL/Lexer.h (291003 => 291004)


--- trunk/Source/WebGPU/WGSL/Lexer.h	2022-03-08 20:05:36 UTC (rev 291003)
+++ trunk/Source/WebGPU/WGSL/Lexer.h	2022-03-08 20:15:09 UTC (rev 291004)
@@ -26,6 +26,7 @@
 #pragma once
 
 #include "Token.h"
+#include <wtf/ASCIICType.h>
 #include <wtf/text/StringView.h>
 #include <wtf/text/WTFString.h>
 
@@ -53,15 +54,6 @@
     bool isAtEndOfFile() const;
     SourcePosition currentPosition() const { return m_currentPosition; }
 
-    // Only public so that the UChar version of the Lexer can defer to the LChar version
-    static bool isWhiteSpace(T character);
-    static bool isIdentifierStart(T character);
-    static bool isValidIdentifierCharacter(T character);
-    static bool isDecimal(T character);
-    static bool isHexadecimal(T character);
-    static uint64_t readDecimal(T character);
-    static uint64_t readHexadecimal(T character);
-
 private:
     unsigned currentOffset() const { return m_currentPosition.m_offset; }
     unsigned currentTokenLength() const { return currentOffset() - m_tokenStartingPosition.m_offset; }
@@ -90,6 +82,14 @@
     // Checks whether there is an "i" or "u" coming, and return the right kind of literal token
     Token parseIntegerLiteralSuffix(double literalValue);
 
+    static bool isIdentifierStart(T character) { return isASCIIAlpha(character); }
+    static bool isValidIdentifierCharacter(T character) { return isASCIIAlphanumeric(character) || character == '_'; }
+    static unsigned readDecimal(T character)
+    {
+        ASSERT(isASCIIDigit(character));
+        return character - '0';
+    }
+
     T m_current;
     const T* m_code;
     const T* m_codeEnd;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to