Scan extended XeTeX superscript character notation

This commit is contained in:
gloamxun committed 2026-11-21 14:04:00 +08:00
1 parent 2bd6e19519
commit 01ef302fb0
2 files changed
+107 -51

No files matched your search

+87 -50
View File
@@ -146,6 +146,44 @@ mod tests {
.any(|t| t.kind == TokenKind::Character('!', CatCode::Other))
);
}
#[test]
fn xetex_extended_superscripts_preserve_spans_in_characters_and_control_words() {
use super::super::InputProfile;
let text = r"^^^^2032 \wide^^^^4e2d^^^^^^01f600 ^^41 ^^^041 ^^^^^1f600";
let mut state = State::default().with_input_profile(InputProfile::XeTeX);
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 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));
assert!(
mouth
.next(&state, &mut reads)
.unwrap()
.unwrap()
.kind
.is_space()
);
let word = mouth.next(&state, &mut reads).unwrap().unwrap();
let name = r"\wide^^^^4e2d^^^^^^01f600";
assert_eq!(
word.kind,
TokenKind::Command(CommandName::Control("wide中😀".into()))
);
assert_eq!(&text[word.span.start..word.span.end], name);
for expected in ['A', 'A', '😀'] {
let token = loop {
let token = mouth.next(&state, &mut reads).unwrap().unwrap();
if !token.kind.is_space() {
break token;
}
};
assert_eq!(token.kind, TokenKind::Character(expected, CatCode::Letter));
}
}
#[test]
fn physical_line_endings_and_utf8_byte_offsets_are_preserved() {
let result = tokens("é界 \r\nB\rC");
@@ -380,38 +418,60 @@ impl Mouth {
reads.insert(Dependency::CatCode(c));
state.catcode(c)
}
// next points at the second superscript mark. The returned length
// includes that mark and all remaining characters in the encoding.
fn expanded_superscript(&self, mark: char, next: usize) -> Option<(char, usize)> {
if self.line.get(next)?.value != mark {
return None;
}
let single = self.line.get(next + 1)?.value;
let mut count = 2;
if self.profile == super::InputProfile::XeTeX {
// XeTeX counts matching marks while the current encoding still
// has enough physical line slots. It supports up to six digits.
while count < 6
&& next + 2 * count - 2 < self.line.len()
&& self.line[next + count - 1].value == mark
{
count += 1;
}
}
let first_digit = next + count - 1;
let hex = self
.line
.get(first_digit..first_digit + count)
.and_then(|digits| {
digits.iter().try_fold(0, |value, digit| {
let digit = match digit.value {
'0'..='9' => digit.value as u32 - u32::from('0'),
'a'..='f' => digit.value as u32 - u32::from('a') + 10,
_ => return None,
};
Some(value * 16 + digit)
})
});
if let Some(code) = hex {
return char::from_u32(code).map(|value| (value, 2 * count - 1));
}
single
.is_ascii()
.then(|| (char::from(single as u8 ^ 64), 2))
}
/// 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> {
loop {
let c = *self.line.get(self.cursor)?;
if Self::catcode(state, reads, c.value) != CatCode::Superscript
|| self.line.get(self.cursor + 1).map(|c| c.value) != Some(c.value)
{
if Self::catcode(state, reads, c.value) != CatCode::Superscript {
return Some(c);
}
let Some(next) = self.line.get(self.cursor + 2).copied() else {
let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor + 1) else {
return Some(c);
};
let hex = |c: char| {
if c.is_ascii_digit() || ('a'..='f').contains(&c) {
c.to_digit(16)
} else {
None
}
};
let (value, consumed) = if let Some(a) = hex(next.value)
&& let Some(b) = self.line.get(self.cursor + 3).and_then(|c| hex(c.value))
{
(char::from_u32(16 * a + b).unwrap(), 4)
} else if (next.value as u32) < 128 {
(char::from_u32((next.value as u32) ^ 64).unwrap(), 3)
} else {
return Some(c);
};
let end = self.line[self.cursor + consumed - 1].end;
let end = self.line[self.cursor + extra].end;
self.line.splice(
self.cursor..self.cursor + consumed,
self.cursor..=self.cursor + extra,
[Character {
value,
start: c.start,
@@ -425,38 +485,15 @@ impl Mouth {
/// Keeping these paths distinct also preserves TeX's displayed context.
fn next_character(&mut self, state: &State, reads: &mut BTreeSet<Dependency>) -> Character {
let mut c = self.line[self.cursor];
let mut consumed = 1;
loop {
let second = self.cursor + consumed;
if Self::catcode(state, reads, c.value) != CatCode::Superscript
|| self.line.get(second).map(|v| v.value) != Some(c.value)
{
break;
}
let Some(next) = self.line.get(second + 1).copied() else {
break;
};
let hex = |c: char| {
if c.is_ascii_digit() || ('a'..='f').contains(&c) {
c.to_digit(16)
} else {
None
}
};
let (value, extra) = if let Some(a) = hex(next.value)
&& let Some(b) = self.line.get(second + 2).and_then(|v| hex(v.value))
{
(char::from_u32(16 * a + b).unwrap(), 3)
} else if (next.value as u32) < 128 {
(char::from_u32(next.value as u32 ^ 64).unwrap(), 2)
} else {
self.cursor += 1;
while Self::catcode(state, reads, c.value) == CatCode::Superscript {
let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor) else {
break;
};
c.value = value;
c.end = self.line[self.cursor + consumed + extra - 1].end;
consumed += extra;
self.cursor += extra;
c.end = self.line[self.cursor - 1].end;
}
self.cursor += consumed;
c
}
pub fn next(