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);
@@ -85,3 +85,49 @@ fn condition_stack_traces_match_real_expansion_and_skipping() {
}
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn primitive_inspection_requires_the_original_name_and_live_meaning() {
for (profile, spelling) in [
(InputProfile::TeX8Bit, r"\ifpdfprimitive"),
(InputProfile::XeTeX, r"\ifprimitive"),
] {
let source = r"\catcode123=1 \catcode125=2 \catcode35=6
\let\check=CHECK
\let\saved=\hbox
\def\query#1{\message{\check#1YES\else NO\fi}}
\query\hbox \query\saved
{\let\hbox=\vbox \query\hbox}
\query\hbox
\message{\expandafter\check\noexpand\iftrue YES\else NO\fi}
\outer\def\outercommand{}
\query\notdefined
\message{\check\outercommand YES\else NO\fi}
\def\kept{\errmessage{should not expand}}
\def\opening{{\kept##A}}
\showtokens\opening
\end"
.replace("CHECK", spelling);
let result = Executor::new(
State::initex().with_etex().with_input_profile(profile),
Limits::default(),
)
.run(
SourceFile {
name: "inspect.tex".into(),
data: source.into_bytes(),
},
&mut NoFiles,
)
.unwrap();
let messages: Vec<_> = result.events.iter().filter_map(|event| match event {
Event::Message { text, .. } => Some(text.as_str()),
_ => None,
}).collect();
assert_eq!(messages, ["YES", "NO", "NO", "YES", "NO", "NO", "NO"]);
assert!(result.events.iter().any(|event| matches!(
event, Event::Show(report) if report.text.trim() == r"> \kept ##A."
)));
assert!(result.reads.contains(&Dependency::Meaning(CommandName::Control("hbox".into()))));
}
}
+28
View File
@@ -18,6 +18,34 @@
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 |
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
## 2026-11-28 原语身份判断与未展开记号诊断
pdfTeX 的 `\ifpdfprimitive` 和 XeTeX 的 `\ifprimitive` 对照当前
兼容模式的初始命令表,按原名比较实际命令含义。用户别名、局部重绑、
恢复、未定义命令和受 `\noexpand` 保护的可展开命令由同一条件路径处理。
`\showtokens` 扫描平衡记号列表,沿用现有诊断与日志通道。宏调用、分组、
字面参数标记和当前转义字符按读到的记号显示。
[pdfTeX 的原语条件](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L11933-L11946)
· [XeTeX 的原语条件](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/xetexdir/xetex.web#L11914-L11928)
两个 profile 各核对 19 个条件结果、3 条记号诊断、一页文字词框和
144 DPI 灰度像素,均与各自参考一致。schema101 的别名重载与参考格式
一致,schema100 加载保留占用同名命令的用户宏。直接使用上一阶段从
原样 `pdflatex.ini` 和 `xelatex.ini` 生成的两份 schema100 镜像,
实际 `article.cls` 各完成一页,局部改绑、恢复和记号输出均通过。
三页项目两次编辑和恢复子文件中的 `\count` 绑定及记号列表,各复用
1,129 步,只更新第二页。两轮 PDF、页面、SVG、辅助文件、来源映射和
日志均与独立全量一致。发布构建和两项定向回归通过。
回执在 `target/p2-native-primitives`,`receipt.json` 的 SHA-256 为
`d558f291ec26a72b40322d7f66987f177a2c3cfcec0373bdb80d6a91d282330c`。
下一入口在 `next-execution`。原版 `\pdfprimitive` 和 `\primitive`
在真实数值扫描中读出 37,展开条件后输出 Yes。`\edef` 保存未展开的
内部执行前缀,之后重绑盒命令时仍能执行原语。当前原生同源两种模式
均在 `primitive.tex:13:60` 报 `extra else`。保存了两端原始日志和参考页。
[pdfTeX 的原语执行与延迟重放](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L9019-L9081)
## 2026-11-28 e-TeX 的逐行分页罚值
原生两种 e-TeX 模式均执行 `\interlinepenalties`、`\clubpenalties`、