Parse delimited macro arguments
This commit is contained in:
1 parent
269c633233
commit
d1d6672503
3 files changed
+150
-5
No files matched your search
+147
-2
@@ -292,6 +292,8 @@ struct MacroDefinition {
|
||||
args: u8,
|
||||
default: Option<String>,
|
||||
body: String,
|
||||
/// Literal parameter text when the macro uses delimited arguments.
|
||||
params: Option<String>,
|
||||
}
|
||||
|
||||
/// A `\newenvironment` definition expanded textually in the body.
|
||||
@@ -392,6 +394,23 @@ fn definition_argument_count(params: &str) -> Option<u8> {
|
||||
Some(count)
|
||||
}
|
||||
|
||||
fn parameter_placeholders(params: &str) -> Option<u8> {
|
||||
let mut count = 0_u8;
|
||||
let mut characters = params.chars();
|
||||
while let Some(character) = characters.next() {
|
||||
if character == '#' {
|
||||
let digit = characters.next()?.to_digit(10)?;
|
||||
if digit == 0 {
|
||||
return None;
|
||||
}
|
||||
count = count.max(digit as u8);
|
||||
} else if character == '\\' {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(count)
|
||||
}
|
||||
|
||||
fn expand_csnames(text: &str) -> String {
|
||||
let mut result = String::with_capacity(text.len());
|
||||
let mut rest = text;
|
||||
@@ -450,7 +469,14 @@ fn edef_definition(line: &str, macros: &[MacroDefinition]) -> Option<MacroDefini
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
let rest = &rest[consumed..];
|
||||
let open = rest.find('{')?;
|
||||
let args = definition_argument_count(&rest[..open])?;
|
||||
let params_text = &rest[..open];
|
||||
let (args, params) = match definition_argument_count(params_text) {
|
||||
Some(args) => (args, None),
|
||||
None => (
|
||||
parameter_placeholders(params_text)?,
|
||||
Some(params_text.to_string()),
|
||||
),
|
||||
};
|
||||
let (body, _) = take_group_after_open(&rest[open + 1..])?;
|
||||
let mut body = expand_csnames(&body);
|
||||
if args == 0 {
|
||||
@@ -461,6 +487,7 @@ fn edef_definition(line: &str, macros: &[MacroDefinition]) -> Option<MacroDefini
|
||||
args,
|
||||
default: None,
|
||||
body,
|
||||
params,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -482,13 +509,21 @@ fn macro_definition(line: &str) -> Option<MacroDefinition> {
|
||||
let (name, consumed) = control_sequence(rest)?;
|
||||
let rest = &rest[consumed..];
|
||||
let open = rest.find('{')?;
|
||||
let args = definition_argument_count(&rest[..open])?;
|
||||
let params_text = &rest[..open];
|
||||
let (args, params) = match definition_argument_count(params_text) {
|
||||
Some(args) => (args, None),
|
||||
None => (
|
||||
parameter_placeholders(params_text)?,
|
||||
Some(params_text.to_string()),
|
||||
),
|
||||
};
|
||||
let (body, _) = take_group_after_open(&rest[open + 1..])?;
|
||||
return Some(MacroDefinition {
|
||||
name,
|
||||
args,
|
||||
default: None,
|
||||
body,
|
||||
params,
|
||||
});
|
||||
}
|
||||
let rest = line
|
||||
@@ -539,6 +574,7 @@ fn macro_definition(line: &str) -> Option<MacroDefinition> {
|
||||
args,
|
||||
default,
|
||||
body,
|
||||
params: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -665,6 +701,9 @@ fn expand_macro_at(text: &str, macros: &[MacroDefinition]) -> Option<(String, us
|
||||
}
|
||||
let name = format!("\\{}", &after_slash[..length]);
|
||||
let definition = macros.iter().find(|macro_| macro_.name == name)?;
|
||||
if let Some(params) = &definition.params {
|
||||
return expand_delimited(text, definition, params);
|
||||
}
|
||||
let tail = &after_slash[length..];
|
||||
let mut args = Vec::new();
|
||||
let mut consumed = 0_usize;
|
||||
@@ -692,6 +731,80 @@ fn expand_macro_at(text: &str, macros: &[MacroDefinition]) -> Option<(String, us
|
||||
|
||||
/// Expand the small set of LaTeX internal commands used by class and package
|
||||
/// files. Returns the replacement text and the bytes consumed.
|
||||
/// Expand a macro whose parameter text contains delimiters.
|
||||
fn expand_delimited(
|
||||
text: &str,
|
||||
definition: &MacroDefinition,
|
||||
params: &str,
|
||||
) -> Option<(String, usize)> {
|
||||
let after_slash = text.strip_prefix('\\')?;
|
||||
let length = after_slash
|
||||
.find(|character: char| !character.is_ascii_alphabetic())
|
||||
.unwrap_or(after_slash.len());
|
||||
let tail = &after_slash[length..];
|
||||
let mut leading = String::new();
|
||||
let mut delimiters: Vec<String> = Vec::new();
|
||||
let mut literal = String::new();
|
||||
let mut characters = params.chars();
|
||||
let mut first = true;
|
||||
while let Some(character) = characters.next() {
|
||||
if character == '#' {
|
||||
let digit = characters.next()?.to_digit(10)?;
|
||||
if first {
|
||||
leading = std::mem::take(&mut literal);
|
||||
first = false;
|
||||
} else {
|
||||
delimiters.push(std::mem::take(&mut literal));
|
||||
}
|
||||
if digit == 0 || digit as u8 != delimiters.len() as u8 + 1 {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
literal.push(character);
|
||||
}
|
||||
}
|
||||
delimiters.push(literal);
|
||||
let mut consumed = 1 + length;
|
||||
let mut rest = tail;
|
||||
let trimmed = rest.trim_start();
|
||||
consumed += rest.len() - trimmed.len();
|
||||
rest = trimmed;
|
||||
if !leading.is_empty() {
|
||||
rest = rest.strip_prefix(&leading)?;
|
||||
consumed += leading.len();
|
||||
}
|
||||
let mut args = Vec::new();
|
||||
for delimiter in &delimiters {
|
||||
if delimiter.is_empty() {
|
||||
let trimmed = rest.trim_start();
|
||||
consumed += rest.len() - trimmed.len();
|
||||
rest = trimmed;
|
||||
if let Some(after_open) = rest.strip_prefix('{') {
|
||||
let (argument, used) = take_group_after_open(after_open)?;
|
||||
args.push(argument);
|
||||
consumed += 1 + used;
|
||||
rest = &after_open[used..];
|
||||
} else {
|
||||
let character = rest.chars().next()?;
|
||||
args.push(character.to_string());
|
||||
consumed += character.len_utf8();
|
||||
rest = &rest[character.len_utf8()..];
|
||||
}
|
||||
} else {
|
||||
let position = rest.find(delimiter)?;
|
||||
let mut argument = rest[..position].to_string();
|
||||
if argument.starts_with('{') && argument.ends_with('}') {
|
||||
argument = argument[1..argument.len() - 1].to_string();
|
||||
}
|
||||
args.push(argument);
|
||||
consumed += position + delimiter.len();
|
||||
rest = &rest[position + delimiter.len()..];
|
||||
}
|
||||
}
|
||||
let expanded = substitute_macro_args(&definition.body, &args);
|
||||
Some((expanded, consumed))
|
||||
}
|
||||
|
||||
fn latex_internal_at(rest: &str, macros: &[MacroDefinition]) -> Option<(String, usize)> {
|
||||
let boundary = |after: &str| {
|
||||
after
|
||||
@@ -1925,6 +2038,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
args: 0,
|
||||
default: None,
|
||||
body: operator,
|
||||
params: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1952,6 +2066,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
args: 0,
|
||||
default: None,
|
||||
body: body.to_string(),
|
||||
params: None,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
@@ -2005,12 +2120,14 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
args: 0,
|
||||
default: None,
|
||||
body: String::new(),
|
||||
params: None,
|
||||
});
|
||||
geometry.macros.push(MacroDefinition {
|
||||
name,
|
||||
args: definition.args,
|
||||
default: definition.default,
|
||||
body: definition.body,
|
||||
params: None,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
@@ -11149,6 +11266,31 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_macro_arguments_parse() {
|
||||
let source = concat!(r"\def\pair#1,#2{(#1|#2)}", "\n\n", r"Body \pair a,b.");
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "Body (a|b).");
|
||||
assert_eq!(geometry.macros[0].params.as_deref(), Some("#1,#2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_arguments_accept_braced_groups() {
|
||||
let source = concat!(
|
||||
r"\def\swap#1/#2{(#2/#1)}",
|
||||
"\n\n",
|
||||
r"\swap {left right}/{right side}."
|
||||
);
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert!(body.contains("(right side/left right)."), "{body:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delimited_arguments_allow_a_leading_literal() {
|
||||
let source = concat!(r"\def\angle<#1>{<#1>}", "\n\n", r"\angle<x>.");
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert!(body.contains("<x>."), "{body:?}");
|
||||
}
|
||||
|
||||
fn newcommand_definitions_expand_in_the_body() {
|
||||
let source = "\\newcommand{\\R}{\\mathbb{R}}\n\\newcommand{\\pair}[2]{(#1,#2)}\n\n$\\R$ and $\\pair{x}{y}$";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
@@ -11220,12 +11362,14 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
args: 0,
|
||||
default: None,
|
||||
body: "\\b".into(),
|
||||
params: None,
|
||||
},
|
||||
MacroDefinition {
|
||||
name: "\\b".into(),
|
||||
args: 0,
|
||||
default: None,
|
||||
body: "B".into(),
|
||||
params: None,
|
||||
},
|
||||
];
|
||||
assert_eq!(expand_macros("\\a", &definitions), "B");
|
||||
@@ -11234,6 +11378,7 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
args: 0,
|
||||
default: None,
|
||||
body: "\\loop".into(),
|
||||
params: None,
|
||||
}];
|
||||
assert_eq!(expand_macros("\\loop", &recursive), "\\loop");
|
||||
assert_eq!(expand_macros("\\missing", &definitions), "\\missing");
|
||||
|
||||
Reference in new issue
Block a user