Define character commands with chardef
This commit is contained in:
1 parent
e81c82cf61
commit
d362d189ac
3 files changed
+55
-3
No files matched your search
@@ -459,6 +459,38 @@ fn strip_definition_prefixes(line: &str) -> &str {
|
||||
}
|
||||
}
|
||||
|
||||
fn char_definition(line: &str) -> Option<MacroDefinition> {
|
||||
let rest = line.strip_prefix("\\chardef")?;
|
||||
let rest = rest.trim_start();
|
||||
let (name, used) = control_sequence(rest)?;
|
||||
let rest = rest[used..].trim_start();
|
||||
let rest = rest.strip_prefix('=')?.trim_start();
|
||||
let value = if let Some(hex) = rest.strip_prefix('"') {
|
||||
let digits: String = hex.chars().take_while(|c| c.is_ascii_hexdigit()).collect();
|
||||
u32::from_str_radix(&digits, 16).ok()?
|
||||
} else if let Some(octal) = rest.strip_prefix('\'') {
|
||||
let digits: String = octal
|
||||
.chars()
|
||||
.take_while(|c| ('0'..='7').contains(c))
|
||||
.collect();
|
||||
u32::from_str_radix(&digits, 8).ok()?
|
||||
} else if let Some(tail) = rest.strip_prefix('`') {
|
||||
let character = tail.chars().next()?;
|
||||
character as u32
|
||||
} else {
|
||||
let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
|
||||
digits.parse().ok()?
|
||||
};
|
||||
let character = char::from_u32(value)?;
|
||||
Some(MacroDefinition {
|
||||
name,
|
||||
args: 0,
|
||||
default: None,
|
||||
body: character.to_string(),
|
||||
params: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn edef_definition(line: &str, macros: &[MacroDefinition]) -> Option<MacroDefinition> {
|
||||
let rest = line
|
||||
.strip_prefix(r"\edef")
|
||||
@@ -2106,6 +2138,12 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
continue;
|
||||
}
|
||||
let definition_line = strip_definition_prefixes(trimmed);
|
||||
if trimmed.starts_with("\\chardef") {
|
||||
if let Some(definition) = char_definition(trimmed) {
|
||||
geometry.macros.push(definition);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if definition_line.starts_with(r"\edef")
|
||||
|| definition_line.starts_with(r"\xdef")
|
||||
|| definition_line.starts_with(r"\protected@edef")
|
||||
@@ -10321,6 +10359,20 @@ a & b
|
||||
assert_eq!(body.trim(), "no");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chardef_defines_character_commands() {
|
||||
let source = concat!(
|
||||
r"\chardef\letter=65",
|
||||
"\n",
|
||||
r#"\chardef\quote="22"#,
|
||||
"\n\n",
|
||||
r"\letter\quote"
|
||||
);
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "A\"");
|
||||
assert_eq!(geometry.macros.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifcat_compares_character_categories() {
|
||||
let letters = "\\ifcat a b same\\else diff\\fi";
|
||||
|
||||
Reference in new issue
Block a user