| 1 | use super::lexer::{ |
| 2 | CharsAndLine, Lexer, LexerItem, ReplaceComments, SkipBackslashNewline, Token, TokenValue, |
| 3 | COMMENT_SENTINEL_VALUE, |
| 4 | }; |
| 5 | use super::token::{Float, Integer, Location, PreprocessorError, Punct}; |
| 6 | use std::ops::Range; |
| 7 | |
| 8 | fn c(c: char, line: u32) -> Option<(char, u32)> { |
| 9 | Some((c, line)) |
| 10 | } |
| 11 | |
| 12 | fn l(line: u32, pos: Range<u32>) -> Location { |
| 13 | Location { |
| 14 | line, |
| 15 | start: pos.start, |
| 16 | end: pos.end, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn unwrap_token(item: Option<LexerItem>) -> Token { |
| 21 | item.unwrap().unwrap() |
| 22 | } |
| 23 | |
| 24 | fn unwrap_token_value(item: Option<LexerItem>) -> TokenValue { |
| 25 | unwrap_token(item).value |
| 26 | } |
| 27 | |
| 28 | fn unwrap_error(item: Option<LexerItem>) -> PreprocessorError { |
| 29 | item.unwrap().unwrap_err().0 |
| 30 | } |
| 31 | |
| 32 | fn expect_lexer_end(lexer: &mut Lexer) { |
| 33 | assert_eq!(unwrap_token_value(lexer.next()), TokenValue::NewLine); |
| 34 | assert_eq!(lexer.next(), None); |
| 35 | } |
| 36 | |
| 37 | impl From<i32> for TokenValue { |
| 38 | fn from(value: i32) -> Self { |
| 39 | TokenValue::Integer(Integer { |
| 40 | value: value as u64, |
| 41 | signed: true, |
| 42 | width: 32, |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl From<u32> for TokenValue { |
| 48 | fn from(value: u32) -> Self { |
| 49 | TokenValue::Integer(Integer { |
| 50 | value: value as u64, |
| 51 | signed: false, |
| 52 | width: 32, |
| 53 | }) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl From<f32> for TokenValue { |
| 58 | fn from(value: f32) -> Self { |
| 59 | TokenValue::Float(Float { value, width: 32 }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn chars_and_location() { |
| 65 | // Test handling of characters in a line. |
| 66 | let mut it = CharsAndLine::new("abc"); |
| 67 | assert_eq!(it.next(), c('a', 1)); |
| 68 | assert_eq!(it.next(), c('b', 1)); |
| 69 | assert_eq!(it.next(), c('c', 1)); |
| 70 | assert_eq!(it.next(), None); |
| 71 | |
| 72 | // Test handling of \n in the regular case. |
| 73 | let mut it = CharsAndLine::new("a\nb"); |
| 74 | assert_eq!(it.next(), c('a', 1)); |
| 75 | assert_eq!(it.next(), c('\n', 1)); |
| 76 | assert_eq!(it.next(), c('b', 2)); |
| 77 | assert_eq!(it.next(), None); |
| 78 | |
| 79 | // Test handling of \r in the regular case. |
| 80 | let mut it = CharsAndLine::new("a\rb"); |
| 81 | assert_eq!(it.next(), c('a', 1)); |
| 82 | assert_eq!(it.next(), c('\n', 1)); |
| 83 | assert_eq!(it.next(), c('b', 2)); |
| 84 | assert_eq!(it.next(), None); |
| 85 | |
| 86 | // Test handling of \n\r. |
| 87 | let mut it = CharsAndLine::new("a\n\rb"); |
| 88 | assert_eq!(it.next(), c('a', 1)); |
| 89 | assert_eq!(it.next(), c('\n', 1)); |
| 90 | assert_eq!(it.next(), c('b', 2)); |
| 91 | assert_eq!(it.next(), None); |
| 92 | |
| 93 | // Test handling of \r\n. |
| 94 | let mut it = CharsAndLine::new("a\r\nb"); |
| 95 | assert_eq!(it.next(), c('a', 1)); |
| 96 | assert_eq!(it.next(), c('\n', 1)); |
| 97 | assert_eq!(it.next(), c('b', 2)); |
| 98 | assert_eq!(it.next(), None); |
| 99 | |
| 100 | // Test handling of a mix of \r and \n |
| 101 | let mut it = CharsAndLine::new("\n\r\n\r\r\r\n"); |
| 102 | assert_eq!(it.next(), c('\n', 1)); |
| 103 | assert_eq!(it.next(), c('\n', 2)); |
| 104 | assert_eq!(it.next(), c('\n', 3)); |
| 105 | assert_eq!(it.next(), c('\n', 4)); |
| 106 | assert_eq!(it.next(), None); |
| 107 | |
| 108 | // Unicode handling |
| 109 | let mut it = CharsAndLine::new("a→üs🦀"); |
| 110 | assert_eq!(it.next(), c('a', 1)); |
| 111 | assert_eq!(it.next(), c('→', 1)); |
| 112 | assert_eq!(it.next(), c('ü', 1)); |
| 113 | assert_eq!(it.next(), c('s', 1)); |
| 114 | assert_eq!(it.next(), c('🦀', 1)); |
| 115 | assert_eq!(it.next(), None); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn skip_backslash_newline() { |
| 120 | // Test a simple case. |
| 121 | let mut it = SkipBackslashNewline::new("a\\\nb"); |
| 122 | assert_eq!(it.next(), c('a', 1)); |
| 123 | assert_eq!(it.next(), c('b', 2)); |
| 124 | assert_eq!(it.next(), None); |
| 125 | |
| 126 | // Test a double case that requires the loop in the algorithm. |
| 127 | let mut it = SkipBackslashNewline::new("a\\\n\\\nb"); |
| 128 | assert_eq!(it.next(), c('a', 1)); |
| 129 | assert_eq!(it.next(), c('b', 3)); |
| 130 | assert_eq!(it.next(), None); |
| 131 | |
| 132 | // Test a backslash on its own |
| 133 | let mut it = SkipBackslashNewline::new("a\\b"); |
| 134 | assert_eq!(it.next(), c('a', 1)); |
| 135 | assert_eq!(it.next(), c('\\', 1)); |
| 136 | assert_eq!(it.next(), c('b', 1)); |
| 137 | assert_eq!(it.next(), None); |
| 138 | |
| 139 | // Test a case just before EOF |
| 140 | let mut it = SkipBackslashNewline::new("\\\n"); |
| 141 | assert_eq!(it.next(), None); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn replace_comments() { |
| 146 | // Test a slash that's not a comment |
| 147 | let mut it = ReplaceComments::new("a/b"); |
| 148 | assert_eq!(it.next(), c('a', 1)); |
| 149 | assert_eq!(it.next(), c('/', 1)); |
| 150 | assert_eq!(it.next(), c('b', 1)); |
| 151 | assert_eq!(it.next(), None); |
| 152 | |
| 153 | // Test a slash with nothing afterwards |
| 154 | let mut it = ReplaceComments::new("a/"); |
| 155 | assert_eq!(it.next(), c('a', 1)); |
| 156 | assert_eq!(it.next(), c('/', 1)); |
| 157 | assert_eq!(it.next(), None); |
| 158 | |
| 159 | // Test a single-line comment |
| 160 | let mut it = ReplaceComments::new("a//foo\nb"); |
| 161 | assert_eq!(it.next(), c('a', 1)); |
| 162 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 163 | assert_eq!(it.next(), c('\n', 1)); |
| 164 | assert_eq!(it.next(), c('b', 2)); |
| 165 | assert_eq!(it.next(), None); |
| 166 | |
| 167 | // Test a single-line comment without an ending newline |
| 168 | let mut it = ReplaceComments::new("//foo"); |
| 169 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 170 | assert_eq!(it.next(), None); |
| 171 | |
| 172 | // Test a single-line comment without nothing afterwards |
| 173 | let mut it = ReplaceComments::new("//"); |
| 174 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 175 | assert_eq!(it.next(), None); |
| 176 | |
| 177 | // Test a single-line comment with a line continuation |
| 178 | let mut it = ReplaceComments::new("//foo\\\na"); |
| 179 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 180 | assert_eq!(it.next(), None); |
| 181 | |
| 182 | // Test a multi-line comment |
| 183 | let mut it = ReplaceComments::new("a/*fo\n\no*/b"); |
| 184 | assert_eq!(it.next(), c('a', 1)); |
| 185 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 186 | assert_eq!(it.next(), c('b', 3)); |
| 187 | assert_eq!(it.next(), None); |
| 188 | |
| 189 | // Test a multi-line comment, without a proper ending (only the *) |
| 190 | let mut it = ReplaceComments::new("a/*fo\n\no*"); |
| 191 | assert_eq!(it.next(), c('a', 1)); |
| 192 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 193 | assert_eq!(it.next(), None); |
| 194 | |
| 195 | // Test a multi-line comment, without a proper ending (nothing) |
| 196 | let mut it = ReplaceComments::new("a/*fo\n\no"); |
| 197 | assert_eq!(it.next(), c('a', 1)); |
| 198 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 199 | assert_eq!(it.next(), None); |
| 200 | |
| 201 | // Test a multi-line comment, or /*/ not being a complete one |
| 202 | let mut it = ReplaceComments::new("a/*/b"); |
| 203 | assert_eq!(it.next(), c('a', 1)); |
| 204 | assert_eq!(it.next(), c(COMMENT_SENTINEL_VALUE, 1)); |
| 205 | assert_eq!(it.next(), None); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn lex_whitespace() { |
| 210 | // Empty input gives nothing. |
| 211 | let mut it = Lexer::new(""); |
| 212 | assert_eq!(it.next(), None); |
| 213 | |
| 214 | // Pure whitespace give nothing too |
| 215 | let mut it = Lexer::new("/**/\t //a"); |
| 216 | assert_eq!(it.next(), None); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn lex_newline() { |
| 221 | let mut it = Lexer::new("\r\n\n"); |
| 222 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 223 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 224 | assert_eq!(it.next(), None); |
| 225 | |
| 226 | // Check a newline is added only if the last token wasn't a newline |
| 227 | let mut it = Lexer::new("\r\n\n\t/**/ //"); |
| 228 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 229 | expect_lexer_end(&mut it); |
| 230 | |
| 231 | let mut it = Lexer::new("\r\n\n#"); |
| 232 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 233 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 234 | assert_eq!(unwrap_token_value(it.next()), TokenValue::Hash); |
| 235 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 236 | assert_eq!(it.next(), None); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn lex_hash() { |
| 241 | let mut it = Lexer::new("a#b"); |
| 242 | assert_eq!( |
| 243 | unwrap_token_value(it.next()), |
| 244 | TokenValue::Ident("a".to_string()) |
| 245 | ); |
| 246 | let token = unwrap_token(it.next()); |
| 247 | assert_eq!(token.value, TokenValue::Hash); |
| 248 | assert_eq!(token.location, l(1, 1..2)); |
| 249 | assert_eq!( |
| 250 | unwrap_token_value(it.next()), |
| 251 | TokenValue::Ident("b".to_string()) |
| 252 | ); |
| 253 | expect_lexer_end(&mut it); |
| 254 | |
| 255 | let mut it = Lexer::new("\nvoid #"); |
| 256 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 257 | assert_eq!( |
| 258 | unwrap_token_value(it.next()), |
| 259 | TokenValue::Ident("void".into()) |
| 260 | ); |
| 261 | let token = unwrap_token(it.next()); |
| 262 | assert_eq!(token.value, TokenValue::Hash); |
| 263 | assert_eq!(token.location, l(2, 6..7)); |
| 264 | expect_lexer_end(&mut it); |
| 265 | } |
| 266 | |
| 267 | #[test] |
| 268 | fn lex_metadata() { |
| 269 | // Test the metadata of the first token |
| 270 | let mut it = Lexer::new("1"); |
| 271 | assert_eq!( |
| 272 | unwrap_token(it.next()), |
| 273 | Token { |
| 274 | value: 1.into(), |
| 275 | location: l(1, 0..1), |
| 276 | leading_whitespace: true, |
| 277 | start_of_line: true |
| 278 | } |
| 279 | ); |
| 280 | expect_lexer_end(&mut it); |
| 281 | |
| 282 | // Test various whitespaces are recognized (or lack of) |
| 283 | let mut it = Lexer::new(" 1/*\n*/2\t3+\n4"); |
| 284 | // 1 is the first token and the whitespace doesn't prevent it from being the start of the |
| 285 | // line |
| 286 | assert_eq!( |
| 287 | unwrap_token(it.next()), |
| 288 | Token { |
| 289 | value: 1.into(), |
| 290 | location: l(1, 1..2), |
| 291 | leading_whitespace: true, |
| 292 | start_of_line: true |
| 293 | } |
| 294 | ); |
| 295 | // 2 is not at the start of the line because the \n in the /**/ doesn't count, however its |
| 296 | // location correctly lists the second line. |
| 297 | assert_eq!( |
| 298 | unwrap_token(it.next()), |
| 299 | Token { |
| 300 | value: 2.into(), |
| 301 | location: l(2, 7..8), |
| 302 | leading_whitespace: true, |
| 303 | start_of_line: false |
| 304 | } |
| 305 | ); |
| 306 | assert_eq!( |
| 307 | unwrap_token(it.next()), |
| 308 | Token { |
| 309 | value: 3.into(), |
| 310 | location: l(2, 9..10), |
| 311 | leading_whitespace: true, |
| 312 | start_of_line: false |
| 313 | } |
| 314 | ); |
| 315 | // + doesn't have a leading whitespace |
| 316 | assert_eq!( |
| 317 | unwrap_token(it.next()), |
| 318 | Token { |
| 319 | value: Punct::Plus.into(), |
| 320 | location: l(2, 10..11), |
| 321 | leading_whitespace: false, |
| 322 | start_of_line: false |
| 323 | } |
| 324 | ); |
| 325 | // The newline is correctly tagged on the preceeding line |
| 326 | assert_eq!( |
| 327 | unwrap_token(it.next()), |
| 328 | Token { |
| 329 | value: TokenValue::NewLine, |
| 330 | location: l(2, 11..12), |
| 331 | leading_whitespace: false, |
| 332 | start_of_line: false |
| 333 | } |
| 334 | ); |
| 335 | // 4 is after a newline that correctly sets start_of_line |
| 336 | assert_eq!( |
| 337 | unwrap_token(it.next()), |
| 338 | Token { |
| 339 | value: 4.into(), |
| 340 | location: l(3, 12..13), |
| 341 | leading_whitespace: true, |
| 342 | start_of_line: true |
| 343 | } |
| 344 | ); |
| 345 | // The final newline added by the lexer is at the correct position |
| 346 | assert_eq!( |
| 347 | unwrap_token(it.next()), |
| 348 | Token { |
| 349 | value: TokenValue::NewLine, |
| 350 | location: l(3, 13..13), |
| 351 | leading_whitespace: false, |
| 352 | start_of_line: false |
| 353 | } |
| 354 | ); |
| 355 | assert_eq!(it.next(), None); |
| 356 | } |
| 357 | |
| 358 | #[test] |
| 359 | fn lex_identifiers() { |
| 360 | // Test some basic identifier cases |
| 361 | let mut it = Lexer::new("foo BA_R baz0"); |
| 362 | let token = unwrap_token(it.next()); |
| 363 | assert_eq!(token.value, TokenValue::Ident("foo".to_string())); |
| 364 | assert_eq!(token.location, l(1, 0..3),); |
| 365 | let token = unwrap_token(it.next()); |
| 366 | assert_eq!(token.value, TokenValue::Ident("BA_R".to_string())); |
| 367 | assert_eq!(token.location, l(1, 4..8),); |
| 368 | let token = unwrap_token(it.next()); |
| 369 | assert_eq!(token.value, TokenValue::Ident("baz0".to_string())); |
| 370 | assert_eq!(token.location, l(1, 9..13),); |
| 371 | expect_lexer_end(&mut it); |
| 372 | |
| 373 | // Test _ is a valid identifier |
| 374 | let mut it = Lexer::new("_"); |
| 375 | assert_eq!( |
| 376 | unwrap_token_value(it.next()), |
| 377 | TokenValue::Ident("_".to_string()) |
| 378 | ); |
| 379 | expect_lexer_end(&mut it); |
| 380 | |
| 381 | // Test that identifiers are not split by escaped newlines |
| 382 | let mut it = Lexer::new("a\\\nb"); |
| 383 | assert_eq!( |
| 384 | unwrap_token_value(it.next()), |
| 385 | TokenValue::Ident("ab".to_string()) |
| 386 | ); |
| 387 | expect_lexer_end(&mut it); |
| 388 | |
| 389 | // Test that identifiers are split by other whitespace like /**/ |
| 390 | let mut it = Lexer::new("a/**/b"); |
| 391 | assert_eq!( |
| 392 | unwrap_token_value(it.next()), |
| 393 | TokenValue::Ident("a".to_string()) |
| 394 | ); |
| 395 | assert_eq!( |
| 396 | unwrap_token_value(it.next()), |
| 397 | TokenValue::Ident("b".to_string()) |
| 398 | ); |
| 399 | expect_lexer_end(&mut it); |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn lex_decimal() { |
| 404 | // Test some basic cases |
| 405 | let mut it = Lexer::new("1 0u 42 65536U"); |
| 406 | assert_eq!(unwrap_token_value(it.next()), 1.into()); |
| 407 | let token = unwrap_token(it.next()); |
| 408 | assert_eq!(token.value, 0u32.into()); |
| 409 | assert_eq!(token.location, l(1, 2..4),); |
| 410 | let token = unwrap_token(it.next()); |
| 411 | assert_eq!(token.value, 42.into()); |
| 412 | assert_eq!(token.location, l(1, 5..7),); |
| 413 | let token = unwrap_token(it.next()); |
| 414 | assert_eq!(token.value, 65536u32.into()); |
| 415 | assert_eq!(token.location, l(1, 8..14),); |
| 416 | expect_lexer_end(&mut it); |
| 417 | |
| 418 | // Test splitting with identifiers |
| 419 | let mut it = Lexer::new("31ab"); |
| 420 | assert_eq!(unwrap_token_value(it.next()), 31.into()); |
| 421 | assert_eq!( |
| 422 | unwrap_token_value(it.next()), |
| 423 | TokenValue::Ident("ab".to_string()) |
| 424 | ); |
| 425 | expect_lexer_end(&mut it); |
| 426 | |
| 427 | // Test splitting with whitespace |
| 428 | let mut it = Lexer::new("31/**/32"); |
| 429 | assert_eq!(unwrap_token_value(it.next()), 31.into()); |
| 430 | assert_eq!(unwrap_token_value(it.next()), 32.into()); |
| 431 | expect_lexer_end(&mut it); |
| 432 | |
| 433 | // Test splitting with punctuation |
| 434 | let mut it = Lexer::new("31+32"); |
| 435 | assert_eq!(unwrap_token_value(it.next()), 31.into()); |
| 436 | assert_eq!(unwrap_token_value(it.next()), Punct::Plus.into()); |
| 437 | assert_eq!(unwrap_token_value(it.next()), 32.into()); |
| 438 | expect_lexer_end(&mut it); |
| 439 | |
| 440 | // Test that 2^64 produces an overflow error but that 2^64-1 correctly parses (even if it might |
| 441 | // produce an error down the line). |
| 442 | let mut it = Lexer::new("18446744073709551616"); |
| 443 | assert_eq!(unwrap_error(it.next()), PreprocessorError::IntegerOverflow); |
| 444 | let mut it = Lexer::new("18446744073709551615"); |
| 445 | assert_eq!( |
| 446 | unwrap_token_value(it.next()), |
| 447 | TokenValue::Integer(Integer { |
| 448 | value: 18446744073709551615, |
| 449 | signed: true, |
| 450 | width: 32 |
| 451 | }) |
| 452 | ); |
| 453 | expect_lexer_end(&mut it); |
| 454 | |
| 455 | // Check that the 16bit or 64bit suffixes produce errors (for now). |
| 456 | let mut it = Lexer::new("13s"); |
| 457 | assert_eq!( |
| 458 | unwrap_error(it.next()), |
| 459 | PreprocessorError::NotSupported16BitLiteral |
| 460 | ); |
| 461 | let mut it = Lexer::new("13S"); |
| 462 | assert_eq!( |
| 463 | unwrap_error(it.next()), |
| 464 | PreprocessorError::NotSupported16BitLiteral |
| 465 | ); |
| 466 | let mut it = Lexer::new("13l"); |
| 467 | assert_eq!( |
| 468 | unwrap_error(it.next()), |
| 469 | PreprocessorError::NotSupported64BitLiteral |
| 470 | ); |
| 471 | let mut it = Lexer::new("13L"); |
| 472 | assert_eq!( |
| 473 | unwrap_error(it.next()), |
| 474 | PreprocessorError::NotSupported64BitLiteral |
| 475 | ); |
| 476 | |
| 477 | // Check that they produce unsupported errors even if they happen with a unsigned suffix too. |
| 478 | let mut it = Lexer::new("13uS"); |
| 479 | assert_eq!( |
| 480 | unwrap_error(it.next()), |
| 481 | PreprocessorError::NotSupported16BitLiteral |
| 482 | ); |
| 483 | let mut it = Lexer::new("13Ul"); |
| 484 | assert_eq!( |
| 485 | unwrap_error(it.next()), |
| 486 | PreprocessorError::NotSupported64BitLiteral |
| 487 | ); |
| 488 | } |
| 489 | |
| 490 | #[test] |
| 491 | fn lex_hexadecimal() { |
| 492 | // Test some basic cases |
| 493 | let mut it = Lexer::new("0x1 0X0u 0xBaFfe 0XcaFeU"); |
| 494 | assert_eq!(unwrap_token_value(it.next()), 1.into()); |
| 495 | assert_eq!(unwrap_token_value(it.next()), 0u32.into()); |
| 496 | let token = unwrap_token(it.next()); |
| 497 | assert_eq!(token.value, 0xBAFFE.into()); |
| 498 | assert_eq!(token.location, l(1, 9..16),); |
| 499 | let token = unwrap_token(it.next()); |
| 500 | assert_eq!(token.value, 0xCAFEu32.into()); |
| 501 | assert_eq!(token.location, l(1, 17..24),); |
| 502 | expect_lexer_end(&mut it); |
| 503 | |
| 504 | // Test with redundant zeroes |
| 505 | let mut it = Lexer::new("0x000 0x000000000000001"); |
| 506 | assert_eq!(unwrap_token_value(it.next()), 0.into()); |
| 507 | assert_eq!(unwrap_token_value(it.next()), 1.into()); |
| 508 | expect_lexer_end(&mut it); |
| 509 | |
| 510 | // Test splitting with identifiers |
| 511 | let mut it = Lexer::new("0x31zb"); |
| 512 | assert_eq!(unwrap_token_value(it.next()), 0x31.into()); |
| 513 | assert_eq!( |
| 514 | unwrap_token_value(it.next()), |
| 515 | TokenValue::Ident("zb".to_string()) |
| 516 | ); |
| 517 | expect_lexer_end(&mut it); |
| 518 | |
| 519 | // Test splitting with whitespace |
| 520 | let mut it = Lexer::new("0x31/**/32"); |
| 521 | assert_eq!(unwrap_token_value(it.next()), 0x31.into()); |
| 522 | assert_eq!(unwrap_token_value(it.next()), 32.into()); |
| 523 | expect_lexer_end(&mut it); |
| 524 | |
| 525 | // Test splitting with punctuation |
| 526 | let mut it = Lexer::new("0x31+32"); |
| 527 | assert_eq!(unwrap_token_value(it.next()), 0x31.into()); |
| 528 | assert_eq!(unwrap_token_value(it.next()), Punct::Plus.into()); |
| 529 | assert_eq!(unwrap_token_value(it.next()), 32.into()); |
| 530 | expect_lexer_end(&mut it); |
| 531 | |
| 532 | // Test that 2^64 produces an overflow error but that 2^64-1 correctly parses (even if it might |
| 533 | // produce an error down the line). |
| 534 | let mut it = Lexer::new("0x10000000000000000"); |
| 535 | assert_eq!(unwrap_error(it.next()), PreprocessorError::IntegerOverflow); |
| 536 | let mut it = Lexer::new("0xFFFFFFFFFFFFFFFF"); |
| 537 | assert_eq!( |
| 538 | unwrap_token_value(it.next()), |
| 539 | TokenValue::Integer(Integer { |
| 540 | value: 18446744073709551615, |
| 541 | signed: true, |
| 542 | width: 32 |
| 543 | }) |
| 544 | ); |
| 545 | expect_lexer_end(&mut it); |
| 546 | } |
| 547 | |
| 548 | #[test] |
| 549 | fn lex_octal() { |
| 550 | // Test some basic cases |
| 551 | let mut it = Lexer::new("01 00u 07654 01234u"); |
| 552 | assert_eq!(unwrap_token_value(it.next()), 1.into()); |
| 553 | assert_eq!(unwrap_token_value(it.next()), 0u32.into()); |
| 554 | assert_eq!(unwrap_token_value(it.next()), 4012.into()); |
| 555 | assert_eq!(unwrap_token_value(it.next()), 668u32.into()); |
| 556 | expect_lexer_end(&mut it); |
| 557 | |
| 558 | // Test with redundant zeroes |
| 559 | let mut it = Lexer::new("0000 0000000000000001"); |
| 560 | assert_eq!(unwrap_token_value(it.next()), 0.into()); |
| 561 | assert_eq!(unwrap_token_value(it.next()), 1.into()); |
| 562 | expect_lexer_end(&mut it); |
| 563 | |
| 564 | // Test splitting with identifiers |
| 565 | let mut it = Lexer::new("031zb"); |
| 566 | assert_eq!(unwrap_token_value(it.next()), 25.into()); |
| 567 | assert_eq!( |
| 568 | unwrap_token_value(it.next()), |
| 569 | TokenValue::Ident("zb".to_string()) |
| 570 | ); |
| 571 | expect_lexer_end(&mut it); |
| 572 | |
| 573 | // Test splitting with whitespace |
| 574 | let mut it = Lexer::new("031/**/32"); |
| 575 | assert_eq!(unwrap_token_value(it.next()), 25.into()); |
| 576 | assert_eq!(unwrap_token_value(it.next()), 32.into()); |
| 577 | expect_lexer_end(&mut it); |
| 578 | |
| 579 | // TODO(kangz): Fix octal numbers consuming 8 and 9s as well. This can be done with extra logic |
| 580 | // already but is not worth the complexity. |
| 581 | |
| 582 | // Test splitting with 8 and 9 |
| 583 | // let mut it = Lexer::new("039 038"); |
| 584 | // assert_eq!(unwrap_token_value(it.next()), 3.into()); |
| 585 | // assert_eq!(unwrap_token_value(it.next()), 9.into()); |
| 586 | // assert_eq!(unwrap_token_value(it.next()), 3.into()); |
| 587 | // assert_eq!(unwrap_token_value(it.next()), 8.into()); |
| 588 | // expect_lexer_end(&mut it); |
| 589 | |
| 590 | // Test splitting with punctuation |
| 591 | let mut it = Lexer::new("031+32"); |
| 592 | assert_eq!(unwrap_token_value(it.next()), 25.into()); |
| 593 | assert_eq!(unwrap_token_value(it.next()), Punct::Plus.into()); |
| 594 | assert_eq!(unwrap_token_value(it.next()), 32.into()); |
| 595 | expect_lexer_end(&mut it); |
| 596 | |
| 597 | // Test that 2^64 produces an overflow error but that 2^64-1 correctly parses (even if it might |
| 598 | // produce an error down the line). |
| 599 | let mut it = Lexer::new("02000000000000000000000"); |
| 600 | assert_eq!(unwrap_error(it.next()), PreprocessorError::IntegerOverflow); |
| 601 | let mut it = Lexer::new("01777777777777777777777"); |
| 602 | assert_eq!( |
| 603 | unwrap_token_value(it.next()), |
| 604 | TokenValue::Integer(Integer { |
| 605 | value: 18446744073709551615, |
| 606 | signed: true, |
| 607 | width: 32 |
| 608 | }) |
| 609 | ); |
| 610 | expect_lexer_end(&mut it); |
| 611 | } |
| 612 | |
| 613 | #[test] |
| 614 | fn lex_float() { |
| 615 | // Test a couple simple cases. |
| 616 | let mut it = Lexer::new("1.0 0.0"); |
| 617 | assert_eq!(unwrap_token_value(it.next()), 1.0f32.into()); |
| 618 | assert_eq!(unwrap_token_value(it.next()), 0.0f32.into()); |
| 619 | expect_lexer_end(&mut it); |
| 620 | |
| 621 | // Test parsing with a leading . |
| 622 | let mut it = Lexer::new(".99 0.01 .00000000"); |
| 623 | assert_eq!(unwrap_token_value(it.next()), 0.99f32.into()); |
| 624 | assert_eq!(unwrap_token_value(it.next()), 0.01f32.into()); |
| 625 | assert_eq!(unwrap_token_value(it.next()), 0.0f32.into()); |
| 626 | expect_lexer_end(&mut it); |
| 627 | |
| 628 | // Test parsing with nothing after the . |
| 629 | let mut it = Lexer::new("42. 0."); |
| 630 | assert_eq!(unwrap_token_value(it.next()), 42.0f32.into()); |
| 631 | assert_eq!(unwrap_token_value(it.next()), 0.0f32.into()); |
| 632 | expect_lexer_end(&mut it); |
| 633 | |
| 634 | // Test parsing with the float suffix |
| 635 | let mut it = Lexer::new("1000.f 1.f .2f"); |
| 636 | assert_eq!(unwrap_token_value(it.next()), 1000.0f32.into()); |
| 637 | assert_eq!(unwrap_token_value(it.next()), 1.0f32.into()); |
| 638 | assert_eq!(unwrap_token_value(it.next()), 0.2f32.into()); |
| 639 | expect_lexer_end(&mut it); |
| 640 | |
| 641 | // Test parsing with exponents |
| 642 | // - with / without float suffixes |
| 643 | // - at different points in the float parsing. |
| 644 | let mut it = Lexer::new("3e10 4.1e-10f .01e12F 4.1e+10f"); |
| 645 | assert_eq!(unwrap_token_value(it.next()), 3e10f32.into()); |
| 646 | assert_eq!(unwrap_token_value(it.next()), 4.1e-10f32.into()); |
| 647 | assert_eq!(unwrap_token_value(it.next()), 0.01e12f32.into()); |
| 648 | assert_eq!(unwrap_token_value(it.next()), 4.1e+10f32.into()); |
| 649 | expect_lexer_end(&mut it); |
| 650 | |
| 651 | // Test parsing with exponents |
| 652 | // - After values looking like octal integer (works) |
| 653 | // - After values looking like hexadecimal integer (doesn't work) |
| 654 | let mut it = Lexer::new("05e2 0x1e-2"); |
| 655 | assert_eq!(unwrap_token_value(it.next()), 5e2f32.into()); |
| 656 | |
| 657 | assert_eq!(unwrap_token_value(it.next()), 0x1Ei32.into()); |
| 658 | assert_eq!(unwrap_token_value(it.next()), Punct::Minus.into()); |
| 659 | assert_eq!(unwrap_token_value(it.next()), 2i32.into()); |
| 660 | |
| 661 | // Test parsing with nothing valid after the 'e' (technically it shouldn't |
| 662 | // be an error, but there's no language where that sequence of token is |
| 663 | // valid. |
| 664 | let mut it = Lexer::new("1.0e"); |
| 665 | assert_eq!( |
| 666 | unwrap_error(it.next()), |
| 667 | PreprocessorError::FloatParsingError |
| 668 | ); |
| 669 | |
| 670 | // Check that 16bit and 64bit suffixes produce errors |
| 671 | let mut it = Lexer::new("1.0l"); |
| 672 | assert_eq!( |
| 673 | unwrap_error(it.next()), |
| 674 | PreprocessorError::NotSupported64BitLiteral |
| 675 | ); |
| 676 | let mut it = Lexer::new("1.0L"); |
| 677 | assert_eq!( |
| 678 | unwrap_error(it.next()), |
| 679 | PreprocessorError::NotSupported64BitLiteral |
| 680 | ); |
| 681 | let mut it = Lexer::new("1.0h"); |
| 682 | assert_eq!( |
| 683 | unwrap_error(it.next()), |
| 684 | PreprocessorError::NotSupported16BitLiteral |
| 685 | ); |
| 686 | let mut it = Lexer::new("1.0H"); |
| 687 | assert_eq!( |
| 688 | unwrap_error(it.next()), |
| 689 | PreprocessorError::NotSupported16BitLiteral |
| 690 | ); |
| 691 | } |
| 692 | |
| 693 | #[test] |
| 694 | fn lex_punctuation() { |
| 695 | // Test parsing some of the token (but not all, that'd be too many tests!) |
| 696 | let mut it = Lexer::new("+ != <<="); |
| 697 | assert_eq!(unwrap_token_value(it.next()), Punct::Plus.into()); |
| 698 | let token = unwrap_token(it.next()); |
| 699 | assert_eq!(token.value, Punct::NotEqual.into()); |
| 700 | assert_eq!(token.location, l(1, 2..4),); |
| 701 | let token = unwrap_token(it.next()); |
| 702 | assert_eq!(token.value, Punct::LeftShiftAssign.into()); |
| 703 | assert_eq!(token.location, l(1, 5..8),); |
| 704 | expect_lexer_end(&mut it); |
| 705 | |
| 706 | // Test parsing a token that's a prefix of another one just before EOF |
| 707 | let mut it = Lexer::new("<"); |
| 708 | assert_eq!(unwrap_token_value(it.next()), Punct::LeftAngle.into()); |
| 709 | expect_lexer_end(&mut it); |
| 710 | |
| 711 | // Test \\\n doesn't split the token |
| 712 | let mut it = Lexer::new("=\\\n="); |
| 713 | assert_eq!(unwrap_token_value(it.next()), Punct::EqualEqual.into()); |
| 714 | expect_lexer_end(&mut it); |
| 715 | |
| 716 | // Test whitespace splits the token |
| 717 | let mut it = Lexer::new("+/**/="); |
| 718 | assert_eq!(unwrap_token_value(it.next()), Punct::Plus.into()); |
| 719 | assert_eq!(unwrap_token_value(it.next()), Punct::Equal.into()); |
| 720 | expect_lexer_end(&mut it); |
| 721 | |
| 722 | // Test a number stops processing the token |
| 723 | let mut it = Lexer::new("!1"); |
| 724 | assert_eq!(unwrap_token_value(it.next()), Punct::Bang.into()); |
| 725 | assert_eq!(unwrap_token_value(it.next()), 1.into()); |
| 726 | expect_lexer_end(&mut it); |
| 727 | |
| 728 | // Test an identifier stops processing the token |
| 729 | let mut it = Lexer::new("&a"); |
| 730 | assert_eq!(unwrap_token_value(it.next()), Punct::Ampersand.into()); |
| 731 | assert_eq!( |
| 732 | unwrap_token_value(it.next()), |
| 733 | TokenValue::Ident("a".to_string()) |
| 734 | ); |
| 735 | expect_lexer_end(&mut it); |
| 736 | |
| 737 | // Test whitespace splits the token |
| 738 | let mut it = Lexer::new(">/**/>"); |
| 739 | assert_eq!(unwrap_token_value(it.next()), Punct::RightAngle.into()); |
| 740 | assert_eq!(unwrap_token_value(it.next()), Punct::RightAngle.into()); |
| 741 | expect_lexer_end(&mut it); |
| 742 | |
| 743 | // Test that tokens are parsed greedily: `a+++++b` is `a ++ ++ + b` (invalid GLSL) and not |
| 744 | // `(a ++) + (++ b)` (valid GLSL) |
| 745 | let mut it = Lexer::new("+++++"); |
| 746 | assert_eq!(unwrap_token_value(it.next()), Punct::Increment.into()); |
| 747 | assert_eq!(unwrap_token_value(it.next()), Punct::Increment.into()); |
| 748 | assert_eq!(unwrap_token_value(it.next()), Punct::Plus.into()); |
| 749 | expect_lexer_end(&mut it); |
| 750 | |
| 751 | // Test that an invalid char produces and error |
| 752 | let mut it = Lexer::new("@"); |
| 753 | assert_eq!( |
| 754 | unwrap_error(it.next()), |
| 755 | PreprocessorError::UnexpectedCharacter |
| 756 | ); |
| 757 | |
| 758 | // Extra punctuation tests for code coverage. |
| 759 | let mut it = Lexer::new("<= >= += -= &= || |= | ^= { } ] ? ."); |
| 760 | assert_eq!(unwrap_token_value(it.next()), Punct::LessEqual.into()); |
| 761 | assert_eq!(unwrap_token_value(it.next()), Punct::GreaterEqual.into()); |
| 762 | assert_eq!(unwrap_token_value(it.next()), Punct::AddAssign.into()); |
| 763 | assert_eq!(unwrap_token_value(it.next()), Punct::SubAssign.into()); |
| 764 | assert_eq!(unwrap_token_value(it.next()), Punct::AndAssign.into()); |
| 765 | assert_eq!(unwrap_token_value(it.next()), Punct::LogicalOr.into()); |
| 766 | assert_eq!(unwrap_token_value(it.next()), Punct::OrAssign.into()); |
| 767 | assert_eq!(unwrap_token_value(it.next()), Punct::Pipe.into()); |
| 768 | assert_eq!(unwrap_token_value(it.next()), Punct::XorAssign.into()); |
| 769 | assert_eq!(unwrap_token_value(it.next()), Punct::LeftBrace.into()); |
| 770 | assert_eq!(unwrap_token_value(it.next()), Punct::RightBrace.into()); |
| 771 | assert_eq!(unwrap_token_value(it.next()), Punct::RightBracket.into()); |
| 772 | assert_eq!(unwrap_token_value(it.next()), Punct::Question.into()); |
| 773 | assert_eq!(unwrap_token_value(it.next()), Punct::Dot.into()); |
| 774 | expect_lexer_end(&mut it); |
| 775 | } |
| 776 | |
| 777 | #[test] |
| 778 | fn lex_had_comments() { |
| 779 | // Test that had_comments doesn't get set to true if there is no comments. |
| 780 | let mut it = Lexer::new("#version"); |
| 781 | assert!(!it.had_comments()); |
| 782 | assert_eq!(unwrap_token_value(it.next()), TokenValue::Hash); |
| 783 | assert!(!it.had_comments()); |
| 784 | assert_eq!( |
| 785 | unwrap_token_value(it.next()), |
| 786 | TokenValue::Ident("version".to_string()) |
| 787 | ); |
| 788 | assert!(!it.had_comments()); |
| 789 | expect_lexer_end(&mut it); |
| 790 | |
| 791 | // Test that had_comments doesn't get triggered by its sentinel value of '\r' |
| 792 | let mut it = Lexer::new("\r!"); |
| 793 | assert!(!it.had_comments()); |
| 794 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 795 | assert_eq!(unwrap_token_value(it.next()), Punct::Bang.into()); |
| 796 | assert!(!it.had_comments()); |
| 797 | expect_lexer_end(&mut it); |
| 798 | |
| 799 | // Test that had_comments gets triggered by // comments |
| 800 | let mut it = Lexer::new("//\n!"); |
| 801 | assert!(!it.had_comments()); |
| 802 | assert_eq!(unwrap_token_value(it.next()), TokenValue::NewLine); |
| 803 | assert!(it.had_comments()); |
| 804 | assert_eq!(unwrap_token_value(it.next()), Punct::Bang.into()); |
| 805 | assert!(it.had_comments()); |
| 806 | expect_lexer_end(&mut it); |
| 807 | |
| 808 | // Test that had_comments doesn't gets triggered by /**/ comments |
| 809 | let mut it = Lexer::new("/**/#version"); |
| 810 | assert!(!it.had_comments()); |
| 811 | assert_eq!(unwrap_token_value(it.next()), TokenValue::Hash); |
| 812 | assert!(it.had_comments()); |
| 813 | assert_eq!( |
| 814 | unwrap_token_value(it.next()), |
| 815 | TokenValue::Ident("version".to_string()) |
| 816 | ); |
| 817 | assert!(it.had_comments()); |
| 818 | expect_lexer_end(&mut it); |
| 819 | } |
| 820 | |
| 821 | // TODO test has_whitespace |