1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
use crate::{CharExt, Scanner, ScannerResult};
/// [`Scanner`] extension for scanning Rust tokens.
///
/// **Note:** When using the `scan_rust_*()` methods, the order they are
/// called matters.
pub trait RustScannerExt<'text>: crate::private::Sealed {
/// Scans a single [Rust line comment].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// // Line Comment
/// //! Inner Doc Comment
/// /// Outer Doc Comment
/// "#;
///
/// let comments = [
/// (3..18, "// Line Comment"),
/// (21..42, "//! Inner Doc Comment"),
/// (45..66, "/// Outer Doc Comment"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for comment in comments {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_line_comment(), Ok(comment));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust line comment]: https://doc.rust-lang.org/reference/comments.html
fn scan_rust_line_comment(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust block comment].
///
/// **Note:** Rust block comment **allow** nested block comments.
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// /* Block Comment */
///
/// /* Multi
/// // Line
/// Block
/// Comment */
///
/// /* Multi
/// // Line /*
/// Nested
/// /* Block */
/// Comment */ */
///
/// /* Unterminated Block Comment
/// "#;
///
/// let comments = [
/// (3..22, "/* Block Comment */"),
/// (26..71, "/* Multi\n // Line\n Block\n Comment */"),
/// (75..141, "/* Multi\n // Line /*\n Nested\n /* Block */\n Comment */ */"),
/// (145..175, "/* Unterminated Block Comment\n"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for comment in comments {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_block_comment(), Ok(comment));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust block comment]: https://doc.rust-lang.org/reference/comments.html
fn scan_rust_block_comment(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust identifier].
///
/// **Note:** This **does not** differentiate between [Rust identifier]s
/// and [Rust keyword]s. If needed manually check if the returned `Ok` string slice
/// is a [Rust keyword] or not.
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// foo
/// foo_bar
/// _foo_
/// æøå
/// ľúbiť
/// 東京
/// "#;
///
/// let idents = [
/// (3..6, "foo"),
/// (9..16, "foo_bar"),
/// (19..24, "_foo_"),
/// (27..33, "æøå"),
/// (36..44, "ľúbiť"),
/// (47..53, "東京"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for ident in idents {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_identifier(), Ok(ident));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust identifier]: https://doc.rust-lang.org/reference/identifiers.html
/// [Rust keyword]: https://doc.rust-lang.org/reference/keywords.html
fn scan_rust_identifier(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [raw Rust identifier].
///
/// **Note:** This **does not** differentiate between [Rust identifier]s
/// and [Rust keyword]s. If needed manually check if the returned `Ok` string slice
/// is a [Rust keyword] or not.
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// r#foo
/// r#type
/// r#while
/// r#æøå
/// r#ľúbiť
/// r#東京
/// "#;
///
/// let idents = [
/// (3..8, "r#foo"),
/// (11..17, "r#type"),
/// (20..27, "r#while"),
/// (30..38, "r#æøå"),
/// (41..51, "r#ľúbiť"),
/// (54..62, "r#東京"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for ident in idents {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_raw_identifier(), Ok(ident));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [raw Rust identifier]: https://doc.rust-lang.org/reference/identifiers.html
/// [Rust identifier]: https://doc.rust-lang.org/reference/identifiers.html
/// [Rust keyword]: https://doc.rust-lang.org/reference/keywords.html
fn scan_rust_raw_identifier(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust character].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// 'A'
/// 'Æ'
/// 'Á'
/// '東'
/// '🦀'
///
/// '"'
/// '\\'
/// '\''
/// '\n'
/// '\0'
/// "#;
///
/// let chars = [
/// (3..6, "'A'"),
/// (9..13, "'Æ'"),
/// (16..20, "'Á'"),
/// (23..28, "'東'"),
/// (31..37, "'🦀'"),
/// (41..44, "'\"'"),
/// (47..51, "'\\\\'"),
/// (54..58, "'\\''"),
/// (61..65, "'\\n'"),
/// (68..72, "'\\0'"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for c in chars {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_char(), Ok(c));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust character]: https://doc.rust-lang.org/reference/tokens.html#character-literals
fn scan_rust_char(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust string].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// "Hello World"
///
/// "Rust strings
/// can span multiple
/// lines"
///
/// "Foo \" Bar"
///
/// "Unterminated String
/// "#;
///
/// let strings = [
/// (3..16, "\"Hello World\""),
/// (20..64, "\"Rust strings\n can span multiple\n lines\""),
/// (68..80, "\"Foo \\\" Bar\""),
/// (84..105, "\"Unterminated String\n"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for string in strings {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_string(), Ok(string));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust string]: https://doc.rust-lang.org/reference/tokens.html#string-literals
fn scan_rust_string(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [raw Rust string].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#####"
/// r#"Hello World"#
///
/// r###"Raw Rust strings"
/// "can span multiple"
/// "lines"###
///
/// r##"Foo #"# Bar"##
///
/// r###"Unterminated String
/// "#####;
///
/// let raw_strings = [
/// (3..19, "r#\"Hello World\"#"),
/// (23..88, "r###\"Raw Rust strings\"\n \"can span multiple\"\n \"lines\"###"),
/// (92..110, "r##\"Foo #\"# Bar\"##"),
/// (114..139, "r###\"Unterminated String\n"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for raw_string in raw_strings {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_raw_string(), Ok(raw_string));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [raw Rust string]: https://doc.rust-lang.org/reference/tokens.html#string-literals
fn scan_rust_raw_string(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust integer decimal literal].
///
/// **Note:** Rust integer literals do not allow a sign in front
/// of the literal, i.e. `-10` is two tokens `["-", "10"]`.
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// 0
/// 123
///
/// 1_
/// 1__
/// 1_2_3
/// 1__2__3__
/// "#;
///
/// let integers = [
/// (3..4, "0"),
/// (7..10, "123"),
/// (14..16, "1_"),
/// (19..22, "1__"),
/// (25..30, "1_2_3"),
/// (33..42, "1__2__3__"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for integer in integers {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_int_dec(), Ok(integer));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust integer decimal literal]: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_dec(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust integer hex literal].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// 0x0
/// 0xFF
///
/// 0x_FF_FF_FF_FF_
/// "#;
///
/// let hex_integers = [
/// (3..6, "0x0"),
/// (9..13, "0xFF"),
/// (17..32, "0x_FF_FF_FF_FF_"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for hex_integer in hex_integers {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_int_hex(), Ok(hex_integer));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust integer hex literal]: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_hex(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust integer octal literal].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// 0o0
/// 0o100
///
/// 0o_1_0_0_
/// "#;
///
/// let oct_integers = [
/// (3..6, "0o0"),
/// (9..14, "0o100"),
/// (18..27, "0o_1_0_0_"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for oct_integer in oct_integers {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_int_oct(), Ok(oct_integer));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust integer octal literal]: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_oct(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust integer binary literal].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// 0b0
/// 0b1
/// 0b10
/// 0b11
/// 0b100
///
/// 0b_1_0_0_
/// "#;
///
/// let bin_integers = [
/// (3..6, "0b0"),
/// (9..12, "0b1"),
/// (15..19, "0b10"),
/// (22..26, "0b11"),
/// (29..34, "0b100"),
/// (38..47, "0b_1_0_0_"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for bin_integer in bin_integers {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_int_bin(), Ok(bin_integer));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust integer binary literal]: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_bin(&mut self) -> ScannerResult<'text, &'text str>;
/// Scans a single [Rust floating-point literal].
///
/// **Note:** This has the same lifetime as the original `text`,
/// so the scanner can continue to be used while this exists.
///
/// # Example
///
/// ```rust
/// use text_scanner::{ext::RustScannerExt, Scanner};
///
/// let text = r#"
/// 12.
/// 12.34
///
/// 12.
/// 12.34
///
/// 12.34E56
/// 12.34E+56
/// 12.34E-56
///
/// 1_2_.
/// 1_2_.3_4_
///
/// 1_2_.3_4_E_5_6_
/// 1_2_.3_4_E+_5_6_
/// 1_2_.3_4_E-_5_6_
/// "#;
///
/// let floats = [
/// (3..6, "12."),
/// (9..14, "12.34"),
/// (18..21, "12."),
/// (24..29, "12.34"),
/// (33..41, "12.34E56"),
/// (44..53, "12.34E+56"),
/// (56..65, "12.34E-56"),
/// (69..74, "1_2_."),
/// (77..86, "1_2_.3_4_"),
/// (90..105, "1_2_.3_4_E_5_6_"),
/// (108..124, "1_2_.3_4_E+_5_6_"),
/// (127..143, "1_2_.3_4_E-_5_6_"),
/// ];
///
/// let mut scanner = Scanner::new(text);
/// for float in floats {
/// scanner.skip_whitespace();
/// assert_eq!(scanner.scan_rust_float(), Ok(float));
/// }
///
/// # scanner.skip_whitespace();
/// # assert_eq!(scanner.remaining_text(), "");
/// ```
///
/// [Rust floating-point literal]: https://doc.rust-lang.org/reference/tokens.html#floating-point-literals
fn scan_rust_float(&mut self) -> ScannerResult<'text, &'text str>;
}
impl<'text> RustScannerExt<'text> for Scanner<'text> {
// Reference: https://doc.rust-lang.org/reference/comments.html
fn scan_rust_line_comment(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_str("//")?;
scanner.skip_until_char_any(&['\n', '\r']);
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/comments.html
fn scan_rust_block_comment(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_str("/*")?;
let mut open = 1;
loop {
scanner.skip_until_char_any(&['*', '/']);
match scanner.next() {
Ok((_r, '*')) => {
if let Ok((_r, '/')) = scanner.next() {
if open == 1 {
break;
}
open -= 1;
}
}
Ok((_r, '/')) => {
if let Ok((_r, '*')) = scanner.next() {
open += 1;
}
}
Ok((_r, _c)) => {}
Err(_) => break,
}
}
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/identifiers.html
fn scan_rust_identifier(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_if(|c| c.is_alphabetic() || (c == '_'))?;
scanner.skip_while(|c| c.is_alphanumeric() || (c == '_'));
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/identifiers.html
fn scan_rust_raw_identifier(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_str("r#")?;
scanner.scan_rust_identifier()?;
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#character-literals
fn scan_rust_char(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_char('\'')?;
let (_r, c) = scanner.next()?;
if c == '\\' {
// Skip the next character as it is escaped
// Note: Technically any character is not valid
_ = scanner.next();
}
scanner.accept_char('\'')?;
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#string-literals
fn scan_rust_string(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_char('"')?;
loop {
scanner.skip_until_char_any(&['"', '\\']);
match scanner.next() {
Ok((_r, '"')) => break,
Ok((_r, '\\')) => {
// Skip the next character as it is escaped
// Note: Technically any character is not valid
_ = scanner.next();
}
Ok(_) => unreachable!(),
Err(_) => break,
}
}
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#raw-string-literals
fn scan_rust_raw_string(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_char('r')?;
let hashes = scanner.skip_while_char('#').0.len();
scanner.accept_char('"')?;
'scan: loop {
scanner.skip_until_char('"');
if scanner.next().is_err() {
break;
}
if hashes > 0 {
for _ in 0..hashes {
if scanner.accept_char('#').is_err() {
continue 'scan;
}
}
break;
} else {
break;
}
}
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_dec(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_if_ext(char::is_ascii_digit)?;
scanner.skip_while(|c| c.is_ascii_digit() || (c == '_'));
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_hex(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_str("0x")?;
scanner.skip_while_char('_');
scanner.accept_if_ext(char::is_ascii_hexdigit)?;
scanner.skip_while(|c| c.is_ascii_hexdigit() || (c == '_'));
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_oct(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_str("0o")?;
scanner.skip_while_char('_');
scanner.accept_if(CharExt::is_ascii_octdigit)?;
scanner.skip_while(|c| CharExt::is_ascii_octdigit(c) || (c == '_'));
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#integer-literals
fn scan_rust_int_bin(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.accept_str("0b")?;
scanner.skip_while_char('_');
scanner.accept_if(CharExt::is_ascii_bindigit)?;
scanner.skip_while(|c| c.is_ascii_bindigit() || (c == '_'));
Ok(())
})
}
// Reference: https://doc.rust-lang.org/reference/tokens.html#floating-point-literals
fn scan_rust_float(&mut self) -> ScannerResult<'text, &'text str> {
self.scan_with(|scanner| {
scanner.scan_rust_int_dec()?;
scanner.accept_char('.')?;
if scanner.scan_rust_int_dec().is_ok() && scanner.accept_char_any(&['e', 'E']).is_ok() {
_ = scanner.accept_char_any(&['+', '-']);
scanner.skip_while_char('_');
scanner.accept_if_ext(char::is_ascii_digit)?;
scanner.skip_while(|c| c.is_ascii_digit() || (c == '_'));
}
Ok(())
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_line_comments() {
let code = "
// Line Comment
// Line Comment\r
//! Inner Line Doc Comment
/// Outer Line Doc Comment
//
//\t
///
";
let mut scanner = Scanner::new(code);
assert_eq!(scanner.skip_whitespace().0, 0..13);
assert_eq!(
scanner.scan_rust_line_comment(),
Ok((13..28, "// Line Comment"))
);
assert_eq!(scanner.skip_whitespace().0, 28..41);
assert_eq!(
scanner.scan_rust_line_comment(),
Ok((41..56, "// Line Comment"))
);
assert_eq!(scanner.skip_whitespace().0, 56..70);
assert_eq!(
scanner.scan_rust_line_comment(),
Ok((70..96, "//! Inner Line Doc Comment"))
);
assert_eq!(scanner.skip_whitespace().0, 96..109);
assert_eq!(
scanner.scan_rust_line_comment(),
Ok((109..135, "/// Outer Line Doc Comment"))
);
assert_eq!(scanner.skip_whitespace().0, 135..148);
assert_eq!(scanner.scan_rust_line_comment(), Ok((148..150, "//")));
assert_eq!(scanner.skip_whitespace().0, 150..163);
assert_eq!(scanner.scan_rust_line_comment(), Ok((163..166, "//\t")));
assert_eq!(scanner.skip_whitespace().0, 166..179);
assert_eq!(scanner.scan_rust_line_comment(), Ok((179..182, "///")));
assert_eq!(scanner.skip_whitespace().0, 182..191);
assert_eq!(scanner.remaining_text(), "");
}
#[test]
fn test_block_comments() {
let code = "
/* Single Line Block Comment */
/* Two Line
Block Comment */
/*
Multiline
Block
Comment
*/
/*
/* Nested
// /* Block */
Comment */
*/
/**/
/*
*/
/**//*
*//**/
/* Unclosed Block Comment
";
let mut scanner = Scanner::new(code);
assert_eq!(scanner.skip_whitespace().0, 0..13);
assert_eq!(
scanner.scan_rust_block_comment(),
Ok((13..44, "/* Single Line Block Comment */"))
);
assert_eq!(scanner.skip_whitespace().0, 44..57);
assert_eq!(
scanner.scan_rust_block_comment(),
Ok((57..97, "/* Two Line\n Block Comment */"))
);
assert_eq!(scanner.skip_whitespace().0, 97..111);
assert_eq!(
scanner.scan_rust_block_comment(),
Ok((
111..190,
"/*\n\n Multiline\n Block\n Comment\n\n */"
))
);
assert_eq!(scanner.skip_whitespace().0, 190..204);
assert_eq!(
scanner.scan_rust_block_comment(),
Ok((204..295, "/*\n\n /* Nested\n // /* Block */\n Comment */\n\n */"))
);
assert_eq!(scanner.skip_whitespace().0, 295..309);
assert_eq!(scanner.scan_rust_block_comment(), Ok((309..313, "/**/")));
assert_eq!(scanner.skip_whitespace().0, 313..326);
assert_eq!(
scanner.scan_rust_block_comment(),
Ok((326..343, "/*\n */"))
);
assert_eq!(scanner.skip_whitespace().0, 343..356);
assert_eq!(scanner.scan_rust_block_comment(), Ok((356..360, "/**/")));
assert_eq!(scanner.skip_whitespace().0, 360..360);
assert_eq!(
scanner.scan_rust_block_comment(),
Ok((360..377, "/*\n */"))
);
assert_eq!(scanner.skip_whitespace().0, 377..377);
assert_eq!(scanner.scan_rust_block_comment(), Ok((377..381, "/**/")));
assert_eq!(scanner.skip_whitespace().0, 381..395);
assert_eq!(
scanner.scan_rust_block_comment(),
Ok((395..429, "/* Unclosed Block Comment\n "))
);
assert_eq!(scanner.skip_whitespace().0, 429..429);
assert_eq!(scanner.remaining_text(), "");
}
#[test]
fn test_identifiers() {
let cases = [
// text, expected, remaining text
("_", Some("_"), ""),
("x", Some("x"), ""),
("foo", Some("foo"), ""),
("_bar", Some("_bar"), ""),
("foo_bar_baz__", Some("foo_bar_baz__"), ""),
("foo-bar", Some("foo"), "-bar"),
("2foo", None, "2foo"),
("+foo", None, "+foo"),
];
for (text, expected, remaining) in cases {
let mut scanner = Scanner::new(text);
let actual = scanner.scan_rust_identifier().map(|(_, ident)| ident).ok();
assert_eq!(actual, expected);
assert_eq!(scanner.remaining_text(), remaining);
}
}
#[test]
fn test_raw_identifiers() {
let cases = [
// text, expected, remaining text
("r#x", Some("r#x"), ""),
("r#foo", Some("r#foo"), ""),
("r#_foo", Some("r#_foo"), ""),
("r#foo_bar_baz__", Some("r#foo_bar_baz__"), ""),
("r#type", Some("r#type"), ""),
("r#struct", Some("r#struct"), ""),
// Warning: Technically Rust does not allow `r#_`. However, this implementation
// only scans the raw identifier format, and does not verify the validity of the
// raw identifiers
("r#_", Some("r#_"), ""),
("r", None, "r"),
("r#", None, "r#"),
("r#2", None, "r#2"),
("r#2foo", None, "r#2foo"),
];
for (text, expected, remaining) in cases {
let mut scanner = Scanner::new(text);
let actual = scanner
.scan_rust_raw_identifier()
.map(|(_, ident)| ident)
.ok();
assert_eq!(actual, expected);
assert_eq!(scanner.remaining_text(), remaining);
}
}
#[test]
fn test_strings() {
let cases = [
// text, expected, remaining text
("\"\"", Some("\"\""), ""),
("\"Hello World\"", Some("\"Hello World\""), ""),
("\"Hello\nWorld\"", Some("\"Hello\nWorld\""), ""),
("\"Hello\\nWorld\"", Some("\"Hello\\nWorld\""), ""),
(r#""Hello \" World""#, Some(r#""Hello \" World""#), ""),
(r#""Hello \\\" World""#, Some(r#""Hello \\\" World""#), ""),
("\"No Closing Quote", Some("\"No Closing Quote"), ""),
(r#""Hello \\" World""#, Some(r#""Hello \\""#), " World\""),
];
for (text, expected, remaining) in cases {
let mut scanner = Scanner::new(text);
let actual = scanner.scan_rust_string().map(|(_, s)| s).ok();
assert_eq!(actual, expected);
assert_eq!(scanner.remaining_text(), remaining);
}
}
#[test]
fn test_raw_strings() {
let cases = [
// text, expected, remaining text
("r\"\"", Some("r\"\""), ""),
("r#\"\"#", Some("r#\"\"#"), ""),
("r#\"\n\"\n\"\"#", Some("r#\"\n\"\n\"\"#"), ""),
("r#\"Hello \" World\"#", Some("r#\"Hello \" World\"#"), ""),
(
"r#####\"Foo #\"# Bar ####\"#### Baz\"#####",
Some("r#####\"Foo #\"# Bar ####\"#### Baz\"#####"),
"",
),
(
"r###\"Foo \"## Bar\" Baz",
Some("r###\"Foo \"## Bar\" Baz"),
"",
),
("r##\"\"#", Some("r##\"\"#"), ""),
("r#\"\"##", Some("r#\"\"#"), "#"),
("r\"Hello \" World\"", Some("r\"Hello \""), " World\""),
];
for (text, expected, remaining) in cases {
let mut scanner = Scanner::new(text);
let actual = scanner.scan_rust_raw_string().map(|(_, s)| s).ok();
assert_eq!(actual, expected);
assert_eq!(scanner.remaining_text(), remaining);
}
}
}