Chain zero-argument expandafter prefixes

This commit is contained in:
gloamxun committed 2026-10-26 03:20:56 +08:00
1 parent 77d76af4a7
commit 2126ba8436
4 files changed
+54 -40

No files matched your search

+50 -36
View File
@@ -880,6 +880,42 @@ fn latex_internal_at(rest: &str, macros: &[MacroDefinition]) -> Option<(String,
None
}
fn expandafter_chain(text: &str, macros: &[MacroDefinition]) -> Option<(String, usize)> {
let mut position = 0;
let mut output = String::new();
loop {
let rest = &text[position..];
let after = rest.strip_prefix(r"\expandafter")?;
if after
.chars()
.next()
.is_some_and(|character| character.is_ascii_alphabetic())
{
return None;
}
position += r"\expandafter".len();
let spaces = after.len() - after.trim_start().len();
position += spaces;
let trimmed = &text[position..];
let (command, length) = control_sequence(trimmed)?;
let definition = macros
.iter()
.find(|known| known.name == command && known.args == 0 && known.default.is_none())?;
output.push_str(&definition.body);
position += length;
let rest = &text[position..];
position += rest.len() - rest.trim_start().len();
if text[position..].starts_with(r"\expandafter") {
continue;
}
break;
}
let (expanded, used) = expand_macro_at(&text[position..], macros)?;
output.push_str(&expanded);
position += used;
Some((output, position))
}
fn expand_macros(source: &str, macros: &[MacroDefinition]) -> String {
let mut text = source.to_string();
for _ in 0..8 {
@@ -914,42 +950,12 @@ fn expand_macros(source: &str, macros: &[MacroDefinition]) -> String {
}
}
}
if let Some(after) = rest.strip_prefix(r"\expandafter") {
let boundary = after
.chars()
.next()
.is_none_or(|character| !character.is_ascii_alphabetic());
if boundary {
let trimmed = after.trim_start();
if let Some(command) = trimmed.strip_prefix('\\') {
let length = command
.find(|character: char| !character.is_ascii_alphabetic())
.unwrap_or(command.len());
if length > 0 {
let kept = 1 + length;
let name = format!("\\{}", &command[..length]);
let definition = macros.iter().find(|macro_| macro_.name == name);
if let Some(definition) = definition
&& definition.args == 0
&& definition.default.is_none()
{
let tail = &trimmed[kept..];
let spaces = tail.len() - tail.trim_start().len();
let argument = &tail[spaces..];
if let Some((expanded, used)) = expand_macro_at(argument, macros) {
output.push_str(&definition.body);
output.push_str(&expanded);
index += r"\expandafter".len()
+ (after.len() - trimmed.len())
+ kept
+ spaces
+ used;
changed = true;
continue;
}
}
}
}
if rest.starts_with(r"\expandafter") {
if let Some((expanded, used)) = expandafter_chain(rest, macros) {
output.push_str(&expanded);
index += used;
changed = true;
continue;
}
}
if let Some((expanded, used)) = latex_internal_at(rest, macros) {
@@ -9959,6 +9965,14 @@ a & b
assert_eq!(body, "1");
}
#[test]
fn expandafter_chains_through_several_prefixes() {
let (body, _) = geometry_and_body(
"\\def\\a{1}\n\\def\\b{2}\n\\def\\c{3}\n\n\\expandafter\\a\\expandafter\\b\\c",
);
assert_eq!(body, "123");
}
#[test]
fn expandafter_expands_the_following_token_first() {
let (body, _) = geometry_and_body("\\def\\x{y}\n\\def\\y{Z}\n\n\\expandafter\\x\\y");