Inspect original primitives and unexpanded tokens

This commit is contained in:
gloamxun committed 2026-11-28 14:01:12 +08:00
1 parent c094d9a81d
commit 2007ac5627
7 files changed
+147 -11

No files matched your search

@@ -142,6 +142,8 @@ pub enum Primitive {
Ifx,
Ifnum,
Ifdefined,
Ifprimitive,
Ifpdfprimitive,
// Conditional commands also nest in skipped input without evaluating it.
If,
Ifcat,
@@ -242,6 +244,7 @@ pub enum Primitive {
Showbox,
Show,
Showthe,
Showtokens,
Showlists,
Showgroups,
Showifs,
@@ -544,6 +547,8 @@ impl Primitive {
| Self::Ifpdfabsnum
| Self::Ifpdfabsdim
| Self::Ifdefined
| Self::Ifprimitive
| Self::Ifpdfprimitive
| Self::If
| Self::Ifcat
| Self::Ifdim
@@ -1352,6 +1357,7 @@ impl Default for State {
state.parameters.insert(p, v);
}
state.install_pdftex_primitives();
state.install_primitive_inspection();
state.install_unicode_commands();
state
}
@@ -1590,6 +1596,7 @@ impl State {
use Primitive::*;
self.profile = Profile::EtexBootstrap;
self.install_line_penalty_primitives();
self.install_primitive_inspection();
let hook = TokenParameter::Everyeof;
self.commands.insert(
CommandName::Control(hook.name()),
@@ -1935,9 +1942,21 @@ impl State {
self.install_unicode_commands();
self.install_xetex_etex_commands();
self.install_direction_commands();
self.install_primitive_inspection();
self
}
pub(super) fn install_primitive_inspection(&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(
"showtokens",
Meaning::Primitive(Primitive::Showtokens),
self.profile == Profile::EtexBootstrap,
);
}
fn update_profile_command(&mut self, name: &str, meaning: Meaning, enabled: bool) {
let name = CommandName::Control(name.into());
if enabled {
@@ -2102,6 +2121,29 @@ impl State {
_ => CatCode::Other,
})
}
/// Built-in meanings before any macro, alias or format assignments.
pub(super) fn original_primitive(&self, name: &CommandName) -> Option<&'static Meaning> {
use super::InputProfile;
use std::sync::OnceLock;
type CommandCatalogue = OnceLock<BTreeMap<CommandName, Meaning>>;
static CATALOGUES: [[CommandCatalogue; 2]; 3] =
[const { [const { OnceLock::new() }; 2] }; 3];
let input = match self.input_profile {
InputProfile::TeX8Bit => 0,
InputProfile::UnicodeScalar => 1,
InputProfile::XeTeX => 2,
};
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
})
.get(name)
}
pub fn meaning(&self, name: &CommandName) -> Meaning {
if *name == CommandName::FrozenRelax {
return Meaning::Primitive(Primitive::Relax);