Freeze unexpanded tokens through an edef
\unexpanded passed its text straight through, so \edef\b{\unexpanded{\a}}
expanded \a at definition time. Split the expander into an inner pass with a
freeze flag: the edef-style wrapper protects each backslash in \unexpanded's
argument with the existing no-expand marker, which the pass restores on the way
out, while the normal document pass still writes the text through so it expands
at use time.
A test defines a=A, \edef b={\unexpanded{\a}}, redefines a=C, and checks \b
prints C with no warnings; the earlier \expanded/\unexpanded/\detokenize test
still passes.
This commit is contained in:
1 parent
4024fa7435
commit
16b61cd368
4 files changed
+60
-10
No files matched your search
+52
-2
@@ -794,6 +794,21 @@ fn brace_depth_delta(line: &str) -> i32 {
|
||||
depth
|
||||
}
|
||||
|
||||
/// Keep `\unexpanded` content out of the current expansion pass: every
|
||||
/// backslash is replaced by the no-expand marker, which the pass restores when
|
||||
/// it finishes, so the tokens expand later instead of now.
|
||||
fn protect_unexpanded(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len() + 4);
|
||||
for character in text.chars() {
|
||||
if character == '\\' {
|
||||
out.push('\u{E000}');
|
||||
} else {
|
||||
out.push(character);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn command_argument(text: &str, command: &str) -> Option<(String, usize)> {
|
||||
let after = text.strip_prefix(command)?;
|
||||
if after
|
||||
@@ -1703,13 +1718,22 @@ fn expandafter_chain(text: &str, macros: &[MacroDefinition]) -> Option<(String,
|
||||
fn expand_macros(source: &str, macros: &[MacroDefinition]) -> String {
|
||||
let mut table = macros.to_vec();
|
||||
let mut tokens = Vec::new();
|
||||
expand_macros_mut(source, &mut table, &mut tokens)
|
||||
expand_macros_inner(source, &mut table, &mut tokens, true)
|
||||
}
|
||||
|
||||
fn expand_macros_mut(
|
||||
source: &str,
|
||||
macros: &mut Vec<MacroDefinition>,
|
||||
tokens: &mut Vec<(String, String)>,
|
||||
) -> String {
|
||||
expand_macros_inner(source, macros, tokens, false)
|
||||
}
|
||||
|
||||
fn expand_macros_inner(
|
||||
source: &str,
|
||||
macros: &mut Vec<MacroDefinition>,
|
||||
tokens: &mut Vec<(String, String)>,
|
||||
freeze_unexpanded: bool,
|
||||
) -> String {
|
||||
let mut text = source.to_string();
|
||||
let mut runtime: Vec<MacroDefinition> = macros.clone();
|
||||
@@ -1758,7 +1782,11 @@ fn expand_macros_mut(
|
||||
continue;
|
||||
}
|
||||
if let Some((body, used)) = command_argument(rest, "\\unexpanded") {
|
||||
output.push_str(&body);
|
||||
if freeze_unexpanded {
|
||||
output.push_str(&protect_unexpanded(&body));
|
||||
} else {
|
||||
output.push_str(&body);
|
||||
}
|
||||
index += used;
|
||||
changed = true;
|
||||
continue;
|
||||
@@ -16305,6 +16333,28 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unexpanded_freezes_edef_body() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{A}\n\\edef\\b{\\unexpanded{\\a}}\n\\def\\a{C}\n\n\\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('C'), "{text}");
|
||||
assert!(!text.contains('A'), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expanded_unexpanded_and_detokenize_work() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\n\\def\\a{Hi}\\expanded{\\a} and \\unexpanded{\\a} and \\detokenize{\\a}.\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user