Honor \catcode`\@ assignments
Class files often set the @ catcode directly instead of calling \makeatletter. Recognise \catcode assignments in the canonicalisation pass for the @ target: the target may be written with a backtick, bare, or as a code point, the value must be numeric (11 letter, 12 other) or \active, and the assignment is consumed and updates the same state \makeatletter toggles. Assignments for other characters are left untouched so nothing is silently dropped. A preview test sets \catcode`\@=11, defines and uses \@foo, and checks "bar" with no warnings and no leaked assignment.
This commit is contained in:
1 parent
b056b11ba9
commit
34d54d91a4
3 files changed
+102
-5
No files matched your search
+96
-1
@@ -5286,7 +5286,70 @@ fn is_internal_at_command(name: &str) -> bool {
|
||||
.is_some_and(|rest| INTERNAL_AT_COMMANDS.contains(&rest))
|
||||
}
|
||||
|
||||
/// Model the one catcode the engine understands: `@` is a letter only between
|
||||
/// Parse the `\catcode` target and value that follow the command, supporting
|
||||
/// the backtick form (`\catcode`\@=11`), a bare character, or a code point.
|
||||
fn catcode_assignment(characters: &[char], start: usize) -> Option<(char, i64, usize)> {
|
||||
let mut index = start;
|
||||
let skip_space = |index: &mut usize| {
|
||||
while *index < characters.len() && characters[*index].is_whitespace() {
|
||||
*index += 1;
|
||||
}
|
||||
};
|
||||
skip_space(&mut index);
|
||||
if index < characters.len() && characters[index] == '`' {
|
||||
index += 1;
|
||||
}
|
||||
if index >= characters.len() {
|
||||
return None;
|
||||
}
|
||||
let target = if characters[index] == '\\' {
|
||||
let character = *characters.get(index + 1)?;
|
||||
index += 2;
|
||||
character
|
||||
} else if characters[index].is_ascii_digit() {
|
||||
let mut value = 0_i64;
|
||||
while index < characters.len() && characters[index].is_ascii_digit() {
|
||||
value = value * 10 + characters[index].to_digit(10)? as i64;
|
||||
index += 1;
|
||||
}
|
||||
char::from_u32(u32::try_from(value).ok()?)?
|
||||
} else {
|
||||
let character = characters[index];
|
||||
index += 1;
|
||||
character
|
||||
};
|
||||
skip_space(&mut index);
|
||||
if index >= characters.len() || characters[index] != '=' {
|
||||
return None;
|
||||
}
|
||||
index += 1;
|
||||
skip_space(&mut index);
|
||||
if index >= characters.len() {
|
||||
return None;
|
||||
}
|
||||
if characters[index].is_ascii_digit() {
|
||||
let mut value = 0_i64;
|
||||
while index < characters.len() && characters[index].is_ascii_digit() {
|
||||
value = value * 10 + characters[index].to_digit(10)? as i64;
|
||||
index += 1;
|
||||
}
|
||||
return Some((target, value, index));
|
||||
}
|
||||
// `\active` and friends are not modelled; consume the word and treat the
|
||||
// target as active (13).
|
||||
let mut word = String::new();
|
||||
while index < characters.len() && characters[index].is_ascii_alphabetic() {
|
||||
word.push(characters[index]);
|
||||
index += 1;
|
||||
}
|
||||
if word == "active" {
|
||||
Some((target, 13, index))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Model the catcode the engine understands: `@` is a letter only between
|
||||
/// `\makeatletter` and `\makeatother`, and grouping restores the state. User
|
||||
/// command names containing `@` are rewritten with a private-use placeholder
|
||||
/// so later scanners still see one control word; route-D's own `\@...` names
|
||||
@@ -5394,6 +5457,15 @@ fn canonicalize_at_catcodes(source: &str) -> String {
|
||||
index = end;
|
||||
continue;
|
||||
}
|
||||
"catcode" => {
|
||||
if let Some((target, value, next)) = catcode_assignment(&characters, end) {
|
||||
if target == '@' {
|
||||
at_letter = value == 11;
|
||||
index = next;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
out.push('\\');
|
||||
@@ -15410,6 +15482,29 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catcode_assignments_toggle_at_letter() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\catcode`\\@=11\n\\def\\@foo{bar}\n\\@foo\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("bar"), "{text}");
|
||||
assert!(!text.contains("catcode"), "{text}");
|
||||
assert!(!text.contains("@foo"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_definitions_accept_parameters() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\add#1{[#1]}\\def\\pair#1,#2{(#1|#2)}\n\n\\add{x} and \\pair a,b\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user