Consume catcode assignments for every character

Only \catcode`\@ assignments were handled; a class file setting the catcode of
# or _ left the assignment in the text. The canonicalisation pass now consumes
any successfully parsed \catcode assignment (backtick, bare or code-point
target), applying the value only to @ (11/12); other targets are recorded by
their consumption but do not change scanning yet, since the scanner still has
a single modelled catcode.

A preview test sets # and _ to 12, then renders "#hash and a_b." with no
warnings and no leaked assignment.
This commit is contained in:
gloamxun committed 2026-10-27 01:04:23 +08:00
1 parent b16e5a53e8
commit 54a0926552
3 files changed
+34 -8

No files matched your search

+27 -2
View File
@@ -5750,11 +5750,13 @@ fn canonicalize_at_catcodes(source: &str) -> String {
}
"catcode" => {
if let Some((target, value, next)) = catcode_assignment(&characters, end) {
// Only the @ catcode changes scanning; the other
// assignments are consumed so they do not leak as text.
if target == '@' {
at_letter = value == 11;
index = next;
continue;
}
index = next;
continue;
}
}
_ => {}
@@ -15767,6 +15769,29 @@ A definition.
assert!(!text.contains("makeatletter"), "{text}");
}
#[test]
fn other_catcode_assignments_are_consumed() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\catcode`\\#=12\n\\catcode`\\_=12\n\n#hash and a_b.\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("#hash"), "{text}");
assert!(text.contains("a_b."), "{text}");
assert!(!text.contains("catcode"), "{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";