diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index d82f2d3..97dcfd1 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -76,6 +76,10 @@ impl From 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); + #[derive(Clone)] enum InputFrame { Source { @@ -707,22 +711,37 @@ impl Executor { files: &mut impl SourceProvider, definition: bool, ) -> Result, Error> { - while let Some(token) = self.raw(files)? { - if let Some(token) = self.expand_one(token, files, definition)? { - return Ok(Some(token)); + self.expanded_with_meaning(files, definition) + .map(|result| result.map(|(input, _)| input)) + } + fn expanded_with_meaning( + &mut self, + files: &mut impl SourceProvider, + definition: bool, + ) -> Result, 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) } fn required_expanded(&mut self, files: &mut impl SourceProvider) -> Result { - self.expanded(files)? + self.required_expanded_with_meaning(files).map(|(input, _)| input) + } + fn required_expanded_with_meaning( + &mut self, + files: &mut impl SourceProvider, + ) -> Result { + self.expanded_with_meaning(files, false)? .ok_or_else(|| self.error("unexpected end of expanded input", self.current)) } fn expanded_non_space(&mut self, files: &mut impl SourceProvider) -> Result { loop { - let token = self.required_expanded(files)?; - if !self.is_spacer(&token.token) { - return Ok(token); + let (input, meaning) = self.required_expanded_with_meaning(files)?; + let meaning = meaning.unwrap_or_else(|| self.meaning(&input.token)); + if !matches!(meaning, Meaning::Character(TokenKind::Character(_, CatCode::Space))) { + return Ok(input); } } } @@ -843,6 +862,16 @@ impl Executor { files: &mut impl SourceProvider, definition_context: bool, ) -> Result, 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, Error> { if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) { return self.fail("native expansion nesting limit exceeded"); } @@ -859,12 +888,12 @@ impl Executor { mut input: InputToken, files: &mut impl SourceProvider, mut definition_context: bool, - ) -> Result, Error> { + ) -> Result, Error> { let mut pending = Vec::new(); let mut first = None; let result = loop { if input.frozen { - break Some(input); + break Some((input, None)); } let meaning = self.meaning(&input.token); let Meaning::Primitive(command @ (Primitive::Expandafter | Primitive::UsePrimitive)) = meaning else { @@ -885,7 +914,7 @@ impl Executor { } self.unread(operand)?; input.token.kind = TokenKind::Command(CommandName::FrozenPrimitivePrefix); - break Some(input); + break Some((input, None)); } // Expanding the second token is the same dispatch with deferred // input. Only a re-entry from a scanner needs a new expansion frame. @@ -901,7 +930,7 @@ impl Executor { let Some(first) = first else { return Ok(result); }; - if let Some(second) = result { + if let Some((second, _)) = result { self.unread(second)?; } self.unread(first)?; @@ -917,7 +946,7 @@ impl Executor { meaning: Meaning, files: &mut impl SourceProvider, definition_context: bool, - ) -> Result, Error> { + ) -> Result, Error> { let token = &input.token; use Primitive::*; match meaning { @@ -929,7 +958,7 @@ impl Executor { } Meaning::Macro(definition) => { if definition.protected && definition_context { - return Ok(Some(input)); + return Ok(Some((input, Some(Meaning::Macro(definition))))); } self.call_macro(definition, token, files)?; } @@ -950,12 +979,13 @@ impl Executor { let mut next = self.normal_raw(files)?; // A nonexpandable control sequence keeps its command // 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::Primitive(p) => p.expandable(), _ => false, }; - return Ok(Some(next)); + return Ok(Some((next, Some(meaning)))); } Number => { let value = self.integer(files)?; @@ -1232,10 +1262,8 @@ impl Executor { if limit == ConditionalLimit::Scanning { let span = token.span; self.unread(input)?; - return Ok(Some( - Token::new(TokenKind::Command(CommandName::FrozenRelax), span) - .into(), - )); + let relax = Token::new(TokenKind::Command(CommandName::FrozenRelax), span); + return Ok(Some((relax.into(), None))); } if !limit.accepts(primitive) { return self.fail("unexpected native conditional branch delimiter"); @@ -1252,10 +1280,10 @@ impl Executor { _ if primitive.conditional() => { 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) } @@ -3924,7 +3952,7 @@ impl Executor { capture_boundary(self, initial_depth, files); self.process_native_pages(files)?; self.check_box_capacity(None)?; - let Some(input) = self.expanded(files)? else { + let Some((input, meaning)) = self.expanded_with_meaning(files, false)? else { break; }; if let Some(boundary) = input.alignment { @@ -3934,7 +3962,7 @@ impl Executor { self.alignment_boundary(boundary, files)?; 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); if xe && (frozen_expandable diff --git a/crates/texrra-tex-core/tests/execution_native.rs b/crates/texrra-tex-core/tests/execution_native.rs index 54676b8..9373ed6 100644 --- a/crates/texrra-tex-core/tests/execution_native.rs +++ b/crates/texrra-tex-core/tests/execution_native.rs @@ -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"); } +#[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] fn skipped_branches_are_not_expanded_or_executed() { let result = execute( diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index c60f954..17188f3 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -18,6 +18,32 @@ | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;完整副作用/恢复契约、旧来源和性能仍缺 | | 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 私有日志与检查点资源重放 实时草稿把日志写到私有文件,使用有界缓冲,成功或失败清理时显式写入。