From 74adb68722ba342ac557523aad68a4e1a1037b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 1 Oct 2026 20:18:37 -0700 Subject: [PATCH] Fixed LiteralParser accepting a float suffix on a hex float with no exponent. (#437) --- src/literal-parser.cpp | 13 +++++++++---- tests/literal-parser_test.cpp | 26 +++++++++++++++----------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/literal-parser.cpp b/src/literal-parser.cpp index ca339ba..824940f 100644 --- a/src/literal-parser.cpp +++ b/src/literal-parser.cpp @@ -154,8 +154,9 @@ namespace bx { const bool anyHex = acceptDigits(_scanner, isHexNum, _profile.separator); - bool isFloat = false; - bool anyHexFrac = false; + bool isFloat = false; + bool anyHexFrac = false; + bool anyExponent = false; if (0 != (_profile.flags & LiteralParser::HexFloat) ) { if (!_scanner.accept('.').isEmpty() ) @@ -168,7 +169,8 @@ namespace bx if ('p' == ch || 'P' == ch) { - isFloat = true; + isFloat = true; + anyExponent = true; _scanner.accept(); _scanner.accept('-', '+'); @@ -188,7 +190,10 @@ namespace bx return LiteralType::Unknown; } - acceptSuffix(_scanner, _profile.floatSuffix); + if (anyExponent) + { + acceptSuffix(_scanner, _profile.floatSuffix); + } return LiteralType::Float; } diff --git a/tests/literal-parser_test.cpp b/tests/literal-parser_test.cpp index 52817e3..d1458cf 100644 --- a/tests/literal-parser_test.cpp +++ b/tests/literal-parser_test.cpp @@ -212,17 +212,21 @@ TEST_CASE("LiteralParser WGSL profile", "[parser]") REQUIRE(testWgsl("0xffu", LP::Hexadecimal) ); // floats: fraction/exponent, edge dots, suffixes, hex float - REQUIRE(testWgsl("1.0", LP::Float) ); - REQUIRE(testWgsl(".5", LP::Float) ); - REQUIRE(testWgsl("2.", LP::Float) ); - REQUIRE(testWgsl("1e5", LP::Float) ); - REQUIRE(testWgsl("3.14e-2", LP::Float) ); - REQUIRE(testWgsl("1.5f", LP::Float) ); - REQUIRE(testWgsl("1f", LP::Float) ); // bare integer + float suffix -> float - REQUIRE(testWgsl("1h", LP::Float) ); - REQUIRE(testWgsl("1lf", LP::Float) ); - REQUIRE(testWgsl("0x1p-3", LP::Float) ); - REQUIRE(testWgsl("0x1.8p3", LP::Float) ); + REQUIRE(testWgsl("1.0", LP::Float) ); + REQUIRE(testWgsl(".5", LP::Float) ); + REQUIRE(testWgsl("2.", LP::Float) ); + REQUIRE(testWgsl("1e5", LP::Float) ); + REQUIRE(testWgsl("3.14e-2", LP::Float) ); + REQUIRE(testWgsl("1.5f", LP::Float) ); + REQUIRE(testWgsl("1f", LP::Float) ); // bare integer + float suffix -> float + REQUIRE(testWgsl("1h", LP::Float) ); + REQUIRE(testWgsl("1lf", LP::Float) ); + REQUIRE(testWgsl("0x1p-3", LP::Float) ); + REQUIRE(testWgsl("0x1.8p3", LP::Float) ); + REQUIRE(testWgsl("0x1.8p3h", LP::Float) ); + REQUIRE(testWgsl("0x1.8p3f", LP::Float) ); + REQUIRE(testWgsl("0xf.h", "0xf.", LP::Float) ); + REQUIRE(testWgsl("0xf.f", LP::Float) ); // prefix match: trailing input is not part of the literal REQUIRE(testWgsl("42;", "42", LP::Decimal) );