Invoke frozen original primitives in native TeX

This commit is contained in:
gloamxun committed 2026-11-28 14:19:26 +08:00
1 parent 2007ac5627
commit ba548ef05d
10 files changed
+219 -33

No files matched your search

+44 -16
View File
@@ -16,12 +16,18 @@ pub enum CommandName {
/// It retains the loaded font even when the spelling selector is redefined.
Font(FontId),
FrozenRelax,
/// Preserved in token lists before the ordinary token naming the operand.
FrozenPrimitivePrefix,
/// The selected primitive meaning, which survives putting a token back.
FrozenPrimitive(String),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum Primitive {
Relax,
Ignorespaces,
UsePrimitive,
DeferredPrimitive,
Explicitspace,
Par,
BeginGroup,
@@ -495,6 +501,7 @@ impl Primitive {
| Firstmarks
| Botmarks
| Noexpand
| UsePrimitive
| Expandafter
| Number
| Uchar
@@ -1357,7 +1364,7 @@ impl Default for State {
state.parameters.insert(p, v);
}
state.install_pdftex_primitives();
state.install_primitive_inspection();
state.install_primitive_commands();
state.install_unicode_commands();
state
}
@@ -1596,7 +1603,7 @@ impl State {
use Primitive::*;
self.profile = Profile::EtexBootstrap;
self.install_line_penalty_primitives();
self.install_primitive_inspection();
self.install_primitive_commands();
let hook = TokenParameter::Everyeof;
self.commands.insert(
CommandName::Control(hook.name()),
@@ -1942,14 +1949,16 @@ impl State {
self.install_unicode_commands();
self.install_xetex_etex_commands();
self.install_direction_commands();
self.install_primitive_inspection();
self.install_primitive_commands();
self
}
pub(super) fn install_primitive_inspection(&mut self) {
pub(super) fn install_primitive_commands(&mut self) {
let xe = self.input_profile == super::InputProfile::XeTeX;
self.update_profile_command("ifprimitive", Meaning::Primitive(Primitive::Ifprimitive), xe);
self.update_profile_command("ifpdfprimitive", Meaning::Primitive(Primitive::Ifpdfprimitive), !xe);
self.update_profile_command("primitive", Meaning::Primitive(Primitive::UsePrimitive), xe);
self.update_profile_command("pdfprimitive", Meaning::Primitive(Primitive::UsePrimitive), !xe);
self.update_profile_command(
"showtokens",
Meaning::Primitive(Primitive::Showtokens),
@@ -2049,7 +2058,14 @@ impl State {
);
}
pub(super) fn primitive_prefix_name(&self) -> &'static str {
if self.input_profile == super::InputProfile::XeTeX { "primitive" } else { "pdfprimitive" }
}
pub(super) fn profile_primitive_name(&self, primitive: Primitive) -> Option<&'static str> {
if matches!(primitive, Primitive::UsePrimitive | Primitive::DeferredPrimitive) {
return Some(self.primitive_prefix_name());
}
if self.input_profile == super::InputProfile::XeTeX {
return XETEX_PRIMITIVE_ALIASES
.iter()
@@ -2122,10 +2138,13 @@ impl State {
})
}
/// Built-in meanings before any macro, alias or format assignments.
pub(super) fn original_primitive(&self, name: &CommandName) -> Option<&'static Meaning> {
pub(super) fn original_primitive<'a>(
&self,
name: &'a CommandName,
) -> Option<(&'a str, &'static Meaning)> {
use super::InputProfile;
use std::sync::OnceLock;
type CommandCatalogue = OnceLock<BTreeMap<CommandName, Meaning>>;
type CommandCatalogue = OnceLock<BTreeMap<String, Meaning>>;
static CATALOGUES: [[CommandCatalogue; 2]; 3] =
[const { [const { OnceLock::new() }; 2] }; 3];
@@ -2134,27 +2153,36 @@ impl State {
InputProfile::UnicodeScalar => 1,
InputProfile::XeTeX => 2,
};
let name = match name {
CommandName::Control(name) | CommandName::FrozenPrimitive(name) => name.as_str(),
CommandName::FrozenPrimitivePrefix => self.primitive_prefix_name(),
CommandName::FrozenRelax => "relax",
CommandName::Font(FontId::NULL) => "nullfont",
_ => return None,
};
let extended = self.profile == Profile::EtexBootstrap;
CATALOGUES[input][usize::from(extended)]
.get_or_init(|| {
let state = Self::initex().with_input_profile(self.input_profile);
let state = if extended { state.with_etex() } else { state };
state.commands
state.commands.into_iter().filter_map(|(name, meaning)| match name {
CommandName::Control(name) => Some((name, meaning)),
_ => None,
}).collect()
})
.get(name)
.map(|meaning| (name, meaning))
}
pub fn meaning(&self, name: &CommandName) -> Meaning {
if *name == CommandName::FrozenRelax {
return Meaning::Primitive(Primitive::Relax);
match name {
CommandName::FrozenRelax => Meaning::Primitive(Primitive::Relax),
CommandName::FrozenPrimitivePrefix => Meaning::Primitive(Primitive::DeferredPrimitive),
CommandName::FrozenPrimitive(_) => self.original_primitive(name)
.map(|(_, meaning)| meaning.clone()).unwrap_or(Meaning::Undefined),
CommandName::Font(font) => Meaning::Font(*font),
_ => self.commands.get(name).cloned().unwrap_or(Meaning::Undefined),
}
if let CommandName::Font(font) = name {
return Meaning::Font(*font);
}
self.commands
.get(name)
.cloned()
.unwrap_or(Meaning::Undefined)
}
/// Classic projection for byte-table consumers. A code outside the
/// representable classic range has TeX's recovery value of zero.