Reuse resolved character categories within each native scan

This commit is contained in:
gloamxun committed 2026-11-29 05:01:56 +08:00
1 parent 4791c42403
commit 48d42052b8
2 files changed
+62 -32

No files matched your search

+32 -32
View File
@@ -71,22 +71,23 @@ mod tests {
#[test]
fn unread_character_category_changes_after_control_word_lookahead() {
let mut mouth = Mouth::new(
0,
"\\set!".into(),
super::super::InputProfile::UnicodeScalar,
);
let mut state = State::default();
let mut reads = DependencyReads::default();
assert_eq!(
mouth.next(&state, &mut reads).unwrap().unwrap().kind,
TokenKind::Command(CommandName::Control("set".into()))
);
state.assign_catcode('!', CatCode::Active, false);
assert_eq!(
mouth.next(&state, &mut reads).unwrap().unwrap().kind,
TokenKind::Command(CommandName::Active('!'))
);
for text in ["\\set!", "\\set^^21"] {
let mut mouth = Mouth::new(
0,
text.into(),
super::super::InputProfile::UnicodeScalar,
);
let mut state = State::default();
let mut reads = DependencyReads::default();
assert_eq!(
mouth.next(&state, &mut reads).unwrap().unwrap().kind,
TokenKind::Command(CommandName::Control("set".into()))
);
state.assign_catcode('!', CatCode::Active, false);
let next = mouth.next(&state, &mut reads).unwrap().unwrap();
assert_eq!(next.kind, TokenKind::Command(CommandName::Active('!')));
assert_eq!((next.span.start, next.span.end), (4, text.len()));
}
}
#[test]
fn endlinechar_is_frozen_when_the_physical_line_is_loaded() {
@@ -640,14 +641,15 @@ impl Mouth {
/// Decode TeX's ^^ form before category lookup. Category changes affect
/// unread characters; tokens already in macro bodies are never retokenized.
fn peek(&mut self, state: &State, reads: &mut DependencyReads) -> Option<Character> {
fn peek(&mut self, state: &State, reads: &mut DependencyReads) -> Option<(Character, CatCode)> {
loop {
let c = *self.line.get(self.cursor)?;
if Self::catcode(state, reads, c.value) != CatCode::Superscript {
return Some(c);
let code = Self::catcode(state, reads, c.value);
if code != CatCode::Superscript {
return Some((c, code));
}
let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor + 1) else {
return Some(c);
return Some((c, code));
};
let mut replacement = Character { value, ..c };
for &part in &self.line[self.cursor + 1..=self.cursor + extra] {
@@ -659,12 +661,16 @@ impl Mouth {
/// get_next's ordinary ^^ conversion changes cur_chr and advances loc.
/// Only the control-sequence scanner rewrites the input buffer (peek).
/// Keeping these paths distinct also preserves TeX's displayed context.
fn next_character(&mut self, state: &State, reads: &mut DependencyReads) -> Character {
fn next_character(&mut self, state: &State, reads: &mut DependencyReads) -> (Character, CatCode) {
let mut c = self.line[self.cursor];
self.cursor += 1;
while Self::catcode(state, reads, c.value) == CatCode::Superscript {
loop {
let code = Self::catcode(state, reads, c.value);
if code != CatCode::Superscript {
return (c, code);
}
let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor) else {
break;
return (c, code);
};
c.value = value;
for &part in &self.line[self.cursor..self.cursor + extra] {
@@ -672,7 +678,6 @@ impl Mouth {
}
self.cursor += extra;
}
c
}
pub fn next(
&mut self,
@@ -685,13 +690,12 @@ impl Mouth {
return Ok(None);
}
}
let c = self.next_character(state, reads);
let (c, code) = self.next_character(state, reads);
let mut span = Span {
source: self.source,
start: c.start,
end: c.end,
};
let code = Self::catcode(state, reads, c.value);
let kind = match code {
CatCode::Ignored => continue,
CatCode::Invalid => {
@@ -726,17 +730,13 @@ impl Mouth {
}
CatCode::Escape => {
let mut name = String::new();
if let Some(next) = self.peek(state, reads) {
if let Some((next, code)) = self.peek(state, reads) {
self.cursor += 1;
span.start = span.start.min(next.start);
span.end = span.end.max(next.end);
name.push(next.value);
let code = Self::catcode(state, reads, next.value);
if code == CatCode::Letter {
while let Some(next) = self.peek(state, reads) {
if Self::catcode(state, reads, next.value) != CatCode::Letter {
break;
}
while let Some((next, CatCode::Letter)) = self.peek(state, reads) {
name.push(next.value);
span.start = span.start.min(next.start);
span.end = span.end.max(next.end);