Queue aftergroup tokens until the group closes
\aftergroup was unsupported, so class-style deferred setup leaked. Expansion
now keeps a parallel stack of pending token lists: '{' and \begingroup push an
empty list, '}' and \endgroup pop it and append its tokens after the group (the
closing brace is emitted first, then the pending tokens with the command-end
marker so a control word cannot merge with the following text).
\aftergroup<token> queues one token and \aftergroup{...} queues the group body.
A test defines a group-local \a while queueing \aftergroup\a, then checks that
the queued \a runs after the group restores A, giving "AA and !X" with no
warnings.
This commit is contained in:
1 parent
9d10def2f5
commit
24d5390d25
3 files changed
+79
-5
No files matched your search
@@ -793,6 +793,29 @@ fn brace_depth_delta(line: &str) -> i32 {
|
||||
depth
|
||||
}
|
||||
|
||||
/// Parse `\aftergroup<token>` (or `\aftergroup{...}`) into the text that should
|
||||
/// run once the current group closes.
|
||||
fn aftergroup_tokens(text: &str) -> Option<(String, usize)> {
|
||||
let after = text.strip_prefix("\\aftergroup")?;
|
||||
if after
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|character| character.is_ascii_alphabetic())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let (token, used) = next_token(after)?;
|
||||
if token == "{" {
|
||||
let brace_start = used - 1;
|
||||
let (content, group_used) = braced_group(&after[brace_start..])?;
|
||||
return Some((
|
||||
content.to_string(),
|
||||
"\\aftergroup".len() + brace_start + group_used,
|
||||
));
|
||||
}
|
||||
Some((token, "\\aftergroup".len() + used))
|
||||
}
|
||||
|
||||
/// Copy a conditional command together with the operand tokens it inspects,
|
||||
/// so macro expansion cannot consume them before the conditional is evaluated.
|
||||
fn protected_conditional_prefix(text: &str) -> Option<(String, usize)> {
|
||||
@@ -1510,6 +1533,7 @@ fn expand_macros_mut(source: &str, macros: &mut Vec<MacroDefinition>) -> String
|
||||
let mut index = 0;
|
||||
let mut changed = false;
|
||||
let mut scopes: Vec<Vec<MacroDefinition>> = Vec::new();
|
||||
let mut after_groups: Vec<Vec<String>> = Vec::new();
|
||||
while index < text.len() {
|
||||
let rest = &text[index..];
|
||||
if let Some((prefix, used)) = protected_conditional_prefix(rest) {
|
||||
@@ -1538,6 +1562,16 @@ fn expand_macros_mut(source: &str, macros: &mut Vec<MacroDefinition>) -> String
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some((tokens, used)) = aftergroup_tokens(rest) {
|
||||
if let Some(pending) = after_groups.last_mut() {
|
||||
pending.push(tokens);
|
||||
} else {
|
||||
output.push_str(&tokens);
|
||||
}
|
||||
index += used;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Some((alias, target, command_start)) = futurelet_lookahead(rest) {
|
||||
let definition = MacroDefinition {
|
||||
name: alias,
|
||||
@@ -1571,12 +1605,19 @@ fn expand_macros_mut(source: &str, macros: &mut Vec<MacroDefinition>) -> String
|
||||
}
|
||||
if rest.starts_with(r"\begingroup") {
|
||||
scopes.push(Vec::new());
|
||||
after_groups.push(Vec::new());
|
||||
index += r"\begingroup".len();
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if rest.starts_with(r"\endgroup") {
|
||||
scopes.pop();
|
||||
if let Some(tokens) = after_groups.pop() {
|
||||
for tokens in tokens {
|
||||
output.push_str(&tokens);
|
||||
output.push('\u{E022}');
|
||||
}
|
||||
}
|
||||
index += r"\endgroup".len();
|
||||
changed = true;
|
||||
continue;
|
||||
@@ -1658,10 +1699,19 @@ fn expand_macros_mut(source: &str, macros: &mut Vec<MacroDefinition>) -> String
|
||||
let character = rest.chars().next().expect("non-empty tail");
|
||||
if character == '{' {
|
||||
scopes.push(Vec::new());
|
||||
after_groups.push(Vec::new());
|
||||
} else if character == '}' {
|
||||
scopes.pop();
|
||||
}
|
||||
output.push(character);
|
||||
if character == '}' {
|
||||
if let Some(tokens) = after_groups.pop() {
|
||||
for tokens in tokens {
|
||||
output.push_str(&tokens);
|
||||
output.push('\u{E022}');
|
||||
}
|
||||
}
|
||||
}
|
||||
index += character.len_utf8();
|
||||
}
|
||||
text = output;
|
||||
@@ -15669,6 +15719,28 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{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";
|
||||
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("AAand!X"), "{text}");
|
||||
assert!(!text.contains("aftergroup"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn futurelet_peeks_without_consuming() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\shownext{[\\next]}\\futurelet\\next\\shownext x\n\n\\def\\yes{Y}\\def\\no{N}\\def\\pick{\\futurelet\\next\\pickii}\\def\\pickii{\\ifx\\next x\\yes\\else\\no\\fi}\n\n\\pick x\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user