Deduplicate native dependency reads on the execution path

This commit is contained in:
gloamxun committed 2026-11-29 04:46:22 +08:00
1 parent c23dabb449
commit 4791c42403
9 files changed
+139 -38

No files matched your search

+20 -20
View File
@@ -1,5 +1,5 @@
use super::{CatCode, CommandName, Dependency, Span, State, Token, TokenKind};
use std::{collections::BTreeSet, sync::Arc};
use super::{CatCode, CommandName, Dependency, DependencyReads, Span, State, Token, TokenKind};
use std::sync::Arc;
use super::xe_encoding::{Character, Encoding};
@@ -18,7 +18,7 @@ mod tests {
fn tokens(text: &str) -> Vec<Token> {
let mut mouth = Mouth::new(0, text.into(), super::super::InputProfile::UnicodeScalar);
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
let mut tokens = Vec::new();
while let Some(token) = mouth.next(&State::default(), &mut reads).unwrap() {
tokens.push(token);
@@ -52,7 +52,7 @@ mod tests {
let mut mouth = Mouth::new(0, "\\~ A\\ B".into(), super::super::InputProfile::TeX8Bit);
let mut state = State::default();
state.assign_catcode('~', CatCode::Space, false);
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
let mut kinds = Vec::new();
while let Some(token) = mouth.next(&state, &mut reads).unwrap() {
kinds.push(token.kind);
@@ -77,7 +77,7 @@ mod tests {
super::super::InputProfile::UnicodeScalar,
);
let mut state = State::default();
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
assert_eq!(
mouth.next(&state, &mut reads).unwrap().unwrap().kind,
TokenKind::Command(CommandName::Control("set".into()))
@@ -96,7 +96,7 @@ mod tests {
super::super::InputProfile::UnicodeScalar,
);
let mut state = State::default();
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
assert_eq!(
mouth.next(&state, &mut reads).unwrap().unwrap().kind,
TokenKind::Character('A', CatCode::Letter)
@@ -122,7 +122,7 @@ mod tests {
state.assign_catcode(mark, CatCode::Letter, false);
}
let mut mouth = Mouth::new(0, "é\n\\a\u{0315}\u{0300}!\na\u{0315}\u{0300}é".into(), InputProfile::XeTeX);
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
let first = mouth.next(&state, &mut reads).unwrap().unwrap();
assert!(matches!(first.kind, TokenKind::Character('é', _)));
assert_eq!((first.span.start, first.span.end), (0, 2));
@@ -142,7 +142,7 @@ mod tests {
assert!(matches!(token.kind, TokenKind::Character(c, _) if c == value));
assert_eq!((token.span.start, token.span.end), range);
}
assert!(reads.contains(&Dependency::Parameter(Parameter::XeTeXinputnormalization)));
assert!(reads.into_ordered().contains(&Dependency::Parameter(Parameter::XeTeXinputnormalization)));
}
#[test]
@@ -154,7 +154,7 @@ mod tests {
);
let mut state = State::default();
state.assign_integer(super::super::state::Key::Endlinechar, -1, false);
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
assert_eq!(
mouth.next(&state, &mut reads).unwrap().unwrap().kind,
TokenKind::Command(CommandName::Control("word".into()))
@@ -191,7 +191,7 @@ mod tests {
state.assign_catcode('中', CatCode::Letter, false);
state.assign_catcode('😀', CatCode::Letter, false);
let mut mouth = Mouth::new(0, text.into(), InputProfile::XeTeX);
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
let prime = mouth.next(&state, &mut reads).unwrap().unwrap();
assert_eq!(prime.kind, TokenKind::Character('′', state.catcode('′')));
assert_eq!((prime.span.start, prime.span.end), (0, 8));
@@ -230,7 +230,7 @@ mod tests {
.collect::<Vec<_>>()
};
let state = State::default().with_input_profile(InputProfile::XeTeX);
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
let mut mouth = Mouth::new(8, utf16("A\r\n😀\r\n"), InputProfile::XeTeX);
let first = mouth.next(&state, &mut reads).unwrap().unwrap();
assert_eq!(first.span, Span { source: 8, start: 2, end: 4 });
@@ -442,7 +442,7 @@ impl Mouth {
pub(super) fn load_line(
&mut self,
state: &State,
reads: &mut BTreeSet<Dependency>,
reads: &mut DependencyReads,
) -> Result<bool, (Span, String)> {
if self.end_after_line || self.offset == self.data.len() {
return Ok(false);
@@ -538,7 +538,7 @@ impl Mouth {
fn load_xe_line(
&mut self,
state: &State,
reads: &mut BTreeSet<Dependency>,
reads: &mut DependencyReads,
) -> Result<bool, (Span, String)> {
let xe = self.xe.as_mut().unwrap();
let framing = xe.encoding.framing();
@@ -578,7 +578,7 @@ impl Mouth {
content_end: usize,
end: usize,
state: &State,
reads: &mut BTreeSet<Dependency>,
reads: &mut DependencyReads,
) {
reads.insert(Dependency::EndLineChar);
if let Ok(value) = u32::try_from(state.endlinechar)
@@ -594,8 +594,8 @@ impl Mouth {
self.cursor = 0;
self.state = LineState::New;
}
fn catcode(state: &State, reads: &mut BTreeSet<Dependency>, c: char) -> CatCode {
reads.insert(Dependency::CatCode(c));
fn catcode(state: &State, reads: &mut DependencyReads, c: char) -> CatCode {
reads.catcode(c);
state.catcode(c)
}
// next points at the second superscript mark. The returned length
@@ -640,7 +640,7 @@ 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 BTreeSet<Dependency>) -> Option<Character> {
fn peek(&mut self, state: &State, reads: &mut DependencyReads) -> Option<Character> {
loop {
let c = *self.line.get(self.cursor)?;
if Self::catcode(state, reads, c.value) != CatCode::Superscript {
@@ -659,7 +659,7 @@ 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 BTreeSet<Dependency>) -> Character {
fn next_character(&mut self, state: &State, reads: &mut DependencyReads) -> Character {
let mut c = self.line[self.cursor];
self.cursor += 1;
while Self::catcode(state, reads, c.value) == CatCode::Superscript {
@@ -677,7 +677,7 @@ impl Mouth {
pub fn next(
&mut self,
state: &State,
reads: &mut BTreeSet<Dependency>,
reads: &mut DependencyReads,
) -> Result<Option<Token>, (Span, String)> {
loop {
while self.cursor == self.line.len() {
@@ -777,7 +777,7 @@ mod byte_tests {
}
state.assign_integer(crate::execution::state::Key::Endlinechar, -1, false);
let mut mouth = Mouth::new(7, (0..=255u8).collect(), InputProfile::TeX8Bit);
let mut reads = BTreeSet::new();
let mut reads = DependencyReads::default();
for b in (0..=255u8).filter(|b| !matches!(b, b'\r' | b'\n')) {
let t = mouth.next(&state, &mut reads).unwrap().unwrap();
assert_eq!(t.kind, TokenKind::Character(char::from(b), CatCode::Other));