Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 15 | #include "src/tint/lang/wgsl/reader/parser/lexer.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 16 | |
| 17 | #include <limits> |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 18 | #include <tuple> |
| 19 | #include <vector> |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 20 | |
| 21 | #include "gtest/gtest.h" |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 22 | #include "src/tint/lang/core/fluent_types.h" |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 23 | #include "src/tint/lang/core/number.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 24 | |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 25 | using namespace tint::core::fluent_types; // NOLINT |
| 26 | |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 27 | namespace tint::wgsl::reader { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | using LexerTest = testing::Test; |
| 31 | |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 32 | // Blankspace constants. These are macros on purpose to be able to easily build |
| 33 | // up string literals with them. |
| 34 | // |
| 35 | // Same line code points |
| 36 | #define kSpace " " |
| 37 | #define kHTab "\t" |
| 38 | #define kL2R "\xE2\x80\x8E" |
| 39 | #define kR2L "\xE2\x80\x8F" |
| 40 | // Line break code points |
| 41 | #define kCR "\r" |
| 42 | #define kLF "\n" |
| 43 | #define kVTab "\x0B" |
| 44 | #define kFF "\x0C" |
| 45 | #define kNL "\xC2\x85" |
| 46 | #define kLS "\xE2\x80\xA8" |
| 47 | #define kPS "\xE2\x80\xA9" |
| 48 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | TEST_F(LexerTest, Empty) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 50 | Source::File file("", ""); |
| 51 | Lexer l(&file); |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 52 | |
| 53 | auto list = l.Lex(); |
| 54 | ASSERT_EQ(1u, list.size()); |
| 55 | EXPECT_TRUE(list[0].IsEof()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 58 | TEST_F(LexerTest, Skips_Blankspace_Basic) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | Source::File file("", "\t\r\n\t ident\t\n\t \r "); |
| 60 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 62 | auto list = l.Lex(); |
| 63 | ASSERT_EQ(2u, list.size()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 65 | { |
| 66 | auto& t = list[0]; |
| 67 | EXPECT_TRUE(t.IsIdentifier()); |
| 68 | EXPECT_EQ(t.source().range.begin.line, 2u); |
| 69 | EXPECT_EQ(t.source().range.begin.column, 6u); |
| 70 | EXPECT_EQ(t.source().range.end.line, 2u); |
| 71 | EXPECT_EQ(t.source().range.end.column, 11u); |
| 72 | EXPECT_EQ(t.to_str(), "ident"); |
| 73 | } |
| 74 | |
| 75 | { |
| 76 | auto& t = list[1]; |
| 77 | EXPECT_TRUE(t.IsEof()); |
| 78 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 81 | TEST_F(LexerTest, Skips_Blankspace_Exotic) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 82 | Source::File file("", // |
| 83 | kVTab kFF kNL kLS kPS kL2R kR2L // |
| 84 | "ident" // |
| 85 | kVTab kFF kNL kLS kPS kL2R kR2L); |
| 86 | Lexer l(&file); |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 87 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 88 | auto list = l.Lex(); |
| 89 | ASSERT_EQ(2u, list.size()); |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 90 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 91 | { |
| 92 | auto& t = list[0]; |
| 93 | EXPECT_TRUE(t.IsIdentifier()); |
| 94 | EXPECT_EQ(t.source().range.begin.line, 6u); |
| 95 | EXPECT_EQ(t.source().range.begin.column, 7u); |
| 96 | EXPECT_EQ(t.source().range.end.line, 6u); |
| 97 | EXPECT_EQ(t.source().range.end.column, 12u); |
| 98 | EXPECT_EQ(t.to_str(), "ident"); |
| 99 | } |
| 100 | |
| 101 | { |
| 102 | auto& t = list[1]; |
| 103 | EXPECT_TRUE(t.IsEof()); |
| 104 | } |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 107 | TEST_F(LexerTest, Skips_Comments_Line) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 108 | Source::File file("", R"(//starts with comment |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 109 | ident1 //ends with comment |
| 110 | // blank line |
| 111 | ident2)"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 112 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 113 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 114 | auto list = l.Lex(); |
| 115 | ASSERT_EQ(3u, list.size()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 116 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 117 | { |
| 118 | auto& t = list[0]; |
| 119 | EXPECT_TRUE(t.IsIdentifier()); |
| 120 | EXPECT_EQ(t.source().range.begin.line, 2u); |
| 121 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 122 | EXPECT_EQ(t.source().range.end.line, 2u); |
| 123 | EXPECT_EQ(t.source().range.end.column, 7u); |
| 124 | EXPECT_EQ(t.to_str(), "ident1"); |
| 125 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 126 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 127 | { |
| 128 | auto& t = list[1]; |
| 129 | EXPECT_TRUE(t.IsIdentifier()); |
| 130 | EXPECT_EQ(t.source().range.begin.line, 4u); |
| 131 | EXPECT_EQ(t.source().range.begin.column, 2u); |
| 132 | EXPECT_EQ(t.source().range.end.line, 4u); |
| 133 | EXPECT_EQ(t.source().range.end.column, 8u); |
| 134 | EXPECT_EQ(t.to_str(), "ident2"); |
| 135 | } |
| 136 | |
| 137 | { |
| 138 | auto& t = list[2]; |
| 139 | EXPECT_TRUE(t.IsEof()); |
| 140 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 143 | TEST_F(LexerTest, Skips_Comments_Unicode) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 144 | Source::File file("", R"(// starts with 🙂🙂🙂 |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 145 | ident1 //ends with 🙂🙂🙂 |
| 146 | // blank line |
| 147 | ident2)"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 148 | Lexer l(&file); |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 149 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 150 | auto list = l.Lex(); |
| 151 | ASSERT_EQ(3u, list.size()); |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 152 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 153 | { |
| 154 | auto& t = list[0]; |
| 155 | EXPECT_TRUE(t.IsIdentifier()); |
| 156 | EXPECT_EQ(t.source().range.begin.line, 2u); |
| 157 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 158 | EXPECT_EQ(t.source().range.end.line, 2u); |
| 159 | EXPECT_EQ(t.source().range.end.column, 7u); |
| 160 | EXPECT_EQ(t.to_str(), "ident1"); |
| 161 | } |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 162 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 163 | { |
| 164 | auto& t = list[1]; |
| 165 | EXPECT_TRUE(t.IsIdentifier()); |
| 166 | EXPECT_EQ(t.source().range.begin.line, 4u); |
| 167 | EXPECT_EQ(t.source().range.begin.column, 2u); |
| 168 | EXPECT_EQ(t.source().range.end.line, 4u); |
| 169 | EXPECT_EQ(t.source().range.end.column, 8u); |
| 170 | EXPECT_EQ(t.to_str(), "ident2"); |
| 171 | } |
| 172 | |
| 173 | { |
| 174 | auto& t = list[2]; |
| 175 | EXPECT_TRUE(t.IsEof()); |
| 176 | } |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | using LineCommentTerminatorTest = testing::TestWithParam<const char*>; |
James Price | 453d5ae | 2022-03-16 21:45:44 +0000 | [diff] [blame] | 180 | TEST_P(LineCommentTerminatorTest, Terminators) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 181 | // Test that line comments are ended by blankspace characters other than |
| 182 | // space, horizontal tab, left-to-right mark, and right-to-left mark. |
Ben Clayton | e55877d | 2022-05-04 13:58:49 +0000 | [diff] [blame] | 183 | auto* c = GetParam(); |
Ben Clayton | e48ef8e | 2022-06-26 10:52:50 +0000 | [diff] [blame] | 184 | std::string src = "const// This is a comment"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 185 | src += c; |
| 186 | src += "ident"; |
| 187 | Source::File file("", src); |
| 188 | Lexer l(&file); |
James Price | 453d5ae | 2022-03-16 21:45:44 +0000 | [diff] [blame] | 189 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 190 | auto is_same_line = [](std::string_view v) { |
| 191 | return v == kSpace || v == kHTab || v == kL2R || v == kR2L; |
| 192 | }; |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 193 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 194 | auto list = l.Lex(); |
| 195 | ASSERT_EQ(is_same_line(c) ? 2u : 3u, list.size()); |
| 196 | |
| 197 | size_t idx = 0; |
| 198 | |
| 199 | { |
| 200 | auto& t = list[idx++]; |
| 201 | EXPECT_TRUE(t.Is(Token::Type::kConst)); |
| 202 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 203 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 204 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 205 | EXPECT_EQ(t.source().range.end.column, 6u); |
| 206 | } |
| 207 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 208 | if (!is_same_line(c)) { |
| 209 | size_t line = is_same_line(c) ? 1u : 2u; |
| 210 | size_t col = is_same_line(c) ? 25u : 1u; |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 211 | |
| 212 | auto& t = list[idx++]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 213 | EXPECT_TRUE(t.IsIdentifier()); |
| 214 | EXPECT_EQ(t.source().range.begin.line, line); |
| 215 | EXPECT_EQ(t.source().range.begin.column, col); |
| 216 | EXPECT_EQ(t.source().range.end.line, line); |
| 217 | EXPECT_EQ(t.source().range.end.column, col + 5); |
| 218 | EXPECT_EQ(t.to_str(), "ident"); |
| 219 | } |
| 220 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 221 | { |
| 222 | auto& t = list[idx]; |
| 223 | EXPECT_TRUE(t.IsEof()); |
| 224 | } |
James Price | 453d5ae | 2022-03-16 21:45:44 +0000 | [diff] [blame] | 225 | } |
| 226 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 227 | LineCommentTerminatorTest, |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 228 | testing::Values( |
| 229 | // same line |
| 230 | kSpace, |
| 231 | kHTab, |
| 232 | kCR, |
| 233 | kL2R, |
| 234 | kR2L, |
| 235 | // line break |
| 236 | kLF, |
| 237 | kVTab, |
| 238 | kFF, |
| 239 | kNL, |
| 240 | kLS, |
| 241 | kPS)); |
James Price | 453d5ae | 2022-03-16 21:45:44 +0000 | [diff] [blame] | 242 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 243 | TEST_F(LexerTest, Skips_Comments_Block) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 244 | Source::File file("", R"(/* comment |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 245 | text */ident)"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 246 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 247 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 248 | auto list = l.Lex(); |
| 249 | ASSERT_EQ(2u, list.size()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 250 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 251 | { |
| 252 | auto& t = list[0]; |
| 253 | EXPECT_TRUE(t.IsIdentifier()); |
| 254 | EXPECT_EQ(t.source().range.begin.line, 2u); |
| 255 | EXPECT_EQ(t.source().range.begin.column, 8u); |
| 256 | EXPECT_EQ(t.source().range.end.line, 2u); |
| 257 | EXPECT_EQ(t.source().range.end.column, 13u); |
| 258 | EXPECT_EQ(t.to_str(), "ident"); |
| 259 | } |
| 260 | |
| 261 | { |
| 262 | auto& t = list[1]; |
| 263 | EXPECT_TRUE(t.IsEof()); |
| 264 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | TEST_F(LexerTest, Skips_Comments_Block_Nested) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 268 | Source::File file("", R"(/* comment |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 269 | text // nested line comments are ignored /* more text |
| 270 | /////**/ */*/ident)"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 271 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 272 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 273 | auto list = l.Lex(); |
| 274 | ASSERT_EQ(2u, list.size()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 275 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 276 | { |
| 277 | auto& t = list[0]; |
| 278 | EXPECT_TRUE(t.IsIdentifier()); |
| 279 | EXPECT_EQ(t.source().range.begin.line, 3u); |
| 280 | EXPECT_EQ(t.source().range.begin.column, 14u); |
| 281 | EXPECT_EQ(t.source().range.end.line, 3u); |
| 282 | EXPECT_EQ(t.source().range.end.column, 19u); |
| 283 | EXPECT_EQ(t.to_str(), "ident"); |
| 284 | } |
| 285 | |
| 286 | { |
| 287 | auto& t = list[1]; |
| 288 | EXPECT_TRUE(t.IsEof()); |
| 289 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | TEST_F(LexerTest, Skips_Comments_Block_Unterminated) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 293 | // I had to break up the /* because otherwise the clang readability check |
| 294 | // errored out saying it could not find the end of a multi-line comment. |
| 295 | Source::File file("", R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 296 | /)" |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 297 | R"(* |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 298 | abcd)"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 299 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 300 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 301 | auto list = l.Lex(); |
| 302 | ASSERT_EQ(1u, list.size()); |
| 303 | |
| 304 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 305 | ASSERT_TRUE(t.Is(Token::Type::kError)); |
| 306 | EXPECT_EQ(t.to_str(), "unterminated block comment"); |
| 307 | EXPECT_EQ(t.source().range.begin.line, 2u); |
| 308 | EXPECT_EQ(t.source().range.begin.column, 3u); |
| 309 | EXPECT_EQ(t.source().range.end.line, 2u); |
| 310 | EXPECT_EQ(t.source().range.end.column, 4u); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 311 | } |
| 312 | |
James Price | 453d5ae | 2022-03-16 21:45:44 +0000 | [diff] [blame] | 313 | TEST_F(LexerTest, Null_InBlankspace_IsError) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 314 | Source::File file("", std::string{' ', 0, ' '}); |
| 315 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 316 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 317 | auto list = l.Lex(); |
| 318 | ASSERT_EQ(1u, list.size()); |
| 319 | |
| 320 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 321 | EXPECT_TRUE(t.IsError()); |
| 322 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 323 | EXPECT_EQ(t.source().range.begin.column, 2u); |
| 324 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 325 | EXPECT_EQ(t.source().range.end.column, 2u); |
| 326 | EXPECT_EQ(t.to_str(), "null character found"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | TEST_F(LexerTest, Null_InLineComment_IsError) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 330 | Source::File file("", std::string{'/', '/', ' ', 0, ' '}); |
| 331 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 332 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 333 | auto list = l.Lex(); |
| 334 | ASSERT_EQ(1u, list.size()); |
| 335 | |
| 336 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 337 | EXPECT_TRUE(t.IsError()); |
| 338 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 339 | EXPECT_EQ(t.source().range.begin.column, 4u); |
| 340 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 341 | EXPECT_EQ(t.source().range.end.column, 4u); |
| 342 | EXPECT_EQ(t.to_str(), "null character found"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | TEST_F(LexerTest, Null_InBlockComment_IsError) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 346 | Source::File file("", std::string{'/', '*', ' ', 0, '*', '/'}); |
| 347 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 348 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 349 | auto list = l.Lex(); |
| 350 | ASSERT_EQ(1u, list.size()); |
| 351 | |
| 352 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 353 | EXPECT_TRUE(t.IsError()); |
| 354 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 355 | EXPECT_EQ(t.source().range.begin.column, 4u); |
| 356 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 357 | EXPECT_EQ(t.source().range.end.column, 4u); |
| 358 | EXPECT_EQ(t.to_str(), "null character found"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | TEST_F(LexerTest, Null_InIdentifier_IsError) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 362 | // Try inserting a null in an identifier. Other valid token |
| 363 | // kinds will behave similarly, so use the identifier case |
| 364 | // as a representative. |
| 365 | Source::File file("", std::string{'a', 0, 'c'}); |
| 366 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 367 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 368 | auto list = l.Lex(); |
| 369 | ASSERT_EQ(2u, list.size()); |
| 370 | |
| 371 | { |
| 372 | auto& t = list[0]; |
| 373 | EXPECT_TRUE(t.IsIdentifier()); |
| 374 | EXPECT_EQ(t.to_str(), "a"); |
| 375 | } |
| 376 | |
| 377 | { |
| 378 | auto& t = list[1]; |
| 379 | EXPECT_TRUE(t.IsError()); |
| 380 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 381 | EXPECT_EQ(t.source().range.begin.column, 2u); |
| 382 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 383 | EXPECT_EQ(t.source().range.end.column, 2u); |
| 384 | EXPECT_EQ(t.to_str(), "null character found"); |
| 385 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | struct FloatData { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 389 | const char* input; |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 390 | double result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 391 | }; |
| 392 | inline std::ostream& operator<<(std::ostream& out, FloatData data) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 393 | out << std::string(data.input); |
| 394 | return out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 395 | } |
| 396 | using FloatTest = testing::TestWithParam<FloatData>; |
| 397 | TEST_P(FloatTest, Parse) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 398 | auto params = GetParam(); |
| 399 | Source::File file("", params.input); |
| 400 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 401 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 402 | auto list = l.Lex(); |
dan sinclair | 2fe7f19 | 2023-03-07 18:20:23 +0000 | [diff] [blame] | 403 | ASSERT_EQ(2u, list.size()) << "Got: " << list[0].to_str(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 404 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 405 | { |
| 406 | auto& t = list[0]; |
| 407 | if (std::string(params.input).back() == 'f') { |
| 408 | EXPECT_TRUE(t.Is(Token::Type::kFloatLiteral_F)); |
| 409 | } else if (std::string(params.input).back() == 'h') { |
| 410 | EXPECT_TRUE(t.Is(Token::Type::kFloatLiteral_H)); |
| 411 | } else { |
| 412 | EXPECT_TRUE(t.Is(Token::Type::kFloatLiteral)); |
| 413 | } |
| 414 | EXPECT_EQ(t.to_f64(), params.result); |
| 415 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 416 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 417 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 418 | EXPECT_EQ(t.source().range.end.column, 1u + strlen(params.input)); |
| 419 | } |
| 420 | |
| 421 | { |
| 422 | auto& t = list[1]; |
| 423 | EXPECT_TRUE(t.IsEof()); |
| 424 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 425 | } |
| 426 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 427 | FloatTest, |
| 428 | testing::Values( |
| 429 | // No decimal, with 'f' suffix |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 430 | FloatData{"0f", 0.0}, |
| 431 | FloatData{"1f", 1.0}, |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 432 | // No decimal, with 'h' suffix |
| 433 | FloatData{"0h", 0.0}, |
| 434 | FloatData{"1h", 1.0}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 435 | |
| 436 | // Zero, with decimal. |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 437 | FloatData{"0.0", 0.0}, |
| 438 | FloatData{"0.", 0.0}, |
| 439 | FloatData{".0", 0.0}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 440 | // Zero, with decimal and 'f' suffix |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 441 | FloatData{"0.0f", 0.0}, |
| 442 | FloatData{"0.f", 0.0}, |
| 443 | FloatData{".0f", 0.0}, |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 444 | // Zero, with decimal and 'h' suffix |
| 445 | FloatData{"0.0h", 0.0}, |
| 446 | FloatData{"0.h", 0.0}, |
| 447 | FloatData{".0h", 0.0}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 448 | |
| 449 | // Non-zero with decimal |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 450 | FloatData{"5.7", 5.7}, |
| 451 | FloatData{"5.", 5.}, |
| 452 | FloatData{".7", .7}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 453 | // Non-zero with decimal and 'f' suffix |
Ben Clayton | e6c03a3 | 2022-05-26 19:39:05 +0000 | [diff] [blame] | 454 | FloatData{"5.7f", static_cast<double>(5.7f)}, |
| 455 | FloatData{"5.f", static_cast<double>(5.f)}, |
| 456 | FloatData{".7f", static_cast<double>(.7f)}, |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 457 | // Non-zero with decimal and 'h' suffix |
| 458 | FloatData{"5.7h", static_cast<double>(f16::Quantize(5.7f))}, |
| 459 | FloatData{"5.h", static_cast<double>(f16::Quantize(5.f))}, |
| 460 | FloatData{".7h", static_cast<double>(f16::Quantize(.7f))}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 461 | |
| 462 | // No decimal, with exponent |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 463 | FloatData{"1e5", 1e5}, |
| 464 | FloatData{"1E5", 1e5}, |
| 465 | FloatData{"1e-5", 1e-5}, |
| 466 | FloatData{"1E-5", 1e-5}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 467 | // No decimal, with exponent and 'f' suffix |
Ben Clayton | e6c03a3 | 2022-05-26 19:39:05 +0000 | [diff] [blame] | 468 | FloatData{"1e5f", static_cast<double>(1e5f)}, |
| 469 | FloatData{"1E5f", static_cast<double>(1e5f)}, |
| 470 | FloatData{"1e-5f", static_cast<double>(1e-5f)}, |
| 471 | FloatData{"1E-5f", static_cast<double>(1e-5f)}, |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 472 | // No decimal, with exponent and 'h' suffix |
| 473 | FloatData{"6e4h", static_cast<double>(f16::Quantize(6e4f))}, |
| 474 | FloatData{"6E4h", static_cast<double>(f16::Quantize(6e4f))}, |
| 475 | FloatData{"1e-5h", static_cast<double>(f16::Quantize(1e-5f))}, |
| 476 | FloatData{"1E-5h", static_cast<double>(f16::Quantize(1e-5f))}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 477 | // With decimal and exponents |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 478 | FloatData{"0.2e+12", 0.2e12}, |
| 479 | FloatData{"1.2e-5", 1.2e-5}, |
| 480 | FloatData{"2.57e23", 2.57e23}, |
| 481 | FloatData{"2.5e+0", 2.5}, |
| 482 | FloatData{"2.5e-0", 2.5}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 483 | // With decimal and exponents and 'f' suffix |
Ben Clayton | e6c03a3 | 2022-05-26 19:39:05 +0000 | [diff] [blame] | 484 | FloatData{"0.2e+12f", static_cast<double>(0.2e12f)}, |
| 485 | FloatData{"1.2e-5f", static_cast<double>(1.2e-5f)}, |
| 486 | FloatData{"2.57e23f", static_cast<double>(2.57e23f)}, |
| 487 | FloatData{"2.5e+0f", static_cast<double>(2.5f)}, |
| 488 | FloatData{"2.5e-0f", static_cast<double>(2.5f)}, |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 489 | // With decimal and exponents and 'h' suffix |
| 490 | FloatData{"0.2e+5h", static_cast<double>(f16::Quantize(0.2e5f))}, |
| 491 | FloatData{"1.2e-5h", static_cast<double>(f16::Quantize(1.2e-5f))}, |
| 492 | FloatData{"6.55e4h", static_cast<double>(f16::Quantize(6.55e4f))}, |
| 493 | FloatData{"2.5e+0h", static_cast<double>(f16::Quantize(2.5f))}, |
| 494 | FloatData{"2.5e-0h", static_cast<double>(f16::Quantize(2.5f))}, |
Ben Clayton | e6c03a3 | 2022-05-26 19:39:05 +0000 | [diff] [blame] | 495 | // Quantization |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 496 | FloatData{"3.141592653589793", 3.141592653589793}, // no quantization |
| 497 | FloatData{"3.141592653589793f", 3.1415927410125732}, // f32 quantized |
dan sinclair | 6c34e11 | 2023-03-17 15:08:06 +0000 | [diff] [blame] | 498 | FloatData{"3.141592653589793h", 3.140625}, // f16 quantized |
| 499 | |
| 500 | // https://bugs.chromium.org/p/tint/issues/detail?id=1863 |
| 501 | FloatData{"0." |
| 502 | "00000000000000000000000000000000000000000000000000" // 50 |
| 503 | "00000000000000000000000000000000000000000000000000" // 100 |
| 504 | "00000000000000000000000000000000000000000000000000" // 150 |
| 505 | "00000000000000000000000000000000000000000000000000" // 200 |
| 506 | "00000000000000000000000000000000000000000000000000" // 250 |
| 507 | "00000000000000000000000000000000000000000000000000" // 300 |
| 508 | "00000000000000000000000000000000000000000000000000" // 350 |
| 509 | "1e+0", |
| 510 | 0.0}, |
| 511 | FloatData{"0." |
| 512 | "00000000000000000000000000000000000000000000000000" // 50 |
| 513 | "00000000000000000000000000000000000000000000000000" // 100 |
| 514 | "00000000000000000000000000000000000000000000000000" // 150 |
| 515 | "00000000000000000000000000000000000000000000000000" // 200 |
| 516 | "00000000000000000000000000000000000000000000000000" // 250 |
| 517 | "00000000000000000000000000000000000000000000000000" // 300 |
| 518 | "00000000000000000000000000000000000000000000000000" // 350 |
| 519 | "1e+10", |
| 520 | 0.0}, |
| 521 | FloatData{"0." |
| 522 | "00000000000000000000000000000000000000000000000000" // 50 |
| 523 | "00000000000000000000000000000000000000000000000000" // 100 |
| 524 | "00000000000000000000000000000000000000000000000000" // 150 |
| 525 | "00000000000000000000000000000000000000000000000000" // 200 |
| 526 | "00000000000000000000000000000000000000000000000000" // 250 |
| 527 | "00000000000000000000000000000000000000000000000000" // 300 |
| 528 | "00000000000000000000000000000000000000000000000000" // 350 |
| 529 | "1e+300", |
| 530 | 1e-51}, |
| 531 | FloatData{"0." |
| 532 | "00000000000000000000000000000000000000000000000000" // 50 |
| 533 | "00000000000000000000000000000000000000000000000000" // 100 |
| 534 | "00000000000000000000000000000000000000000000000000" // 150 |
| 535 | "00000000000000000000000000000000000000000000000000" // 200 |
| 536 | "00000000000000000000000000000000000000000000000000" // 250 |
| 537 | "00000000000000000000000000000000000000000000000000" // 300 |
| 538 | "00000000000000000000000000000000000000000000000000" // 350 |
| 539 | "1", |
| 540 | 0.0}, |
| 541 | |
| 542 | FloatData{"1" |
| 543 | "00000000000000000000000000000000000000000000000000" // 50 |
| 544 | "00000000000000000000000000000000000000000000000000" // 100 |
| 545 | ".0e-350", |
| 546 | 1e-250} |
| 547 | |
Ben Clayton | e6c03a3 | 2022-05-26 19:39:05 +0000 | [diff] [blame] | 548 | )); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 549 | |
| 550 | using FloatTest_Invalid = testing::TestWithParam<const char*>; |
| 551 | TEST_P(FloatTest_Invalid, Handles) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 552 | Source::File file("", GetParam()); |
| 553 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 554 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 555 | auto list = l.Lex(); |
| 556 | ASSERT_FALSE(list.empty()); |
| 557 | |
| 558 | auto& t = list[0]; |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 559 | EXPECT_FALSE(t.Is(Token::Type::kFloatLiteral) || t.Is(Token::Type::kFloatLiteral_F) || |
| 560 | t.Is(Token::Type::kFloatLiteral_H)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 561 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 562 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 563 | FloatTest_Invalid, |
| 564 | testing::Values(".", |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 565 | // Need a mantissa digit |
| 566 | ".e5", |
| 567 | ".E5", |
| 568 | // Need exponent digits |
| 569 | ".e", |
| 570 | ".e+", |
| 571 | ".e-", |
| 572 | ".E", |
| 573 | ".e+", |
| 574 | ".e-", |
| 575 | // Overflow |
Ben Clayton | e6c03a3 | 2022-05-26 19:39:05 +0000 | [diff] [blame] | 576 | "2.5e+256f", |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 577 | "6.5520e+4h", |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 578 | // Decimal exponent must immediately |
| 579 | // follow the 'e'. |
| 580 | "2.5e 12", |
| 581 | "2.5e +12", |
| 582 | "2.5e -12", |
| 583 | "2.5e+ 123", |
| 584 | "2.5e- 123", |
| 585 | "2.5E 12", |
| 586 | "2.5E +12", |
| 587 | "2.5E -12", |
| 588 | "2.5E+ 123", |
| 589 | "2.5E- 123")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 590 | |
| 591 | using AsciiIdentifierTest = testing::TestWithParam<const char*>; |
| 592 | TEST_P(AsciiIdentifierTest, Parse) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 593 | Source::File file("", GetParam()); |
| 594 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 595 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 596 | auto list = l.Lex(); |
| 597 | ASSERT_EQ(2u, list.size()); |
| 598 | |
| 599 | { |
| 600 | auto& t = list[0]; |
| 601 | EXPECT_TRUE(t.IsIdentifier()); |
| 602 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 603 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 604 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 605 | EXPECT_EQ(t.source().range.end.column, 1u + strlen(GetParam())); |
| 606 | EXPECT_EQ(t.to_str(), GetParam()); |
| 607 | } |
| 608 | |
| 609 | { |
| 610 | auto& t = list[1]; |
| 611 | EXPECT_TRUE(t.IsEof()); |
| 612 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 613 | } |
| 614 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 615 | AsciiIdentifierTest, |
| 616 | testing::Values("a", |
| 617 | "test", |
| 618 | "test01", |
| 619 | "test_", |
| 620 | "_test", |
| 621 | "test_01", |
| 622 | "ALLCAPS", |
| 623 | "MiXeD_CaSe", |
| 624 | "abcdefghijklmnopqrstuvwxyz", |
| 625 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 626 | "alldigits_0123456789")); |
| 627 | |
| 628 | struct UnicodeCase { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 629 | const char* utf8; |
| 630 | size_t count; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 631 | }; |
| 632 | |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 633 | using ValidUnicodeIdentifierTest = testing::TestWithParam<UnicodeCase>; |
| 634 | TEST_P(ValidUnicodeIdentifierTest, Parse) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 635 | Source::File file("", GetParam().utf8); |
| 636 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 637 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 638 | auto list = l.Lex(); |
| 639 | ASSERT_EQ(2u, list.size()); |
| 640 | |
| 641 | { |
| 642 | auto& t = list[0]; |
| 643 | EXPECT_TRUE(t.IsIdentifier()); |
| 644 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 645 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 646 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 647 | EXPECT_EQ(t.source().range.end.column, 1u + GetParam().count); |
| 648 | EXPECT_EQ(t.to_str(), GetParam().utf8); |
| 649 | } |
| 650 | |
| 651 | { |
| 652 | auto& t = list[1]; |
| 653 | EXPECT_TRUE(t.IsEof()); |
| 654 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 655 | } |
| 656 | INSTANTIATE_TEST_SUITE_P( |
| 657 | LexerTest, |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 658 | ValidUnicodeIdentifierTest, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 659 | testing::ValuesIn({ |
| 660 | UnicodeCase{// "𝐢𝐝𝐞𝐧𝐭𝐢𝐟𝐢𝐞𝐫" |
| 661 | "\xf0\x9d\x90\xa2\xf0\x9d\x90\x9d\xf0\x9d\x90\x9e\xf0\x9d" |
| 662 | "\x90\xa7\xf0\x9d\x90\xad\xf0\x9d\x90\xa2\xf0\x9d\x90\x9f" |
| 663 | "\xf0\x9d\x90\xa2\xf0\x9d\x90\x9e\xf0\x9d\x90\xab", |
| 664 | 40}, |
| 665 | UnicodeCase{// "𝑖𝑑𝑒𝑛𝑡𝑖𝑓𝑖𝑒𝑟" |
| 666 | "\xf0\x9d\x91\x96\xf0\x9d\x91\x91\xf0\x9d\x91\x92\xf0\x9d" |
| 667 | "\x91\x9b\xf0\x9d\x91\xa1\xf0\x9d\x91\x96\xf0\x9d\x91\x93" |
| 668 | "\xf0\x9d\x91\x96\xf0\x9d\x91\x92\xf0\x9d\x91\x9f", |
| 669 | 40}, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 670 | UnicodeCase{// "identifier" |
| 671 | "\xef\xbd\x89\xef\xbd\x84\xef\xbd\x85\xef\xbd\x8e\xef\xbd\x94\xef" |
| 672 | "\xbd\x89\xef\xbd\x86\xef\xbd\x89\xef\xbd\x85\xef\xbd\x92", |
| 673 | 30}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 674 | UnicodeCase{// "𝕚𝕕𝕖𝕟𝕥𝕚𝕗𝕚𝕖𝕣𝟙𝟚𝟛" |
| 675 | "\xf0\x9d\x95\x9a\xf0\x9d\x95\x95\xf0\x9d\x95\x96\xf0\x9d" |
| 676 | "\x95\x9f\xf0\x9d\x95\xa5\xf0\x9d\x95\x9a\xf0\x9d\x95\x97" |
| 677 | "\xf0\x9d\x95\x9a\xf0\x9d\x95\x96\xf0\x9d\x95\xa3\xf0\x9d" |
| 678 | "\x9f\x99\xf0\x9d\x9f\x9a\xf0\x9d\x9f\x9b", |
| 679 | 52}, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 680 | UnicodeCase{// "𝖎𝖉𝖊𝖓𝖙𝖎𝖋𝖎𝖊𝖗123" |
| 681 | "\xf0\x9d\x96\x8e\xf0\x9d\x96\x89\xf0\x9d\x96\x8a\xf0\x9d\x96\x93" |
| 682 | "\xf0\x9d\x96\x99\xf0\x9d\x96\x8e\xf0\x9d\x96\x8b\xf0\x9d\x96\x8e" |
| 683 | "\xf0\x9d\x96\x8a\xf0\x9d\x96\x97\x31\x32\x33", |
| 684 | 43}, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 685 | })); |
| 686 | |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 687 | using InvalidUnicodeIdentifierTest = testing::TestWithParam<const char*>; |
| 688 | TEST_P(InvalidUnicodeIdentifierTest, Parse) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 689 | Source::File file("", GetParam()); |
| 690 | Lexer l(&file); |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 691 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 692 | auto list = l.Lex(); |
| 693 | ASSERT_FALSE(list.empty()); |
| 694 | |
| 695 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 696 | EXPECT_TRUE(t.IsError()); |
| 697 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 698 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 699 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 700 | EXPECT_EQ(t.source().range.end.column, 1u); |
| 701 | EXPECT_EQ(t.to_str(), "invalid UTF-8"); |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 702 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 703 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 704 | InvalidUnicodeIdentifierTest, |
| 705 | testing::ValuesIn({ |
| 706 | "\x80\x80\x80\x80", // 10000000 |
| 707 | "\x81\x80\x80\x80", // 10000001 |
| 708 | "\x8f\x80\x80\x80", // 10001111 |
| 709 | "\x90\x80\x80\x80", // 10010000 |
| 710 | "\x91\x80\x80\x80", // 10010001 |
| 711 | "\x9f\x80\x80\x80", // 10011111 |
| 712 | "\xa0\x80\x80\x80", // 10100000 |
| 713 | "\xa1\x80\x80\x80", // 10100001 |
| 714 | "\xaf\x80\x80\x80", // 10101111 |
| 715 | "\xb0\x80\x80\x80", // 10110000 |
| 716 | "\xb1\x80\x80\x80", // 10110001 |
| 717 | "\xbf\x80\x80\x80", // 10111111 |
| 718 | "\xc0\x80\x80\x80", // 11000000 |
| 719 | "\xc1\x80\x80\x80", // 11000001 |
| 720 | "\xf5\x80\x80\x80", // 11110101 |
| 721 | "\xf6\x80\x80\x80", // 11110110 |
| 722 | "\xf7\x80\x80\x80", // 11110111 |
| 723 | "\xf8\x80\x80\x80", // 11111000 |
| 724 | "\xfe\x80\x80\x80", // 11111110 |
| 725 | "\xff\x80\x80\x80", // 11111111 |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 726 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 727 | "\xd0", // 2-bytes, missing second byte |
| 728 | "\xe8\x8f", // 3-bytes, missing third byte |
| 729 | "\xf4\x8f\x8f", // 4-bytes, missing fourth byte |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 730 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 731 | "\xd0\x7f", // 2-bytes, second byte MSB unset |
| 732 | "\xe8\x7f\x8f", // 3-bytes, second byte MSB unset |
| 733 | "\xe8\x8f\x7f", // 3-bytes, third byte MSB unset |
| 734 | "\xf4\x7f\x8f\x8f", // 4-bytes, second byte MSB unset |
| 735 | "\xf4\x8f\x7f\x8f", // 4-bytes, third byte MSB unset |
| 736 | "\xf4\x8f\x8f\x7f", // 4-bytes, fourth byte MSB unset |
| 737 | })); |
Ben Clayton | 64b7754 | 2022-03-15 16:18:03 +0000 | [diff] [blame] | 738 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 739 | TEST_F(LexerTest, IdentifierTest_SingleUnderscoreDoesNotMatch) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 740 | Source::File file("", "_"); |
| 741 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 742 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 743 | auto list = l.Lex(); |
| 744 | ASSERT_FALSE(list.empty()); |
| 745 | |
| 746 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 747 | EXPECT_FALSE(t.IsIdentifier()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | TEST_F(LexerTest, IdentifierTest_DoesNotStartWithDoubleUnderscore) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 751 | Source::File file("", "__test"); |
| 752 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 753 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 754 | auto list = l.Lex(); |
| 755 | ASSERT_FALSE(list.empty()); |
| 756 | |
| 757 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 758 | EXPECT_FALSE(t.IsIdentifier()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | TEST_F(LexerTest, IdentifierTest_DoesNotStartWithNumber) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 762 | Source::File file("", "01test"); |
| 763 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 764 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 765 | auto list = l.Lex(); |
| 766 | EXPECT_FALSE(list.empty()); |
| 767 | |
| 768 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 769 | EXPECT_FALSE(t.IsIdentifier()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 772 | //////////////////////////////////////////////////////////////////////////////// |
| 773 | // ParseIntegerTest |
| 774 | //////////////////////////////////////////////////////////////////////////////// |
| 775 | struct ParseIntegerCase { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 776 | const char* input; |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 777 | int64_t result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 778 | }; |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 779 | |
| 780 | inline std::ostream& operator<<(std::ostream& out, ParseIntegerCase data) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 781 | out << std::string(data.input); |
| 782 | return out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 785 | using ParseIntegerTest = testing::TestWithParam<std::tuple<char, ParseIntegerCase>>; |
| 786 | TEST_P(ParseIntegerTest, Parse) { |
| 787 | auto suffix = std::get<0>(GetParam()); |
| 788 | auto params = std::get<1>(GetParam()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 789 | Source::File file("", params.input); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 790 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 791 | Lexer l(&file); |
| 792 | |
| 793 | auto list = l.Lex(); |
| 794 | ASSERT_FALSE(list.empty()); |
| 795 | |
| 796 | auto& t = list[0]; |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 797 | switch (suffix) { |
| 798 | case 'i': |
| 799 | EXPECT_TRUE(t.Is(Token::Type::kIntLiteral_I)); |
| 800 | break; |
| 801 | case 'u': |
| 802 | EXPECT_TRUE(t.Is(Token::Type::kIntLiteral_U)); |
| 803 | break; |
| 804 | case 0: |
| 805 | EXPECT_TRUE(t.Is(Token::Type::kIntLiteral)); |
| 806 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 807 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 808 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 809 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 810 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 811 | EXPECT_EQ(t.source().range.end.column, 1u + strlen(params.input)); |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 812 | ASSERT_FALSE(t.IsError()) << t.to_str(); |
Ben Clayton | f693488 | 2022-05-04 21:01:12 +0000 | [diff] [blame] | 813 | EXPECT_EQ(t.to_i64(), params.result); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 816 | INSTANTIATE_TEST_SUITE_P(Dec_AInt, |
| 817 | ParseIntegerTest, |
| 818 | testing::Combine(testing::Values('\0'), // No suffix |
| 819 | testing::ValuesIn(std::vector<ParseIntegerCase>{ |
| 820 | {"0", 0}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 821 | {"2", 2}, |
| 822 | {"123", 123}, |
| 823 | {"2147483647", 2147483647}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 824 | }))); |
| 825 | |
| 826 | INSTANTIATE_TEST_SUITE_P(Dec_u32, |
| 827 | ParseIntegerTest, |
| 828 | testing::Combine(testing::Values('u'), // Suffix |
| 829 | testing::ValuesIn(std::vector<ParseIntegerCase>{ |
| 830 | {"0u", 0}, |
| 831 | {"123u", 123}, |
| 832 | {"4294967295u", 4294967295ll}, |
| 833 | }))); |
| 834 | |
| 835 | INSTANTIATE_TEST_SUITE_P(Dec_i32, |
| 836 | ParseIntegerTest, |
| 837 | testing::Combine(testing::Values('i'), // Suffix |
| 838 | testing::ValuesIn(std::vector<ParseIntegerCase>{ |
| 839 | {"0i", 0u}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 840 | {"123i", 123}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 841 | {"2147483647i", 2147483647}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 842 | }))); |
| 843 | |
| 844 | INSTANTIATE_TEST_SUITE_P(Hex_AInt, |
| 845 | ParseIntegerTest, |
| 846 | testing::Combine(testing::Values('\0'), // No suffix |
| 847 | testing::ValuesIn(std::vector<ParseIntegerCase>{ |
| 848 | {"0x0", 0}, |
| 849 | {"0X0", 0}, |
| 850 | {"0x42", 66}, |
| 851 | {"0X42", 66}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 852 | {"0xeF1Abc9", 0xeF1Abc9}, |
| 853 | {"0XeF1Abc9", 0xeF1Abc9}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 854 | {"0x80000000", 0x80000000}, |
| 855 | {"0X80000000", 0X80000000}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 856 | {"0x7FFFFFFF", 0x7fffffff}, |
| 857 | {"0X7FFFFFFF", 0x7fffffff}, |
| 858 | {"0x7fffffff", 0x7fffffff}, |
| 859 | {"0x7fffffff", 0x7fffffff}, |
| 860 | {"0x7FfFfFfF", 0x7fffffff}, |
| 861 | {"0X7FfFfFfF", 0x7fffffff}, |
| 862 | {"0x7fffffffffffffff", 0x7fffffffffffffffll}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 863 | }))); |
| 864 | |
| 865 | INSTANTIATE_TEST_SUITE_P(Hex_u32, |
| 866 | ParseIntegerTest, |
| 867 | testing::Combine(testing::Values('u'), // Suffix |
| 868 | testing::ValuesIn(std::vector<ParseIntegerCase>{ |
| 869 | {"0x0u", 0}, |
| 870 | {"0x42u", 66}, |
| 871 | {"0xeF1Abc9u", 250719177}, |
| 872 | {"0xFFFFFFFFu", 0xffffffff}, |
| 873 | {"0XFFFFFFFFu", 0xffffffff}, |
| 874 | {"0xffffffffu", 0xffffffff}, |
| 875 | {"0Xffffffffu", 0xffffffff}, |
| 876 | {"0xfFfFfFfFu", 0xffffffff}, |
| 877 | {"0XfFfFfFfFu", 0xffffffff}, |
| 878 | }))); |
| 879 | |
| 880 | INSTANTIATE_TEST_SUITE_P(Hex_i32, |
| 881 | ParseIntegerTest, |
| 882 | testing::Combine(testing::Values('i'), // Suffix |
| 883 | testing::ValuesIn(std::vector<ParseIntegerCase>{ |
| 884 | {"0x0i", 0}, |
| 885 | {"0x42i", 66}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 886 | {"0xeF1Abc9i", 250719177}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 887 | {"0x7FFFFFFFi", 0x7fffffff}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 888 | {"0X7FFFFFFFi", 0x7fffffff}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 889 | {"0x7fffffffi", 0x7fffffff}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 890 | {"0X7fffffffi", 0x7fffffff}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 891 | {"0x7FfFfFfFi", 0x7fffffff}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 892 | {"0X7FfFfFfFi", 0x7fffffff}, |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 893 | }))); |
| 894 | //////////////////////////////////////////////////////////////////////////////// |
| 895 | // ParseIntegerTest_CannotBeRepresented |
| 896 | //////////////////////////////////////////////////////////////////////////////// |
| 897 | using ParseIntegerTest_CannotBeRepresented = |
| 898 | testing::TestWithParam<std::tuple<const char*, const char*>>; |
| 899 | TEST_P(ParseIntegerTest_CannotBeRepresented, Parse) { |
| 900 | auto type = std::get<0>(GetParam()); |
| 901 | auto source = std::get<1>(GetParam()); |
| 902 | Source::File file("", source); |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 903 | |
| 904 | Lexer l(&file); |
| 905 | auto list = l.Lex(); |
| 906 | ASSERT_FALSE(list.empty()); |
| 907 | |
| 908 | auto& t = list[0]; |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 909 | EXPECT_TRUE(t.Is(Token::Type::kError)); |
| 910 | auto expect = "value cannot be represented as '" + std::string(type) + "'"; |
| 911 | EXPECT_EQ(t.to_str(), expect); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 912 | } |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 913 | INSTANTIATE_TEST_SUITE_P(AbstractInt, |
| 914 | ParseIntegerTest_CannotBeRepresented, |
| 915 | testing::Combine(testing::Values("abstract-int"), |
| 916 | testing::Values("9223372036854775808", |
| 917 | "0xFFFFFFFFFFFFFFFF", |
| 918 | "0xffffffffffffffff", |
| 919 | "0x8000000000000000"))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 920 | |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 921 | INSTANTIATE_TEST_SUITE_P(i32, |
| 922 | ParseIntegerTest_CannotBeRepresented, |
| 923 | testing::Combine(testing::Values("i32"), // type |
| 924 | testing::Values("2147483648i"))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 925 | |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 926 | INSTANTIATE_TEST_SUITE_P(u32, |
| 927 | ParseIntegerTest_CannotBeRepresented, |
dan sinclair | b3b027d | 2022-10-26 15:20:47 +0000 | [diff] [blame] | 928 | testing::Combine(testing::Values("u32"), // type |
| 929 | testing::Values("4294967296u"))); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 930 | |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 931 | //////////////////////////////////////////////////////////////////////////////// |
| 932 | // ParseIntegerTest_LeadingZeros |
| 933 | //////////////////////////////////////////////////////////////////////////////// |
| 934 | using ParseIntegerTest_LeadingZeros = testing::TestWithParam<const char*>; |
| 935 | TEST_P(ParseIntegerTest_LeadingZeros, Parse) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 936 | Source::File file("", GetParam()); |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 937 | |
| 938 | Lexer l(&file); |
| 939 | auto list = l.Lex(); |
| 940 | ASSERT_FALSE(list.empty()); |
| 941 | |
| 942 | auto& t = list[0]; |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 943 | EXPECT_TRUE(t.Is(Token::Type::kError)); |
| 944 | EXPECT_EQ(t.to_str(), "integer literal cannot have leading 0s"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 945 | } |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 946 | |
| 947 | INSTANTIATE_TEST_SUITE_P(LeadingZero, |
| 948 | ParseIntegerTest_LeadingZeros, |
dan sinclair | b3b027d | 2022-10-26 15:20:47 +0000 | [diff] [blame] | 949 | testing::Values("01234", "0000", "00u")); |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 950 | |
| 951 | //////////////////////////////////////////////////////////////////////////////// |
| 952 | // ParseIntegerTest_NoSignificantDigits |
| 953 | //////////////////////////////////////////////////////////////////////////////// |
| 954 | using ParseIntegerTest_NoSignificantDigits = testing::TestWithParam<const char*>; |
| 955 | TEST_P(ParseIntegerTest_NoSignificantDigits, Parse) { |
| 956 | Source::File file("", GetParam()); |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 957 | |
| 958 | Lexer l(&file); |
| 959 | auto list = l.Lex(); |
| 960 | ASSERT_FALSE(list.empty()); |
| 961 | |
| 962 | auto& t = list[0]; |
Ben Clayton | 8b09bc9 | 2022-05-30 17:54:38 +0000 | [diff] [blame] | 963 | EXPECT_TRUE(t.Is(Token::Type::kError)); |
| 964 | EXPECT_EQ(t.to_str(), "integer or float hex literal has no significant digits"); |
| 965 | } |
| 966 | |
| 967 | INSTANTIATE_TEST_SUITE_P(LeadingZero, |
| 968 | ParseIntegerTest_NoSignificantDigits, |
dan sinclair | b3b027d | 2022-10-26 15:20:47 +0000 | [diff] [blame] | 969 | testing::Values("0x", "0X", "0xu", "0Xu", "0xi", "0Xi")); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 970 | |
| 971 | struct TokenData { |
| 972 | const char* input; |
| 973 | Token::Type type; |
| 974 | }; |
| 975 | inline std::ostream& operator<<(std::ostream& out, TokenData data) { |
| 976 | out << std::string(data.input); |
| 977 | return out; |
| 978 | } |
| 979 | using PunctuationTest = testing::TestWithParam<TokenData>; |
| 980 | TEST_P(PunctuationTest, Parses) { |
| 981 | auto params = GetParam(); |
| 982 | Source::File file("", params.input); |
| 983 | Lexer l(&file); |
| 984 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 985 | auto list = l.Lex(); |
| 986 | ASSERT_GE(list.size(), 2u); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 987 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 988 | { |
| 989 | auto& t = list[0]; |
| 990 | EXPECT_TRUE(t.Is(params.type)); |
| 991 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 992 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 993 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 994 | EXPECT_EQ(t.source().range.end.column, 1u + strlen(params.input)); |
| 995 | } |
| 996 | |
| 997 | { |
| 998 | auto& t = list[1]; |
| 999 | EXPECT_EQ(t.source().range.begin.column, 1 + std::string(params.input).size()); |
| 1000 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1001 | } |
| 1002 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 1003 | PunctuationTest, |
| 1004 | testing::Values(TokenData{"&", Token::Type::kAnd}, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1005 | TokenData{"->", Token::Type::kArrow}, |
| 1006 | TokenData{"@", Token::Type::kAttr}, |
| 1007 | TokenData{"/", Token::Type::kForwardSlash}, |
| 1008 | TokenData{"!", Token::Type::kBang}, |
| 1009 | TokenData{"[", Token::Type::kBracketLeft}, |
| 1010 | TokenData{"]", Token::Type::kBracketRight}, |
| 1011 | TokenData{"{", Token::Type::kBraceLeft}, |
| 1012 | TokenData{"}", Token::Type::kBraceRight}, |
| 1013 | TokenData{":", Token::Type::kColon}, |
| 1014 | TokenData{",", Token::Type::kComma}, |
| 1015 | TokenData{"=", Token::Type::kEqual}, |
| 1016 | TokenData{"==", Token::Type::kEqualEqual}, |
| 1017 | TokenData{">", Token::Type::kGreaterThan}, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1018 | TokenData{"<", Token::Type::kLessThan}, |
| 1019 | TokenData{"<=", Token::Type::kLessThanEqual}, |
| 1020 | TokenData{"<<", Token::Type::kShiftLeft}, |
| 1021 | TokenData{"%", Token::Type::kMod}, |
| 1022 | TokenData{"!=", Token::Type::kNotEqual}, |
| 1023 | TokenData{"-", Token::Type::kMinus}, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1024 | TokenData{".", Token::Type::kPeriod}, |
| 1025 | TokenData{"+", Token::Type::kPlus}, |
| 1026 | TokenData{"++", Token::Type::kPlusPlus}, |
| 1027 | TokenData{"|", Token::Type::kOr}, |
| 1028 | TokenData{"||", Token::Type::kOrOr}, |
| 1029 | TokenData{"(", Token::Type::kParenLeft}, |
| 1030 | TokenData{")", Token::Type::kParenRight}, |
| 1031 | TokenData{";", Token::Type::kSemicolon}, |
| 1032 | TokenData{"*", Token::Type::kStar}, |
| 1033 | TokenData{"~", Token::Type::kTilde}, |
| 1034 | TokenData{"_", Token::Type::kUnderscore}, |
| 1035 | TokenData{"^", Token::Type::kXor}, |
| 1036 | TokenData{"+=", Token::Type::kPlusEqual}, |
| 1037 | TokenData{"-=", Token::Type::kMinusEqual}, |
| 1038 | TokenData{"*=", Token::Type::kTimesEqual}, |
| 1039 | TokenData{"/=", Token::Type::kDivisionEqual}, |
| 1040 | TokenData{"%=", Token::Type::kModuloEqual}, |
| 1041 | TokenData{"&=", Token::Type::kAndEqual}, |
| 1042 | TokenData{"|=", Token::Type::kOrEqual}, |
dan sinclair | 73778d3b | 2022-08-02 18:18:05 +0000 | [diff] [blame] | 1043 | TokenData{"^=", Token::Type::kXorEqual}, |
dan sinclair | 73778d3b | 2022-08-02 18:18:05 +0000 | [diff] [blame] | 1044 | TokenData{"<<=", Token::Type::kShiftLeftEqual})); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1045 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 1046 | using SplittablePunctuationTest = testing::TestWithParam<TokenData>; |
| 1047 | TEST_P(SplittablePunctuationTest, Parses) { |
| 1048 | auto params = GetParam(); |
| 1049 | Source::File file("", params.input); |
| 1050 | Lexer l(&file); |
| 1051 | |
| 1052 | auto list = l.Lex(); |
| 1053 | ASSERT_GE(list.size(), 3u); |
| 1054 | |
| 1055 | { |
| 1056 | auto& t = list[0]; |
| 1057 | EXPECT_TRUE(t.Is(params.type)); |
| 1058 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 1059 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 1060 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 1061 | EXPECT_EQ(t.source().range.end.column, 1u + strlen(params.input)); |
| 1062 | } |
| 1063 | |
Ben Clayton | 6fc130b | 2023-01-23 18:32:41 +0000 | [diff] [blame] | 1064 | const size_t num_placeholders = list[0].NumPlaceholders(); |
| 1065 | EXPECT_GT(num_placeholders, 0u); |
| 1066 | ASSERT_EQ(list.size(), 2u + num_placeholders); |
| 1067 | |
| 1068 | for (size_t i = 0; i < num_placeholders; i++) { |
| 1069 | auto& t = list[1 + i]; |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 1070 | EXPECT_TRUE(t.Is(Token::Type::kPlaceholder)); |
| 1071 | EXPECT_EQ(t.source().range.begin.line, 1u); |
Ben Clayton | 6fc130b | 2023-01-23 18:32:41 +0000 | [diff] [blame] | 1072 | EXPECT_EQ(t.source().range.begin.column, 2u + i); |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 1073 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 1074 | EXPECT_EQ(t.source().range.end.column, 1u + strlen(params.input)); |
| 1075 | } |
| 1076 | |
| 1077 | { |
Ben Clayton | 6fc130b | 2023-01-23 18:32:41 +0000 | [diff] [blame] | 1078 | auto& t = list.back(); |
| 1079 | EXPECT_TRUE(t.Is(Token::Type::kEOF)); |
| 1080 | EXPECT_EQ(t.source().range.begin.column, 2u + num_placeholders); |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 1084 | SplittablePunctuationTest, |
dan sinclair | 6c8dc15 | 2022-08-17 16:30:30 +0000 | [diff] [blame] | 1085 | testing::Values(TokenData{"&&", Token::Type::kAndAnd}, |
| 1086 | TokenData{">=", Token::Type::kGreaterThanEqual}, |
dan sinclair | 2788bec | 2022-08-19 21:26:01 +0000 | [diff] [blame] | 1087 | TokenData{"--", Token::Type::kMinusMinus}, |
Ben Clayton | 6fc130b | 2023-01-23 18:32:41 +0000 | [diff] [blame] | 1088 | TokenData{">>", Token::Type::kShiftRight}, |
| 1089 | TokenData{">>=", Token::Type::kShiftRightEqual})); |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 1090 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1091 | using KeywordTest = testing::TestWithParam<TokenData>; |
| 1092 | TEST_P(KeywordTest, Parses) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1093 | auto params = GetParam(); |
| 1094 | Source::File file("", params.input); |
| 1095 | Lexer l(&file); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1096 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 1097 | auto list = l.Lex(); |
| 1098 | ASSERT_GE(list.size(), 2u); |
| 1099 | |
| 1100 | auto& t = list[0]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1101 | EXPECT_TRUE(t.Is(params.type)) << params.input; |
| 1102 | EXPECT_EQ(t.source().range.begin.line, 1u); |
| 1103 | EXPECT_EQ(t.source().range.begin.column, 1u); |
| 1104 | EXPECT_EQ(t.source().range.end.line, 1u); |
| 1105 | EXPECT_EQ(t.source().range.end.column, 1u + strlen(params.input)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1106 | |
dan sinclair | 0cbf5a9 | 2022-07-25 16:43:08 +0000 | [diff] [blame] | 1107 | EXPECT_EQ(list[1].source().range.begin.column, 1 + std::string(params.input).size()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1108 | } |
Ben Clayton | 643f2aa | 2023-02-18 08:36:01 +0000 | [diff] [blame] | 1109 | INSTANTIATE_TEST_SUITE_P(LexerTest, |
| 1110 | KeywordTest, |
| 1111 | testing::Values(TokenData{"alias", Token::Type::kAlias}, |
| 1112 | TokenData{"bitcast", Token::Type::kBitcast}, |
| 1113 | TokenData{"break", Token::Type::kBreak}, |
| 1114 | TokenData{"case", Token::Type::kCase}, |
| 1115 | TokenData{"const", Token::Type::kConst}, |
| 1116 | TokenData{"const_assert", Token::Type::kConstAssert}, |
| 1117 | TokenData{"continue", Token::Type::kContinue}, |
| 1118 | TokenData{"continuing", Token::Type::kContinuing}, |
| 1119 | TokenData{"default", Token::Type::kDefault}, |
| 1120 | TokenData{"diagnostic", Token::Type::kDiagnostic}, |
| 1121 | TokenData{"discard", Token::Type::kDiscard}, |
| 1122 | TokenData{"else", Token::Type::kElse}, |
| 1123 | TokenData{"enable", Token::Type::kEnable}, |
| 1124 | TokenData{"fallthrough", Token::Type::kFallthrough}, |
| 1125 | TokenData{"false", Token::Type::kFalse}, |
| 1126 | TokenData{"fn", Token::Type::kFn}, |
| 1127 | TokenData{"for", Token::Type::kFor}, |
| 1128 | TokenData{"if", Token::Type::kIf}, |
| 1129 | TokenData{"let", Token::Type::kLet}, |
| 1130 | TokenData{"loop", Token::Type::kLoop}, |
| 1131 | TokenData{"override", Token::Type::kOverride}, |
| 1132 | TokenData{"return", Token::Type::kReturn}, |
dan sinclair | 187e0d5 | 2023-02-22 20:57:28 +0000 | [diff] [blame] | 1133 | TokenData{"requires", Token::Type::kRequires}, |
Ben Clayton | 643f2aa | 2023-02-18 08:36:01 +0000 | [diff] [blame] | 1134 | TokenData{"struct", Token::Type::kStruct}, |
| 1135 | TokenData{"switch", Token::Type::kSwitch}, |
| 1136 | TokenData{"true", Token::Type::kTrue}, |
Ben Clayton | 643f2aa | 2023-02-18 08:36:01 +0000 | [diff] [blame] | 1137 | TokenData{"var", Token::Type::kVar}, |
| 1138 | TokenData{"while", Token::Type::kWhile})); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1139 | |
| 1140 | } // namespace |
dan sinclair | 6119045 | 2023-07-26 20:47:54 +0000 | [diff] [blame] | 1141 | } // namespace tint::wgsl::reader |