Execute def and let definitions from the preamble
This commit is contained in:
1 parent
6a4483cf4d
commit
568442a75b
4 files changed
+102
-4
No files matched your search
@@ -207,7 +207,57 @@ fn take_group_after_open(text: &str) -> Option<(String, usize)> {
|
||||
}
|
||||
|
||||
/// Parse `\newcommand{\name}[n]{body}` (and renew/provide variants).
|
||||
fn control_sequence(text: &str) -> Option<(String, usize)> {
|
||||
let after_slash = text.strip_prefix('\\')?;
|
||||
let first = after_slash.chars().next()?;
|
||||
let length = if first.is_ascii_alphabetic() {
|
||||
after_slash
|
||||
.find(|character: char| !character.is_ascii_alphabetic())
|
||||
.unwrap_or(after_slash.len())
|
||||
} else {
|
||||
first.len_utf8()
|
||||
};
|
||||
Some((format!("\\{}", &after_slash[..length]), length + 1))
|
||||
}
|
||||
|
||||
fn definition_argument_count(params: &str) -> Option<u8> {
|
||||
let mut count = 0_u8;
|
||||
let mut characters = params.chars().peekable();
|
||||
while let Some(character) = characters.next() {
|
||||
if character.is_whitespace() {
|
||||
continue;
|
||||
}
|
||||
if character != '#' {
|
||||
return None;
|
||||
}
|
||||
let digit = characters.next()?.to_digit(10)?;
|
||||
if digit == 0 || digit > 9 {
|
||||
return None;
|
||||
}
|
||||
count = count.max(digit as u8);
|
||||
}
|
||||
Some(count)
|
||||
}
|
||||
|
||||
fn macro_alias(line: &str) -> Option<(String, String)> {
|
||||
let rest = line.strip_prefix("\\let")?.trim_start();
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
let rest = rest[consumed..].trim_start();
|
||||
let rest = rest.strip_prefix('=').unwrap_or(rest).trim_start();
|
||||
let (target, _) = control_sequence(rest)?;
|
||||
Some((name, target))
|
||||
}
|
||||
|
||||
fn macro_definition(line: &str) -> Option<MacroDefinition> {
|
||||
if let Some(rest) = line.strip_prefix("\\def") {
|
||||
let rest = rest.trim_start();
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
let rest = &rest[consumed..];
|
||||
let open = rest.find('{')?;
|
||||
let args = definition_argument_count(&rest[..open])?;
|
||||
let (body, _) = take_group_after_open(&rest[open + 1..])?;
|
||||
return Some(MacroDefinition { name, args, body });
|
||||
}
|
||||
let rest = line
|
||||
.strip_prefix("\\newcommand")
|
||||
.or_else(|| line.strip_prefix("\\renewcommand"))
|
||||
@@ -503,12 +553,31 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
if trimmed.starts_with("\\newcommand")
|
||||
|| trimmed.starts_with("\\renewcommand")
|
||||
|| trimmed.starts_with("\\providecommand")
|
||||
|| trimmed.starts_with("\\def")
|
||||
{
|
||||
if let Some(definition) = macro_definition(trimmed) {
|
||||
geometry.macros.push(definition);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some((name, target)) = macro_alias(trimmed) {
|
||||
let definition = geometry
|
||||
.macros
|
||||
.iter()
|
||||
.find(|definition| definition.name == target)
|
||||
.cloned()
|
||||
.unwrap_or(MacroDefinition {
|
||||
name: name.clone(),
|
||||
args: 0,
|
||||
body: String::new(),
|
||||
});
|
||||
geometry.macros.push(MacroDefinition {
|
||||
name,
|
||||
args: definition.args,
|
||||
body: definition.body,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if trimmed.starts_with("\\newtheorem") {
|
||||
if let Some(definition) = newtheorem_definition(trimmed) {
|
||||
geometry.theorem_definitions.push(definition);
|
||||
@@ -4153,6 +4222,35 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
assert!(body.contains("(x,y)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[test]
|
||||
fn def_definitions_expand_in_the_body() {
|
||||
let (body, geometry) = geometry_and_body("\\def\\half{1/2}\n\nA \\half here.");
|
||||
assert_eq!(geometry.macros.len(), 1);
|
||||
assert_eq!(body.trim(), "A 1/2 here.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn def_arguments_are_substituted() {
|
||||
let (body, geometry) =
|
||||
geometry_and_body("\\def\\pair#1#2{(#1, #2)}\n\nA \\pair{a}{b} pair.");
|
||||
assert_eq!(geometry.macros[0].args, 2);
|
||||
assert_eq!(body.trim(), "A (a, b) pair.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn let_aliases_copy_definitions() {
|
||||
let (body, geometry) = geometry_and_body("\\def\\x{A}\n\\let\\y=\\x\n\n\\y b.");
|
||||
assert_eq!(geometry.macros.len(), 2);
|
||||
assert_eq!(body.trim(), "A b.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn let_to_unknown_sequence_stays_quiet() {
|
||||
let (body, _) = geometry_and_body("\\let\\z=\\missing\n\na\\z b");
|
||||
assert_eq!(body.trim(), "a b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn macro_arguments_substitute_in_order() {
|
||||
let definition =
|
||||
|
||||
Reference in new issue
Block a user