wgsl-reader: hex prefix only is an error

These are errors:

    let a = 0x;
    let b = -0x;

Fixes: tint:1338
Change-Id: I9d26ad66e32deb954550c0ecfbda0a9005bcd380
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/72380
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: David Neto <dneto@google.com>
Auto-Submit: David Neto <dneto@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
David Neto 2021-12-09 22:35:44 +00:00 committed by Tint LUCI CQ
parent 1461b032aa
commit 9c179a601c
2 changed files with 26 additions and 0 deletions

View File

@ -681,6 +681,10 @@ Token Lexer::try_hex_integer() {
"...) has too many digits"}; "...) has too many digits"};
} }
} }
if (first == end) {
return {Token::Type::kError, source,
"integer or float hex literal has no significant digits"};
}
pos_ = end; pos_ = end;
location_.column += (end - start); location_.column += (end - start);

View File

@ -400,6 +400,28 @@ INSTANTIATE_TEST_SUITE_P(
HexSignedIntData{"-0x80000000", std::numeric_limits<int32_t>::min()}, HexSignedIntData{"-0x80000000", std::numeric_limits<int32_t>::min()},
HexSignedIntData{"0x7FFFFFFF", std::numeric_limits<int32_t>::max()})); HexSignedIntData{"0x7FFFFFFF", std::numeric_limits<int32_t>::max()}));
TEST_F(LexerTest, HexPrefixOnly_IsError) {
// Could be the start of a hex integer or hex float, but is neither.
Source::FileContent content("0x");
Lexer l("test.wgsl", &content);
auto t = l.next();
ASSERT_TRUE(t.Is(Token::Type::kError));
EXPECT_EQ(t.to_str(),
"integer or float hex literal has no significant digits");
}
TEST_F(LexerTest, NegativeHexPrefixOnly_IsError) {
// Could be the start of a hex integer or hex float, but is neither.
Source::FileContent content("-0x");
Lexer l("test.wgsl", &content);
auto t = l.next();
ASSERT_TRUE(t.Is(Token::Type::kError));
EXPECT_EQ(t.to_str(),
"integer or float hex literal has no significant digits");
}
TEST_F(LexerTest, IntegerTest_HexSignedTooLarge) { TEST_F(LexerTest, IntegerTest_HexSignedTooLarge) {
Source::FileContent content("0x80000000"); Source::FileContent content("0x80000000");
Lexer l("test.wgsl", &content); Lexer l("test.wgsl", &content);