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

+14 -3
View File
@@ -1154,7 +1154,8 @@ impl Executor {
}
Iftrue | Iffalse | Ifx | Ifnum | Ifdefined | If | Ifcat | Ifodd | Ifcsname
| Ifcase | Ifeof | Ifdim | Ifpdfabsnum | Ifpdfabsdim | Ifvmode | Ifhmode
| Ifmmode | Ifinner | Ifvoid | Ifhbox | Ifvbox | Iffontchar | Ifincsname => {
| Ifmmode | Ifinner | Ifvoid | Ifhbox | Ifvbox | Iffontchar | Ifincsname
| Ifprimitive | Ifpdfprimitive => {
self.evaluate_conditional(primitive, token.span, false, files)?;
}
Unless => {
@@ -1519,6 +1520,16 @@ impl Executor {
let next = self.normal_raw(files)?.token;
self.meaning(&next) != Meaning::Undefined
}
Ifprimitive | Ifpdfprimitive => {
let operand = self.normal_raw(files)?;
let live = self.meaning(&operand.token);
matches!(
&operand.token.kind,
TokenKind::Command(name)
if !Self::suppresses_meaning(&operand, &live)
&& self.state.original_primitive(name) == Some(&live)
)
}
Ifcsname => {
let name = self.csname(files)?;
self.record_meaning_read(&name);
@@ -4033,8 +4044,8 @@ impl Executor {
Aftergroup => self.schedule_after_group(files)?,
Showbox => self.show_box(files)?,
Showlists | Showgroups | Showifs => self.show_activity(primitive, files)?,
Show | Showthe => self.show(
primitive == Showthe,
Show | Showthe | Showtokens => self.show(
primitive,
token.invocation.unwrap_or(token.span),
files,
)?,
@@ -282,12 +282,18 @@ impl Executor {
pub(super) fn show(
&mut self,
the: bool,
command: Primitive,
span: Span,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
let (mut head, mut tail) = if the {
let text = match self.the_value(files)? {
let value = match command {
Primitive::Show => None,
Primitive::Showthe => Some(self.the_value(files)?),
Primitive::Showtokens => Some(TheValue::Tokens(self.scan_group(files)?)),
_ => unreachable!("token or quantity diagnostic"),
};
let (mut head, mut tail) = if let Some(value) = value {
let text = match value {
TheValue::Text(text) => text,
TheValue::Tokens(tokens) => {
let mut text = String::new();
@@ -356,7 +362,7 @@ impl Executor {
self.events.push(Event::Show(ShowReport {
output,
log_output,
the,
the: command == Primitive::Showthe,
text,
context,
span,
@@ -883,7 +883,7 @@ impl Executor {
| Knbccode | Knaccode | Letterspacefont | Textfont | Scriptfont
| Scriptscriptfont | Endlinechar | Newlinechar | Escapechar | Globaldefs | Time
| Day | Month | Year | BeginGroup | EndGroup | Relax | Message | Errmessage
| Endinput | Showbox | Show | Showthe | Showlists | Showifs | Showgroups
| Endinput | Showbox | Show | Showthe | Showtokens | Showlists | Showifs | Showgroups
| Discretionary | DiscretionaryHyphen
| Parshape | LinePenalties(_) | Partokenname => {
return Ok(false);
@@ -16,9 +16,9 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 100;
const COMPATIBLE_SCHEMAS: [u32; 66] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, SCHEMA,
const SCHEMA: u32 = 101;
const COMPATIBLE_SCHEMAS: [u32; 67] = [
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, SCHEMA,
];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
@@ -217,6 +217,9 @@ impl FormatImage {
if image.schema < 100 {
image.state.install_line_penalty_primitives();
}
if image.schema < 101 {
image.state.install_primitive_inspection();
}
let pdf_objects = image.state.pdf_objects.as_deref();
let mut numbered_images = 0;
for (index, saved) in image.pdf_images.iter().enumerate() {
@@ -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);