Carry resolved TeX command meanings through expansion

This commit is contained in:
gloamxun committed 2026-11-29 01:28:05 +08:00
1 parent c6b5c9ed56
commit 93d3d38983
3 files changed
+87 -23

No files matched your search

+51 -23
View File
@@ -76,6 +76,10 @@ impl From<Token> for InputToken {
} }
} }
} }
// A meaning is valid until the returned token is used or put back. Tokens
// that bypass normal expansion may still need to be looked up by the caller.
type ExpandedToken = (InputToken, Option<Meaning>);
#[derive(Clone)] #[derive(Clone)]
enum InputFrame { enum InputFrame {
Source { Source {
@@ -707,22 +711,37 @@ impl Executor {
files: &mut impl SourceProvider, files: &mut impl SourceProvider,
definition: bool, definition: bool,
) -> Result<Option<InputToken>, Error> { ) -> Result<Option<InputToken>, Error> {
while let Some(token) = self.raw(files)? { self.expanded_with_meaning(files, definition)
if let Some(token) = self.expand_one(token, files, definition)? { .map(|result| result.map(|(input, _)| input))
return Ok(Some(token)); }
fn expanded_with_meaning(
&mut self,
files: &mut impl SourceProvider,
definition: bool,
) -> Result<Option<ExpandedToken>, Error> {
while let Some(input) = self.raw(files)? {
if let Some(expanded) = self.expand_one_with_meaning(input, files, definition)? {
return Ok(Some(expanded));
} }
} }
Ok(None) Ok(None)
} }
fn required_expanded(&mut self, files: &mut impl SourceProvider) -> Result<InputToken, Error> { fn required_expanded(&mut self, files: &mut impl SourceProvider) -> Result<InputToken, Error> {
self.expanded(files)? self.required_expanded_with_meaning(files).map(|(input, _)| input)
}
fn required_expanded_with_meaning(
&mut self,
files: &mut impl SourceProvider,
) -> Result<ExpandedToken, Error> {
self.expanded_with_meaning(files, false)?
.ok_or_else(|| self.error("unexpected end of expanded input", self.current)) .ok_or_else(|| self.error("unexpected end of expanded input", self.current))
} }
fn expanded_non_space(&mut self, files: &mut impl SourceProvider) -> Result<InputToken, Error> { fn expanded_non_space(&mut self, files: &mut impl SourceProvider) -> Result<InputToken, Error> {
loop { loop {
let token = self.required_expanded(files)?; let (input, meaning) = self.required_expanded_with_meaning(files)?;
if !self.is_spacer(&token.token) { let meaning = meaning.unwrap_or_else(|| self.meaning(&input.token));
return Ok(token); if !matches!(meaning, Meaning::Character(TokenKind::Character(_, CatCode::Space))) {
return Ok(input);
} }
} }
} }
@@ -843,6 +862,16 @@ impl Executor {
files: &mut impl SourceProvider, files: &mut impl SourceProvider,
definition_context: bool, definition_context: bool,
) -> Result<Option<InputToken>, Error> { ) -> Result<Option<InputToken>, Error> {
self.expand_one_with_meaning(input, files, definition_context)
.map(|result| result.map(|(input, _)| input))
}
fn expand_one_with_meaning(
&mut self,
input: InputToken,
files: &mut impl SourceProvider,
definition_context: bool,
) -> Result<Option<ExpandedToken>, Error> {
if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) { if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) {
return self.fail("native expansion nesting limit exceeded"); return self.fail("native expansion nesting limit exceeded");
} }
@@ -859,12 +888,12 @@ impl Executor {
mut input: InputToken, mut input: InputToken,
files: &mut impl SourceProvider, files: &mut impl SourceProvider,
mut definition_context: bool, mut definition_context: bool,
) -> Result<Option<InputToken>, Error> { ) -> Result<Option<ExpandedToken>, Error> {
let mut pending = Vec::new(); let mut pending = Vec::new();
let mut first = None; let mut first = None;
let result = loop { let result = loop {
if input.frozen { if input.frozen {
break Some(input); break Some((input, None));
} }
let meaning = self.meaning(&input.token); let meaning = self.meaning(&input.token);
let Meaning::Primitive(command @ (Primitive::Expandafter | Primitive::UsePrimitive)) = meaning else { let Meaning::Primitive(command @ (Primitive::Expandafter | Primitive::UsePrimitive)) = meaning else {
@@ -885,7 +914,7 @@ impl Executor {
} }
self.unread(operand)?; self.unread(operand)?;
input.token.kind = TokenKind::Command(CommandName::FrozenPrimitivePrefix); input.token.kind = TokenKind::Command(CommandName::FrozenPrimitivePrefix);
break Some(input); break Some((input, None));
} }
// Expanding the second token is the same dispatch with deferred // Expanding the second token is the same dispatch with deferred
// input. Only a re-entry from a scanner needs a new expansion frame. // input. Only a re-entry from a scanner needs a new expansion frame.
@@ -901,7 +930,7 @@ impl Executor {
let Some(first) = first else { let Some(first) = first else {
return Ok(result); return Ok(result);
}; };
if let Some(second) = result { if let Some((second, _)) = result {
self.unread(second)?; self.unread(second)?;
} }
self.unread(first)?; self.unread(first)?;
@@ -917,7 +946,7 @@ impl Executor {
meaning: Meaning, meaning: Meaning,
files: &mut impl SourceProvider, files: &mut impl SourceProvider,
definition_context: bool, definition_context: bool,
) -> Result<Option<InputToken>, Error> { ) -> Result<Option<ExpandedToken>, Error> {
let token = &input.token; let token = &input.token;
use Primitive::*; use Primitive::*;
match meaning { match meaning {
@@ -929,7 +958,7 @@ impl Executor {
} }
Meaning::Macro(definition) => { Meaning::Macro(definition) => {
if definition.protected && definition_context { if definition.protected && definition_context {
return Ok(Some(input)); return Ok(Some((input, Some(Meaning::Macro(definition)))));
} }
self.call_macro(definition, token, files)?; self.call_macro(definition, token, files)?;
} }
@@ -950,12 +979,13 @@ impl Executor {
let mut next = self.normal_raw(files)?; let mut next = self.normal_raw(files)?;
// A nonexpandable control sequence keeps its command // A nonexpandable control sequence keeps its command
// meaning, including a character alias in scan_toks. // meaning, including a character alias in scan_toks.
next.frozen |= match self.meaning(&next.token) { let meaning = self.meaning(&next.token);
next.frozen |= match &meaning {
Meaning::Undefined | Meaning::Macro(_) => true, Meaning::Undefined | Meaning::Macro(_) => true,
Meaning::Primitive(p) => p.expandable(), Meaning::Primitive(p) => p.expandable(),
_ => false, _ => false,
}; };
return Ok(Some(next)); return Ok(Some((next, Some(meaning))));
} }
Number => { Number => {
let value = self.integer(files)?; let value = self.integer(files)?;
@@ -1232,10 +1262,8 @@ impl Executor {
if limit == ConditionalLimit::Scanning { if limit == ConditionalLimit::Scanning {
let span = token.span; let span = token.span;
self.unread(input)?; self.unread(input)?;
return Ok(Some( let relax = Token::new(TokenKind::Command(CommandName::FrozenRelax), span);
Token::new(TokenKind::Command(CommandName::FrozenRelax), span) return Ok(Some((relax.into(), None)));
.into(),
));
} }
if !limit.accepts(primitive) { if !limit.accepts(primitive) {
return self.fail("unexpected native conditional branch delimiter"); return self.fail("unexpected native conditional branch delimiter");
@@ -1252,10 +1280,10 @@ impl Executor {
_ if primitive.conditional() => { _ if primitive.conditional() => {
return self.fail(format!("unsupported native conditional: {primitive:?}")); return self.fail(format!("unsupported native conditional: {primitive:?}"));
} }
_ => return Ok(Some(input)), _ => return Ok(Some((input, Some(Meaning::Primitive(primitive))))),
} }
} }
_ => return Ok(Some(input)), meaning => return Ok(Some((input, Some(meaning)))),
} }
Ok(None) Ok(None)
} }
@@ -3924,7 +3952,7 @@ impl Executor {
capture_boundary(self, initial_depth, files); capture_boundary(self, initial_depth, files);
self.process_native_pages(files)?; self.process_native_pages(files)?;
self.check_box_capacity(None)?; self.check_box_capacity(None)?;
let Some(input) = self.expanded(files)? else { let Some((input, meaning)) = self.expanded_with_meaning(files, false)? else {
break; break;
}; };
if let Some(boundary) = input.alignment { if let Some(boundary) = input.alignment {
@@ -3934,7 +3962,7 @@ impl Executor {
self.alignment_boundary(boundary, files)?; self.alignment_boundary(boundary, files)?;
continue; continue;
} }
let meaning = self.meaning(&input.token); let meaning = meaning.unwrap_or_else(|| self.meaning(&input.token));
let frozen_expandable = Self::suppresses_meaning(&input, &meaning); let frozen_expandable = Self::suppresses_meaning(&input, &meaning);
if xe if xe
&& (frozen_expandable && (frozen_expandable
@@ -156,6 +156,16 @@ fn expansion_order_let_ifx_noexpand_and_csname_follow_token_meanings() {
assert_eq!(text(&execute(r"\ifx XBAD\else GOOD\fi")), "GOOD"); assert_eq!(text(&execute(r"\ifx XBAD\else GOOD\fi")), "GOOD");
} }
#[test]
fn backed_up_commands_see_changes_from_everypar_and_expandafter() {
let result = execute(concat!(
r"\let\chosen=A\def\change{\let\chosen=C}",
r"\everypar{\let\chosen=B}",
r"\chosen\expandafter\change\chosen\par",
));
assert_eq!(text(&result), "BC");
}
#[test] #[test]
fn skipped_branches_are_not_expanded_or_executed() { fn skipped_branches_are_not_expanded_or_executed() {
let result = execute( let result = execute(
+26
View File
@@ -18,6 +18,32 @@
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 | | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 |
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
## 2026-11-29 尾部执行范围与展开后的命令含义
正常展开将已读取的命令含义交给主执行循环和跳空格扫描,省去即时
重复查表。被退回输入栈的 token 只保存 token 本身,再次取出时按
当时的绑定解析。实际 pdfTeX INITEX 对照覆盖每段钩子重定义字符别名和
expandafter 推迟使用别名,两端均输出 BC。语义步数及其上限保留。
[上游 XeTeX 的扩展取 token 和执行分派](https://github.com/TeX-Live/texlive-source/blob/e49735c7ed1aa2ed9c2e68e6632d27f20d42eca7/texk/web2c/xetexdir/xetex.web#L8465-L8479)
原样 A4 使用三个实际检查点定位执行范围。读取到正文尾行前、结束行前、
结束行已载入后的剩余步骤分别为 55,847、53,705 和 51,813。单独的
跟踪副本在正文尾部记录 2,103 次宏调用,其中 1,968 次发生在进入
enddocument 后。前后版本的跟踪日志归一化后逐字节一致,PDF 和 aux
完全一致。继续按检查点的实际状态分清剩余工作,优先从页构建与出页后
的钩子和引用处理寻找可独立验证的复用边界。
对原样 A4 的主文件和子文件各 10 对编辑,PDF、SVG、来源映射、aux、
归一化日志、变更页和复用步数一致,最终版本另开进程全量核对。服务端
预览就绪的配对中位变化是主文件 +0.49 ms、子文件 -0.86 ms。当前尚无
稳定的时延收益证据,保留实际浏览器和分规模项目的正式验收。
执行套件 13 项通过。一个旧断言期望 UnicodeScalar 下拒绝 chardef 256。
用改动前的 c6b5c9e 实跑重现该行为,UnicodeScalar 接受并读取 256,
传统 8 位模式报告范围错误。原测试与首个失败保留,未改其判定。
对齐边界、嵌套模板、XeTeX 原语和输入编码四项定向检查通过。
证据在 target/p4-execution-tail/。
## 2026-11-29 私有日志与检查点资源重放 ## 2026-11-29 私有日志与检查点资源重放
实时草稿把日志写到私有文件,使用有界缓冲,成功或失败清理时显式写入。 实时草稿把日志写到私有文件,使用有界缓冲,成功或失败清理时显式写入。