Protect tokens from expansion with noexpand

This commit is contained in:
gloamxun committed 2026-10-26 02:06:23 +08:00
1 parent c74d604c40
commit 4352922240
4 files changed
+71 -6

No files matched your search

+67 -2
View File
@@ -649,6 +649,32 @@ fn expand_macros(source: &str, macros: &[MacroDefinition]) -> String {
let mut changed = false;
while index < text.len() {
let rest = &text[index..];
if let Some(after) = rest.strip_prefix(r"\noexpand") {
let boundary = after
.chars()
.next()
.is_none_or(|character| !character.is_ascii_alphabetic());
if boundary {
let trimmed = after.trim_start();
let skipped = after.len() - trimmed.len();
if trimmed.starts_with('\\') {
if let Some((command, used)) = control_sequence(trimmed) {
output.push('\u{E000}');
output.push_str(command.trim_start_matches('\\'));
index += r"\noexpand".len() + skipped + used;
changed = true;
continue;
}
}
if let Some(character) = trimmed.chars().next() {
output.push('\u{E000}');
output.push(character);
index += r"\noexpand".len() + skipped + character.len_utf8();
changed = true;
continue;
}
}
}
if let Some(after) = rest.strip_prefix(r"\expandafter") {
let boundary = after
.chars()
@@ -702,7 +728,7 @@ fn expand_macros(source: &str, macros: &[MacroDefinition]) -> String {
break;
}
}
text
text.replace('\u{E000}', "\\")
}
/// Find the matching `\end{name}` for an already-opened environment and
@@ -2213,8 +2239,26 @@ fn expand_count_registers(body: &str, registers: &mut Vec<(String, i64)>) -> Str
result
}
/// Later definitions replace earlier ones with the same name, matching TeX's
/// redefinition semantics for `\def`, `\newcommand` and friends.
fn dedupe_macros(macros: &mut Vec<MacroDefinition>) {
let mut result: Vec<MacroDefinition> = Vec::new();
for definition in macros.drain(..) {
if let Some(position) = result
.iter()
.position(|known| known.name == definition.name)
{
result[position] = definition;
} else {
result.push(definition);
}
}
*macros = result;
}
fn geometry_and_body(source: &str) -> (String, PageGeometry) {
let (stripped, mut geometry) = strip_page_geometry(source);
dedupe_macros(&mut geometry.macros);
// Removed assignments can leave blank lines at the edges; they are not
// paragraphs of their own.
let mut body = latex_body(&stripped).trim_matches('\n').to_string();
@@ -8377,7 +8421,11 @@ a & b
let (body, geometry) =
geometry_and_body("\\def\\a{A}\n\\edef\\b{\\a B}\n\\def\\a{C}\n\nBody \\b.");
assert_eq!(body, "Body A B.");
assert_eq!(geometry.macros.len(), 3);
assert_eq!(
geometry.macros.len(),
2,
"the later def replaces the earlier one"
);
}
#[test]
@@ -8468,6 +8516,23 @@ a & b
assert_eq!(body.trim(), "diff");
}
#[test]
fn noexpand_keeps_a_token_for_later_expansion() {
let protected = "\\def\\a{A}\n\\edef\\b{\\noexpand\\a}\n\\def\\a{C}\n\n\\b";
let (body, _) = geometry_and_body(protected);
assert_eq!(body.trim(), "C", "noexpand store should expand at use time");
let expanded = "\\def\\a{A}\n\\edef\\b{\\a}\n\\def\\a{C}\n\n\\b";
let (body, _) = geometry_and_body(expanded);
assert_eq!(body.trim(), "A", "plain edef should capture the old body");
}
#[test]
fn noexpand_keeps_a_character_literal() {
let source = "\\def\\x{A}\n\\edef\\y{\\noexpand\\x}\n\\def\\x{B}\n\n\\y";
let (body, _) = geometry_and_body(source);
assert_eq!(body.trim(), "B");
}
#[test]
fn page_numbers_follow_the_page_counter() {
let offset = preview_json("\\setcounter{page}{5}\n\nAlpha\n\n\\newpage\n\nBeta")