Run afterassignment tokens after their assignment

\afterassignment<token> leaked because assignment processing happens in later
passes. Add a rewrite after the body is extracted: find \afterassignment, read
its token, locate the following assignment (a \def/\gdef through
runtime_definition, or the next top-level '=' plus its value token), and move
the command before the assignment's follower so the assignment passes run
first. The queued token stays in the stream for normal expansion.

A preview test queues \hello before \a=5 and checks H appears, Text. still
renders and no \afterassignment leaks.
This commit is contained in:
gloamxun committed 2026-10-27 01:01:28 +08:00
1 parent 24d5390d25
commit b16e5a53e8
3 files changed
+78 -5

No files matched your search

+71
View File
@@ -5175,6 +5175,53 @@ fn measure_savebox(content: &str, base_size: i64) -> (i64, i64, i64) {
/// Capture `\def` lines written in the document body, before macro expansion.
/// Reserve `\newcount`/`\newdimen`/`\newskip` registers declared in the body.
/// Move the token queued by `\afterassignment` behind the assignment it
/// follows, so the normal assignment passes run first.
fn apply_afterassignment(body: &str) -> String {
let mut text = body.to_string();
loop {
let Some(position) = text.find("\\afterassignment") else {
break;
};
let after = &text[position + "\\afterassignment".len()..];
let Some((token, token_used)) = next_token(after) else {
break;
};
let rest = &after[token_used..];
let Some(end) = assignment_end(rest) else {
break;
};
text = format!(
"{}{}{}{}",
&text[..position],
&rest[..end],
token,
&rest[end..]
);
}
text
}
fn assignment_end(text: &str) -> Option<usize> {
if let Some((_, used, _)) = runtime_definition(text) {
return Some(used);
}
let mut depth = 0_i32;
for (index, character) in text.char_indices() {
match character {
'{' => depth += 1,
'}' => depth -= 1,
'=' if depth == 0 => {
let after = &text[index + 1..];
let (_, used) = next_token(after)?;
return Some(index + 1 + used);
}
_ => {}
}
}
None
}
fn capture_body_registers(
body: &mut String,
counts: &mut Vec<(String, i64)>,
@@ -5811,6 +5858,7 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
// paragraphs of their own.
let mut body = latex_body(&stripped).trim_matches('\n').to_string();
geometry.bibliography = extract_bibliography(&mut body);
body = apply_afterassignment(&body);
capture_body_registers(
&mut body,
&mut geometry.count_registers,
@@ -15719,6 +15767,29 @@ A definition.
assert!(!text.contains("makeatletter"), "{text}");
}
#[test]
fn afterassignment_runs_after_the_assignment() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\newcount\\a\n\\def\\hello{H}\n\\afterassignment\\hello\\a=5\n\nText.\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let svg = value["pages"][0]["svg"].as_str().expect("svg");
let text: String = svg
.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>()
.split_whitespace()
.collect();
assert!(text.contains('H'), "{text}");
assert!(text.contains("Text."), "{text}");
assert!(!text.contains("afterassignment"), "{text}");
}
#[test]
fn aftergroup_runs_tokens_at_group_end() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{A}{\\def\\a{B}\\aftergroup\\a}\\a and {\\aftergroup{!}}X\n\n\\end{document}\n";