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 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
//! The Texlang lexer
//!
//! This module contains Texlang's TeX lexer, which converts TeX source code into TeX tokens.
//!
//! ## Just-in-time lexing
//!
//! A TeX lexer is different to most other lexers because TeX's lexing rules
//! are dynamic and can be changed from within TeX source code.
//! In particular, TeX has the following primitives which can change the lexing rules:
//!
//! - The `\catcode` primitive is used to change the category code
//! that is applied to each character in the input, and thus the kind
//! of token that each character gets converted to.
//!
//! - The `\endlinechar` primitive is used to change the character
//! that TeX appends to each line in the input file.
//! TeX appends this character after stripping the new line characters (`\n` or `\r\n`)
//! and any trailing space characters (` `, ASCII code 32) from the end of the line.
//!
//! The most important implication of TeX having dynamic lexing rules is that
//! the lexer is lazy, or "just in time".
//! One cannot, in general, run the lexer for a entire input file to obtain a list of tokens.
//! Instead one must request new tokens just as they are needed.
//! Here is an example of TeX source code that relies on this behavior:
//! ```tex
//! \def\Hello{The macro `Hello' was expanded.\par}
//! \def\HelloWorld{The macro `Hello World' was expanded.\par}
//! % change the category code of the character W to category code other
//! \catcode`\W = 12
//! \HelloWorld
//! ```
//! If the lexer were run over the whole source file at once, the last line
//! would be tokenized as the single control sequence `\HelloWorld`.
//! However the third line redefines the category code of `W` to other.
//! Because non-singleton control sequence names consist only of characters with the letter category code,
//! the last line is tokenized as the control sequence `\Hello`
//! followed by the other token `W` and then four letter tokens for `orld`.
//!
//! Due to this "just in time" behavior, the API for the Texlang lexer looks somewhat like a Rust iterator.
//! The next token is retrieved on-demand using the Lexer's [`next`](Lexer::next) method.
//!
//! ## Subtle lexing behavior
//!
//! Another implication of TeX's dynamic lexing rules is that the process of lexing is fragile
//! and susceptible to subtle bugs.
//! Consider, for example, the following TeX source code:
//! ```tex
//! A\endlinechar=`\X
//! B
//! C
//! ```
//! What is the output of this code?
//! One might expect that the end of line character will be `<return>` on the first line,
//! and `X` on the subsequent two lines, thus giving `A BXCX`.
//! However the result is actually `A B CX`!
//! The exact order of operations here is:
//!
//! 1. The `\endlinechar` control sequence is returned from the lexer, and the
//! primitive starts running.
//! 1. The optional `=` is parsed from the input.
//! 1. TeX starts parsing a number.
//! The first character from the lexer is `` ` ``, which indicates that
//! the number will be provided via a single character control sequence.
//! 1. The control sequence `\A` is then returned from the lexer.
//! At this point the lexer is at the end of line 1, and hasn't started the new line.
//! 1. Next, following TeX's rules for scanning numbers of the form `` `\A ``,
//! an optional space is parsed from the input.
//! See e.g. section 442 in TeX The Program.
//! Parsing this optional space triggers the lexer to return another token.
//! Because the current line is over, the lexer loads the next line and -- crucially -
//! uses the _current_ definition of the end of line character, which is `\r`.
//! 1. In this case it happens that there is no optional space.
//! So at this point the end of line character is changed to `X`.
//! At the end of the second line, this will be used when loading the third line.
//!
//! The lesson from this example is that the output of TeX source code
//! is dependent on the precise order of lexing operations.
//! It is very easy for implementations to get this wrong.
//! To minimize the chances of bugs in the Texlang lexer, its implementation
//! very closely follows the implementation of Knuth's TeX
//! (see sections 343 - 356 of TeX The Program).
//!
//! ## Using the Texlang lexer
//!
//! The Texlang lexer is used internally in the Texlang VM to read from input files,
//! but may also be used outside the VM in libraries.
//! For example, implementations of the `\openin`/`\read`
//! primitives also need to tokenize TeX source code.
//! For this reason the lexer is a public part of Texlang's API.
//!
//!
use crate::error;
use crate::token;
use crate::token::trace;
use crate::token::CsNameInterner;
use crate::token::Token;
use crate::types::CatCode;
use crate::vm;
/// Error possibly returned when the input contains an invalid character
#[derive(Debug)]
pub struct InvalidCharacterError {
/// The character. Its catcode is [`CatCode::Invalid`].
pub char: char,
/// Trace of the character.
pub trace: trace::SourceCodeTrace,
}
impl InvalidCharacterError {
/// Create a new invalid character error.
pub fn new<S: vm::TexlangState>(vm: &vm::VM<S>, char: char, trace_key: trace::Key) -> Self {
Self {
char,
trace: vm.trace(Token::new_letter(char, trace_key)),
}
}
}
impl error::TexError for InvalidCharacterError {
fn kind(&self) -> error::Kind {
error::Kind::Token(&self.trace)
}
fn title(&self) -> String {
format![
"input contains a character {} (Unicode code point {}) with category code {}",
self.char,
self.char as u32,
CatCode::Invalid
]
}
fn source_annotation(&self) -> String {
"invalid character".into()
}
fn notes(&self) -> Vec<error::display::Note> {
vec![format![
"characters with category code {} cannot appear in the input",
CatCode::Invalid
]
.into()]
}
}
/// Configuration for a specific instance of the [`Lexer`].
///
/// A Rust type implementing this trait is provided
/// when getting the next token from the lexer.
pub trait Config {
/// Return the current category code of the provided character.
fn cat_code(&self, c: char) -> CatCode;
/// Return the current end of line character.
fn end_line_char(&self) -> Option<char>;
}
/// Result of calling [Lexer::next].
pub enum Result {
/// A token.
Token(token::Token),
/// An invalid character appeared in the input.
InvalidCharacter(char, trace::Key),
/// The end of a line was reached.
///
/// This is only reported from [Lexer::next] if its `report_end_of_line` argument is true.
EndOfLine,
/// The end of input was reached.
EndOfInput,
}
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
enum State {
NewLine,
MidLine,
SkipBlanks,
}
/// The Texlang lexer
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Lexer {
raw_lexer: RawLexer,
state: State,
first_line_started: bool,
// We read control sequence names into a shared buffer to avoid allocating for each one.
#[cfg_attr(feature = "serde", serde(skip))]
buffer: String,
}
impl Lexer {
/// Create a new lexer.
pub fn new(source_code: String, trace_key_range: trace::KeyRange) -> Lexer {
Lexer {
raw_lexer: RawLexer::new(source_code, trace_key_range),
state: State::NewLine,
first_line_started: false,
buffer: Default::default(),
}
}
/// Get the next token.
pub fn next<C: Config>(
&mut self,
config: &C,
cs_name_interner: &mut CsNameInterner,
report_end_of_line: bool,
) -> Result {
loop {
let raw_token = match self.raw_lexer.next(config) {
None => {
self.state = State::NewLine;
if !self.raw_lexer.start_new_line(config) {
return Result::EndOfInput;
}
if report_end_of_line {
if self.first_line_started {
return Result::EndOfLine;
}
self.first_line_started = true;
}
continue;
}
Some(raw_token) => raw_token,
};
let c = raw_token.char;
let (value, next_state) = match raw_token.code {
CatCode::Escape => {
let (cs_name, new_state) = self.read_control_sequence(config, cs_name_interner);
(
Token::new_control_sequence(cs_name, raw_token.trace_key),
new_state,
)
}
CatCode::EndOfLine => {
self.raw_lexer.end_line();
match self.state {
State::NewLine => (
Token::new_control_sequence(
cs_name_interner.get_or_intern("par"),
raw_token.trace_key,
),
State::NewLine,
),
State::MidLine => {
(Token::new_space(' ', raw_token.trace_key), State::NewLine)
}
State::SkipBlanks => {
continue;
}
}
}
CatCode::Space => match self.state {
State::NewLine | State::SkipBlanks => continue,
State::MidLine => (
Token::new_space(' ', raw_token.trace_key),
State::SkipBlanks,
),
},
CatCode::BeginGroup => (
Token::new_begin_group(c, raw_token.trace_key),
State::MidLine,
),
CatCode::EndGroup => (Token::new_end_group(c, raw_token.trace_key), State::MidLine),
CatCode::MathShift => (
Token::new_math_shift(c, raw_token.trace_key),
State::MidLine,
),
CatCode::AlignmentTab => (
Token::new_alignment_tab(c, raw_token.trace_key),
State::MidLine,
),
CatCode::Parameter => {
(Token::new_parameter(c, raw_token.trace_key), State::MidLine)
}
CatCode::Superscript => {
if self
.raw_lexer
.maybe_apply_caret_notation(raw_token.char, true)
{
continue;
}
(
Token::new_superscript(c, raw_token.trace_key),
State::MidLine,
)
}
CatCode::Subscript => {
(Token::new_subscript(c, raw_token.trace_key), State::MidLine)
}
CatCode::Letter => (Token::new_letter(c, raw_token.trace_key), State::MidLine),
CatCode::Other => (Token::new_other(c, raw_token.trace_key), State::MidLine),
CatCode::Active => (
Token::new_active_character(c, raw_token.trace_key),
State::MidLine,
),
CatCode::Comment => {
self.raw_lexer.end_line();
continue;
}
CatCode::Ignored => {
continue;
}
CatCode::Invalid => {
return Result::InvalidCharacter(raw_token.char, raw_token.trace_key)
}
};
self.state = next_state;
return Result::Token(value);
}
}
/// Marks the input as ended.
///
/// Subsequent invocations of [`Lexer::next`] will return [`Result::EndOfInput`].
pub fn end(&mut self) {
self.raw_lexer.end()
}
fn read_control_sequence<F: Config>(
&mut self,
config: &F,
cs_name_interner: &mut CsNameInterner,
) -> (token::CsName, State) {
self.buffer.clear();
let first_raw_token = match self.raw_lexer.next(config) {
None => return (cs_name_interner.get_or_intern(""), State::NewLine),
Some(first_raw_token) => first_raw_token,
};
match first_raw_token.code {
CatCode::Letter => {
self.buffer.push(first_raw_token.char);
while let Some(raw_token) = self.raw_lexer.peek(config) {
match raw_token.code {
CatCode::Letter => {
self.raw_lexer.advance();
self.buffer.push(raw_token.char);
}
CatCode::Superscript => {
if self
.raw_lexer
.maybe_apply_caret_notation(raw_token.char, false)
{
continue;
}
break;
}
_ => break,
}
}
}
CatCode::Superscript => {
if self
.raw_lexer
.maybe_apply_caret_notation(first_raw_token.char, true)
{
return self.read_control_sequence(config, cs_name_interner);
}
self.buffer.push(first_raw_token.char);
}
_ => {
self.buffer.push(first_raw_token.char);
}
};
let new_state = match first_raw_token.code {
CatCode::Letter | CatCode::Space => State::SkipBlanks,
_ => State::MidLine,
};
(cs_name_interner.get_or_intern(&self.buffer), new_state)
}
}
#[derive(Debug)]
struct RawToken {
code: CatCode,
char: char,
trace_key: trace::Key,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug)]
struct RawLexer {
// TODO: when serializing we only need to serialize self.source_code[self.pos..]
source_code: String,
current_line: String,
pos: usize,
next_line: usize,
// Number of characters that were trimmed from the right side of the current line.
// End line char is factored in here.
// This is used to calculate the correct trace key for the first character in a new line.
num_trimmed_right: usize,
trace_key_range: trace::KeyRange,
}
impl RawLexer {
fn new(source_code: String, trace_key_range: trace::KeyRange) -> RawLexer {
RawLexer {
source_code,
current_line: "".into(),
pos: 0,
next_line: 0,
num_trimmed_right: 0,
trace_key_range,
}
}
fn end_line(&mut self) {
let num_skipped_chars: usize = self.current_line[self.pos..].chars().count();
self.trace_key_range.advance_by(num_skipped_chars);
self.pos = self.current_line.len();
}
/// Start a new line in the input. Returns true if and only if there is another line to read.
fn start_new_line<C: Config>(&mut self, config: &C) -> bool {
let num_skipped_chars: usize = self.current_line[self.pos..].chars().count();
self.trace_key_range.advance_by(num_skipped_chars);
self.trace_key_range.advance_by(self.num_trimmed_right);
self.pos = 0;
self.current_line.clear();
if self.next_line >= self.source_code.len() {
return false;
}
let start = self.next_line;
let mut end = self.next_line;
let mut num_spaces = 0;
for c in self.source_code[self.next_line..].chars() {
if c == '\n' {
// TODO: if end_line_char = None and start==end
// we should continue scanning? This ensures that after this function is invoked
// either the input is ended or the current line is non empty.
// Only thing is we have to be mindful of is the eventual \read use case where signaling
// on every line end may be needed.
num_spaces += 1;
break;
}
if c != ' ' {
end += c.len_utf8();
end += num_spaces;
num_spaces = 0;
}
if c == ' ' {
num_spaces += 1;
}
}
self.num_trimmed_right = num_spaces;
self.next_line = end + num_spaces;
self.current_line.push_str(&self.source_code[start..end]);
if let Some(end_line_char) = config.end_line_char() {
// num_spaces may be 0 if this is the end of the file. In this case num_trimmed_right
// because this is the last line, so we do a safe saturating_sub.
self.num_trimmed_right = self.num_trimmed_right.saturating_sub(1);
self.current_line.push(end_line_char)
}
true
}
#[inline]
fn next<C: Config>(&mut self, config: &C) -> Option<RawToken> {
match self.next_char() {
Some(c) => {
self.pos += c.len_utf8();
Some(RawToken {
char: c,
code: config.cat_code(c),
trace_key: self.trace_key_range.next(),
})
}
None => None,
}
}
fn peek<C: Config>(&mut self, config: &C) -> Option<RawToken> {
match self.next_char() {
Some(c) => {
let code = config.cat_code(c);
Some(RawToken {
char: c,
code,
trace_key: self.trace_key_range.peek(),
})
}
None => None,
}
}
fn maybe_apply_caret_notation(&mut self, char_1: char, char_1_consumed: bool) -> bool {
let char_2_start = if char_1_consumed {
self.pos
} else {
self.pos + char_1.len_utf8()
};
let char_2 = match self.current_line[char_2_start..].chars().next() {
// If the line is over, don't transform.
// This is what TeX does; see the TeXBook section 355 and related sections.
None => return false,
Some(next_char) => next_char,
};
if char_2 != char_1 {
return false;
}
let char_3_start = char_2_start + char_2.len_utf8();
let char_3 = match self.current_line[char_3_start..].chars().next() {
// If the line is over, don't transform.
// This is what TeX does; see the TeXBook section 355 and related sections.
None => return false,
Some(c) => c,
};
if !char_1_consumed {
self.advance();
}
self.advance();
if !char_3.is_ascii() {
return true;
}
let u: u8 = match (char_3 as u32).try_into() {
Ok(u) => u,
Err(_) => return true, // unreachable because char_3 is ASCII
};
let m = match u {
0x00..=0x3F => u + 0x40,
0x40..=0x7F => u - 0x40,
_ => return true, // unreachable because char_3 is ASCII
};
// Given the way `m` is calculated, we're guaranteed that it is ASCII. However,
// this assert is to have defense-in-depth in light of the unsafe block next.
assert!(char::from_u32(m as u32).unwrap().is_ascii());
// SAFETY: the original character `char_3` is single-byte/ASCII and
// the replacement character `m` is single-byte/ASCII so the replacement
// preserves the UTF-8 structure of the string.
unsafe {
self.current_line.as_bytes_mut()[self.pos] = m;
}
true
}
fn next_char(&self) -> Option<char> {
self.current_line[self.pos..].chars().next()
}
fn advance(&mut self) {
self.pos += self.current_line[self.pos..]
.chars()
.next()
.unwrap()
.len_utf8();
self.trace_key_range.next();
}
fn end(&mut self) {
// This ends the current line, so on the next token request a new line will be created.
self.pos = self.current_line.len();
// This marks the input as having no additional lines, so the new line request will return
// and end of input marker.
self.next_line = self.source_code.len();
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::token::CommandRef;
use crate::token::Value;
use crate::types::CatCode::*;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq)]
enum TokenValue<'a> {
Character(char, CatCode, u32),
ControlSequence(&'a str, u32),
NewLine,
}
use TokenValue::*;
impl<'a> TokenValue<'a> {
fn new(token: Token, interner: &'a CsNameInterner) -> TokenValue<'a> {
let trace_key = token.trace_key().as_u32();
if let Value::CommandRef(CommandRef::ControlSequence(cs_name)) = token.value() {
TokenValue::ControlSequence(interner.resolve(cs_name).unwrap(), trace_key)
} else {
TokenValue::Character(token.char().unwrap(), token.cat_code().unwrap(), trace_key)
}
}
}
struct TestConfig {
cat_codes: HashMap<char, CatCode>,
end_line_char: Option<char>,
}
impl Config for TestConfig {
fn cat_code(&self, c: char) -> CatCode {
self.cat_codes.get(&c).copied().unwrap_or_default()
}
fn end_line_char(&self) -> Option<char> {
self.end_line_char
}
}
fn lexer_test(
input: &str,
expected_tokens: Vec<TokenValue>,
end_line_char: Option<char>,
cat_code_overrides: Vec<(char, CatCode)>,
) {
let mut cat_codes: HashMap<char, CatCode> = CatCode::PLAIN_TEX_DEFAULTS
.iter()
.enumerate()
.map(|(a, b)| (char::from_u32(a.try_into().unwrap()).unwrap(), *b))
.collect();
for (c, cat_code) in cat_code_overrides {
cat_codes.insert(c, cat_code);
}
let config = TestConfig {
cat_codes,
end_line_char,
};
let mut cs_name_interner: CsNameInterner = Default::default();
let mut actual = Vec::new();
let mut lexer = Lexer::new(input.into(), trace::KeyRange::for_testing());
loop {
let next = lexer.next(&config, &mut cs_name_interner, true);
if let Result::EndOfInput = next {
break;
}
actual.push(next);
}
let actual: Vec<TokenValue<'_>> = actual
.into_iter()
.map(|t| match t {
Result::Token(t) => TokenValue::new(t, &cs_name_interner),
Result::InvalidCharacter(_, _) => panic!("invalid character"),
Result::EndOfLine => TokenValue::NewLine,
Result::EndOfInput => unreachable!(),
})
.collect();
assert_eq!(expected_tokens, actual);
}
macro_rules! lexer_tests {
(
end_line_char( $end_line_char: expr ),
cat_code_overrides $cat_code_overrides: tt,
$( ( $name: ident, $input: expr, $ ( $expected_token : expr, ) * ), )+
) => {
$(
#[test]
fn $name() {
let end_line_char = $end_line_char;
let cat_code_overrides = vec! $cat_code_overrides;
let input = $input;
let expected_tokens = vec!( $( $expected_token ),* );
lexer_test(&input, expected_tokens, end_line_char, cat_code_overrides);
}
)+
};
}
lexer_tests![
end_line_char(Some('\r')),
cat_code_overrides(),
(empty_1, r"",),
(empty_2, "\n", ControlSequence("par", 0),),
(
control_sequence_basic_1,
r"\a{b}",
ControlSequence("a", 0),
Character('{', BeginGroup, 2),
Character('b', Letter, 3),
Character('}', EndGroup, 4),
Character(' ', Space, 5),
),
(
control_sequence_basic_2,
r"\A1",
ControlSequence("A", 0),
Character('1', Other, 2),
Character(' ', Space, 3),
),
(
control_sequence_single_letter_trailing_space_1,
r"\a b",
ControlSequence("a", 0),
Character('b', Letter, 3),
Character(' ', Space, 4),
),
(
control_sequence_single_letter_trailing_space_2,
r"\a b",
ControlSequence("a", 0),
Character('b', Letter, 4),
Character(' ', Space, 5),
),
(
control_sequence_single_letter_trailing_newline_1,
"\\a\n b",
ControlSequence("a", 0),
NewLine,
Character('b', Letter, 4),
Character(' ', Space, 5),
),
(
control_sequence_single_letter_trailing_newline_2,
"\\a\n\nb",
ControlSequence("a", 0),
NewLine,
ControlSequence("par", 3),
NewLine,
Character('b', Letter, 4),
Character(' ', Space, 5),
),
(
control_sequence_multi_letter_1,
"\\ABC{D}",
ControlSequence("ABC", 0),
Character('{', BeginGroup, 4),
Character('D', Letter, 5),
Character('}', EndGroup, 6),
Character(' ', Space, 7),
),
(
control_sequence_multi_letter_2,
"\\ABC",
ControlSequence("ABC", 0),
),
(
control_sequence_single_other_1,
"\\{{",
ControlSequence("{", 0),
Character('{', BeginGroup, 2),
Character(' ', Space, 3),
),
(
control_sequence_single_other_2,
"\\+A",
ControlSequence("+", 0),
Character('A', Letter, 2),
Character(' ', Space, 3),
),
(
control_sequence_single_other_trailing_space,
"\\+ A",
ControlSequence("+", 0),
Character(' ', Space, 2),
Character('A', Letter, 3),
Character(' ', Space, 4),
),
(
control_sequence_single_space_trailing_space,
"\\ A",
ControlSequence(" ", 0),
Character('A', Letter, 3),
Character(' ', Space, 4),
),
(
comment_1,
"A%B\nC",
Character('A', Letter, 0),
NewLine,
Character('C', Letter, 4),
Character(' ', Space, 5),
),
(
comment_1_with_space,
"A%B \nC",
Character('A', Letter, 0),
NewLine,
Character('C', Letter, 5),
Character(' ', Space, 6),
),
(
comment_2,
"A%B\n%C\nD",
Character('A', Letter, 0),
NewLine,
NewLine,
Character('D', Letter, 7),
Character(' ', Space, 8),
),
(comment_3, "A%a comment here", Character('A', Letter, 0),),
(
comment_4,
"A%\n B",
Character('A', Letter, 0),
NewLine,
Character('B', Letter, 4),
Character(' ', Space, 5),
),
(
comment_5,
"A%\n\n B",
Character('A', Letter, 0),
NewLine,
ControlSequence("par", 3),
NewLine,
Character('B', Letter, 5),
Character(' ', Space, 6),
),
(
comment_6,
"\\A %\nB",
ControlSequence("A", 0),
NewLine,
Character('B', Letter, 5),
Character(' ', Space, 6),
),
(
texbook_exercise_8_2_e,
"A%\n B%",
Character('A', Letter, 0),
NewLine,
Character('B', Letter, 4),
),
(
texbook_exercise_8_4,
r" $x^2$~ \Tex ^^C",
Character('$', MathShift, 1),
Character('x', Letter, 2),
Character('^', Superscript, 3),
Character('2', Other, 4),
Character('$', MathShift, 5),
Character('~', Active, 6),
Character(' ', Space, 7),
ControlSequence("Tex", 8),
Character('\u{3}', Other, 15),
Character(' ', Space, 16),
),
(
texbook_exercise_8_5,
"Hi!\n\n\n",
Character('H', Letter, 0),
Character('i', Letter, 1),
Character('!', Other, 2),
Character(' ', Space, 3),
NewLine,
ControlSequence("par", 4),
NewLine,
ControlSequence("par", 5),
),
(
double_space_creates_one_space,
"A B",
Character('A', Letter, 0),
Character(' ', Space, 1),
Character('B', Letter, 3),
Character(' ', Space, 4),
),
(
single_newline_creates_one_space,
"A\nB",
Character('A', Letter, 0),
Character(' ', Space, 1),
NewLine,
Character('B', Letter, 2),
Character(' ', Space, 3),
),
(
space_and_newline_creates_space,
"A \nB",
Character('A', Letter, 0),
Character(' ', Space, 1),
NewLine,
Character('B', Letter, 3),
Character(' ', Space, 4),
),
(
par_1,
"A\n\nB",
Character('A', Letter, 0),
Character(' ', Space, 1),
NewLine,
ControlSequence("par", 2),
NewLine,
Character('B', Letter, 3),
Character(' ', Space, 4),
),
(
par_2,
"A\n \nB",
Character('A', Letter, 0),
Character(' ', Space, 1),
NewLine,
ControlSequence("par", 2),
NewLine,
Character('B', Letter, 4),
Character(' ', Space, 5),
),
(
par_3,
"A\n\n\nB",
Character('A', Letter, 0),
Character(' ', Space, 1),
NewLine,
ControlSequence("par", 2),
NewLine,
ControlSequence("par", 3),
NewLine,
Character('B', Letter, 4),
Character(' ', Space, 5),
),
(
caret_notation_1,
"^^k",
Character('+', Other, 2),
Character(' ', Space, 3),
),
(
caret_notation_2,
"^^+",
Character('k', Letter, 2),
Character(' ', Space, 3),
),
(
caret_notation_3,
"^^+m",
Character('k', Letter, 2),
Character('m', Letter, 3),
Character(' ', Space, 4),
),
(caret_notation_4, "^^\n", Character('M', Letter, 2),),
(caret_notation_5, "^^", Character('M', Letter, 2),),
(
caret_notation_6,
"^^\nA",
Character('M', Letter, 2),
NewLine,
Character('A', Letter, 3),
Character(' ', Space, 4),
),
(
caret_notation_recursive_1,
"^^\u{1E}^+",
Character('k', Letter, 4),
Character(' ', Space, 5),
),
(
caret_notation_recursive_2,
"\\^^\u{1E}^+",
ControlSequence("k", 0),
),
(
caret_notation_recursive_3,
"\\j^^\u{1E}^+",
ControlSequence("jk", 0),
),
(
// This test case triggers a recursive call to `new_control_sequence`.
caret_notation_recursive_4,
format!["\\^^{}+", "\u{1E}^".repeat(200)],
ControlSequence("k", 0),
),
(
caret_notation_end_of_input_1,
"^^",
Character('M', Letter, 2),
),
(
caret_notation_end_of_input_2,
"\\^^",
ControlSequence("M", 0),
),
(
caret_notation_end_of_input_3,
"\\a^^",
ControlSequence("aM", 0),
),
(
caret_notation_boundary_1,
"^^\u{00}",
Character(char::from_u32(0x40).unwrap(), Other, 2),
Character(' ', Space, 3),
),
(
caret_notation_boundary_3,
"^^\u{40}",
// skipped character
ControlSequence("par", 3),
),
(
caret_notation_boundary_4,
"^^\u{7F}",
Character(char::from_u32(0x3F).unwrap(), Other, 2),
Character(' ', Space, 3),
),
(
caret_notation_cs_1,
r"\^^m",
ControlSequence("-", 0),
Character(' ', Space, 4),
),
(
caret_notation_cs_2,
r"\^^ma",
ControlSequence("-", 0),
Character('a', Letter, 4),
Character(' ', Space, 5),
),
(caret_notation_cs_3, r"\^^-", ControlSequence("m", 0),),
(caret_notation_cs_4, r"\^^-a", ControlSequence("ma", 0),),
(
caret_notation_cs_5,
r"\^^-^^-+",
ControlSequence("mm", 0),
Character('+', Other, 7),
Character(' ', Space, 8),
),
(caret_notation_cs_6, r"\a^^-", ControlSequence("am", 0),),
(
caret_notation_cs_7,
"\\^a",
ControlSequence("^", 0),
Character('a', Letter, 2),
Character(' ', Space, 3),
),
(
caret_notation_cs_8,
"\\a^a",
ControlSequence("a", 0),
Character('^', Superscript, 2),
Character('a', Letter, 3),
Character(' ', Space, 4),
),
];
lexer_tests![
end_line_char(Some('\r')),
cat_code_overrides(('Z', Ignored)),
(
control_sequence_single_ignored,
r"\Z",
ControlSequence("Z", 0),
Character(' ', Space, 2),
),
(ignored_character_1, "Z", ControlSequence("par", 1),),
(
ignored_character_2,
"AZB",
Character('A', Letter, 0),
Character('B', Letter, 2),
Character(' ', Space, 3),
),
(
texbook_exercise_8_2_f,
r"\AZB",
ControlSequence("A", 0),
Character('B', Letter, 3),
Character(' ', Space, 4),
),
];
lexer_tests![
end_line_char(Some('\r')),
cat_code_overrides(('W', Invalid)),
(
control_sequence_single_invalid,
r"\W",
ControlSequence("W", 0),
Character(' ', Space, 2),
),
];
lexer_tests![
end_line_char(Some('\r')),
cat_code_overrides(('X', EndOfLine)),
(
non_standard_newline_character,
"AXB",
Character('A', Letter, 0),
Character(' ', Space, 1),
),
(
non_standard_newline_character_2,
"AXXB",
Character('A', Letter, 0),
Character(' ', Space, 1),
),
(
non_standard_newline_character_after_cs,
r"\A XB",
ControlSequence("A", 0),
),
(single_non_standard_newline, "X", ControlSequence("par", 0),),
];
lexer_tests![
end_line_char(Some('\r')),
cat_code_overrides(('Y', Space)),
(
non_standard_whitespace_1,
"AYB",
Character('A', Letter, 0),
Character(' ', Space, 1),
Character('B', Letter, 2),
Character(' ', Space, 3),
),
];
lexer_tests![
end_line_char(Some('\r')),
cat_code_overrides(
('\u{01}', Escape),
('\u{02}', Superscript),
('\u{03}', Space),
('\u{0D}', Letter),
),
(
texbook_exercise_8_6,
r"^^B^^BM^^A^^B^^C^^M^^@\M ",
Character('\u{02}', Superscript, 2),
Character('\u{02}', Superscript, 5),
Character('M', Letter, 6),
ControlSequence("\u{02}", 9),
Character(' ', Space, 15),
Character('\u{0D}', Letter, 18),
ControlSequence("M\u{0D}", 22),
),
];
lexer_tests![
end_line_char(Some('B')),
cat_code_overrides(),
(
control_sequence_includes_end_line_char_1,
r"\A",
ControlSequence("AB", 0),
),
(
control_sequence_includes_end_line_char_2,
r"\A ",
ControlSequence("AB", 0),
),
(
control_sequence_includes_end_line_char_3,
r"\",
ControlSequence("B", 0),
),
(
control_sequence_includes_end_line_char_4,
r"\ ",
ControlSequence("B", 0),
),
(
control_sequence_does_not_span_lines,
"\\A\nC",
ControlSequence("AB", 0),
NewLine,
Character('C', Letter, 3),
Character('B', Letter, 4),
),
(
repeated_end_line_char_1,
"\n\n\n",
Character('B', Letter, 0),
NewLine,
Character('B', Letter, 1),
NewLine,
Character('B', Letter, 2),
),
(
repeated_end_line_char_2,
"A\nA\nA\n",
Character('A', Letter, 0),
Character('B', Letter, 1),
NewLine,
Character('A', Letter, 2),
Character('B', Letter, 3),
NewLine,
Character('A', Letter, 4),
Character('B', Letter, 5),
),
(
right_side_trimming,
"A \nA \n",
Character('A', Letter, 0),
Character('B', Letter, 1),
NewLine,
Character('A', Letter, 4),
Character('B', Letter, 5),
),
(
left_side_trimming,
"A\n A\n",
Character('A', Letter, 0),
Character('B', Letter, 1),
NewLine,
Character('A', Letter, 3),
Character('B', Letter, 4),
),
];
lexer_tests![
end_line_char(None),
cat_code_overrides(),
(
multiple_skipped_lines,
"A\n\n\nB",
Character('A', Letter, 0),
NewLine,
NewLine,
NewLine,
Character('B', Letter, 4),
),
(
empty_cs_name,
"\\\nB",
ControlSequence("", 0),
NewLine,
Character('B', Letter, 2),
),
];
}