Compare integers and token meanings in conditionals
This commit is contained in:
1 parent
a0f4a35c94
commit
c74d604c40
4 files changed
+180
-9
No files matched your search
+176
-5
@@ -1000,11 +1000,66 @@ fn conditional_assignment(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
enum ConditionalToken {
|
||||
Control(String),
|
||||
Character(char),
|
||||
}
|
||||
|
||||
fn conditional_token(text: &str) -> Option<(ConditionalToken, usize)> {
|
||||
let trimmed = text.trim_start();
|
||||
let skipped = text.len() - trimmed.len();
|
||||
if trimmed.starts_with('\\') {
|
||||
let (command, used) = control_sequence(trimmed)?;
|
||||
return Some((ConditionalToken::Control(command), skipped + used));
|
||||
}
|
||||
let character = trimmed.chars().next()?;
|
||||
Some((
|
||||
ConditionalToken::Character(character),
|
||||
skipped + character.len_utf8(),
|
||||
))
|
||||
}
|
||||
|
||||
fn conditional_operand(
|
||||
text: &str,
|
||||
counts: &[(String, i64)],
|
||||
counters: &[(String, u32)],
|
||||
) -> Option<(i64, usize)> {
|
||||
let trimmed = text.trim_start();
|
||||
let skipped = text.len() - trimmed.len();
|
||||
if let Some(rest) = trimmed.strip_prefix("\\value") {
|
||||
let rest = rest.trim_start();
|
||||
let name = rest.strip_prefix('{')?;
|
||||
let close = name.find('}')?;
|
||||
let key = name[..close].trim();
|
||||
let value = counters
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|(known, _)| known == key)
|
||||
.map(|(_, value)| i64::from(*value))?;
|
||||
return Some((value, skipped + (trimmed.len() - rest.len()) + close + 2));
|
||||
}
|
||||
if trimmed.starts_with('\\') {
|
||||
let (command, used) = control_sequence(trimmed)?;
|
||||
let name = command.trim_start_matches('\\');
|
||||
let value = counts
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|(known, _)| known == name)
|
||||
.map(|(_, value)| *value)?;
|
||||
return Some((value, skipped + used));
|
||||
}
|
||||
let (value, used) = scan_signed_integer(trimmed)?;
|
||||
Some((value, skipped + used))
|
||||
}
|
||||
|
||||
fn conditional_at(
|
||||
text: &str,
|
||||
index: usize,
|
||||
flags: &[ConditionalDefinition],
|
||||
macros: &[MacroDefinition],
|
||||
counts: &[(String, i64)],
|
||||
counters: &[(String, u32)],
|
||||
) -> Option<(bool, usize)> {
|
||||
let (command, consumed) = control_sequence(text.get(index..)?)?;
|
||||
let word = command.strip_prefix('\\')?;
|
||||
@@ -1022,6 +1077,50 @@ fn conditional_at(
|
||||
let defined = macros.iter().any(|definition| definition.name == command);
|
||||
return Some((defined, consumed + skipped + command_consumed));
|
||||
}
|
||||
if word == "ifnum" {
|
||||
let after = text.get(index + consumed..)?;
|
||||
let (left, left_used) = conditional_operand(after, counts, counters)?;
|
||||
let rest = &after[left_used..];
|
||||
let trimmed = rest.trim_start();
|
||||
let skipped = rest.len() - trimmed.len();
|
||||
let relation = trimmed.chars().next()?;
|
||||
if !matches!(relation, '=' | '<' | '>') {
|
||||
return None;
|
||||
}
|
||||
let (right, right_used) = conditional_operand(&trimmed[1..], counts, counters)?;
|
||||
let value = match relation {
|
||||
'=' => left == right,
|
||||
'<' => left < right,
|
||||
_ => left > right,
|
||||
};
|
||||
return Some((value, consumed + left_used + skipped + 1 + right_used));
|
||||
}
|
||||
if word == "ifx" {
|
||||
let after = text.get(index + consumed..)?;
|
||||
let (first, first_used) = conditional_token(after)?;
|
||||
let (second, second_used) = conditional_token(&after[first_used..])?;
|
||||
let value = match (&first, &second) {
|
||||
(ConditionalToken::Character(left), ConditionalToken::Character(right)) => {
|
||||
left == right
|
||||
}
|
||||
(ConditionalToken::Control(left), ConditionalToken::Control(right)) => {
|
||||
match (
|
||||
macros.iter().find(|definition| &definition.name == left),
|
||||
macros.iter().find(|definition| &definition.name == right),
|
||||
) {
|
||||
(Some(left), Some(right)) => {
|
||||
left.body == right.body
|
||||
&& left.args == right.args
|
||||
&& left.default == right.default
|
||||
}
|
||||
(None, None) => left == right,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
return Some((value, consumed + first_used + second_used));
|
||||
}
|
||||
let name = word.strip_prefix("if")?;
|
||||
let flag = flags.iter().find(|flag| flag.name == name)?;
|
||||
Some((flag.value, consumed))
|
||||
@@ -1057,14 +1156,18 @@ fn expand_conditionals(
|
||||
source: &str,
|
||||
flags: &mut [ConditionalDefinition],
|
||||
macros: &[MacroDefinition],
|
||||
counts: &[(String, i64)],
|
||||
counters: &[(String, u32)],
|
||||
) -> String {
|
||||
expand_conditionals_at(source, flags, macros, 0)
|
||||
expand_conditionals_at(source, flags, macros, counts, counters, 0)
|
||||
}
|
||||
|
||||
fn expand_conditionals_at(
|
||||
source: &str,
|
||||
flags: &mut [ConditionalDefinition],
|
||||
macros: &[MacroDefinition],
|
||||
counts: &[(String, i64)],
|
||||
counters: &[(String, u32)],
|
||||
depth: usize,
|
||||
) -> String {
|
||||
if depth > 8 {
|
||||
@@ -1082,7 +1185,9 @@ fn expand_conditionals_at(
|
||||
index = position + consumed;
|
||||
continue;
|
||||
}
|
||||
if let Some((value, consumed)) = conditional_at(source, position, flags, macros) {
|
||||
if let Some((value, consumed)) =
|
||||
conditional_at(source, position, flags, macros, counts, counters)
|
||||
{
|
||||
let body_start = position + consumed;
|
||||
if let Some((else_at, fi_at)) = conditional_end(source, body_start) {
|
||||
let (chosen, after) = match else_at {
|
||||
@@ -1103,7 +1208,14 @@ fn expand_conditionals_at(
|
||||
}
|
||||
};
|
||||
let chosen = chosen.trim_start();
|
||||
output.push_str(&expand_conditionals_at(chosen, flags, macros, depth + 1));
|
||||
output.push_str(&expand_conditionals_at(
|
||||
chosen,
|
||||
flags,
|
||||
macros,
|
||||
counts,
|
||||
counters,
|
||||
depth + 1,
|
||||
));
|
||||
index = after;
|
||||
continue;
|
||||
}
|
||||
@@ -1317,6 +1429,22 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some((command, used)) = control_sequence(trimmed) {
|
||||
let name = command.trim_start_matches('\\');
|
||||
if let Some(position) = geometry
|
||||
.count_registers
|
||||
.iter()
|
||||
.position(|(known, _)| known == name)
|
||||
{
|
||||
let rest = trimmed[used..].trim_start();
|
||||
if let Some(value) = rest.strip_prefix('=') {
|
||||
if let Some((assigned, _)) = scan_signed_integer(value) {
|
||||
geometry.count_registers[position].1 = assigned;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(name) = newif_definition(trimmed) {
|
||||
geometry
|
||||
.conditionals
|
||||
@@ -2093,13 +2221,25 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
geometry.bibliography = extract_bibliography(&mut body);
|
||||
body = expand_environments(&body, &geometry.environments);
|
||||
let macros = geometry.macros.clone();
|
||||
body = expand_conditionals(&body, &mut geometry.conditionals, ¯os);
|
||||
body = expand_conditionals(
|
||||
&body,
|
||||
&mut geometry.conditionals,
|
||||
¯os,
|
||||
&geometry.count_registers,
|
||||
&geometry.counters,
|
||||
);
|
||||
body = expand_macros(&body, ¯os);
|
||||
let mut count_registers = geometry.count_registers.clone();
|
||||
body = expand_count_registers(&body, &mut count_registers);
|
||||
geometry.count_registers = count_registers;
|
||||
body = expand_links(&body);
|
||||
body = expand_conditionals(&body, &mut geometry.conditionals, ¯os);
|
||||
body = expand_conditionals(
|
||||
&body,
|
||||
&mut geometry.conditionals,
|
||||
¯os,
|
||||
&geometry.count_registers,
|
||||
&geometry.counters,
|
||||
);
|
||||
let today = today_string();
|
||||
body = expand_today(&body, &today);
|
||||
(body, geometry)
|
||||
@@ -8297,6 +8437,37 @@ a & b
|
||||
assert_eq!(geometry.count_registers, vec![("depth".to_string(), -3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifnum_compares_literal_integers() {
|
||||
let source = "\\ifnum 2 < 3 smaller\\else bigger\\fi and \\ifnum 3=2 no\\else yes\\fi";
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "smaller and yes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifnum_reads_count_registers() {
|
||||
let source = "\\newcount\\a\n\\a=3\n\n\\ifnum\\a=3 yes\\else no\\fi";
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "yes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifnum_reads_latex_counters() {
|
||||
let source = "\\setcounter{section}{2}\n\n\\ifnum\\value{section}=2 two\\else other\\fi";
|
||||
let (body, _) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "two");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifx_compares_macro_meaning() {
|
||||
let same = "\\def\\x{A}\n\\def\\y{A}\n\n\\ifx\\x\\y same\\else diff\\fi";
|
||||
let (body, _) = geometry_and_body(same);
|
||||
assert_eq!(body.trim(), "same");
|
||||
let different = "\\def\\x{A}\n\\def\\y{B}\n\n\\ifx\\x\\y same\\else diff\\fi";
|
||||
let (body, _) = geometry_and_body(different);
|
||||
assert_eq!(body.trim(), "diff");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_numbers_follow_the_page_counter() {
|
||||
let offset = preview_json("\\setcounter{page}{5}\n\nAlpha\n\n\\newpage\n\nBeta")
|
||||
|
||||
Reference in new issue
Block a user