use std::collections::HashMap;
use super::noad::{Atom, Class, Field, MathChar, Noad};
use super::parse::Formula;
use crate::catcode::Cat;
use crate::expand::{Engine, Meaning};
use crate::lexer::Lexer;
use crate::token::{CsId, Token};
pub enum Number<'f> {
Step,
Tag(&'f [Token], bool),
Unnumbered,
}
pub struct Place<'f> {
pub row: usize,
pub number: Number<'f>,
pub labels: Vec<&'f [Token]>,
}
pub fn places<'f>(f: &'f Formula, environment: &str) -> Vec<Place<'f>> {
let starred = environment.ends_with('*');
let base = environment.trim_end_matches('*');
let last = f.rows.len().saturating_sub(1);
let tag_in = |row: Option<usize>| {
f.tags
.iter()
.rev()
.find(|(r, _, _)| row.is_none_or(|row| *r == row))
.map(|(_, t, star)| Number::Tag(t.as_slice(), *star))
};
let labels_in = |row: Option<usize>| -> Vec<&'f [Token]> {
f.labels
.iter()
.filter(|(r, _)| row.is_none_or(|row| *r == row))
.map(|(_, key)| key.as_slice())
.collect()
};
match base {
"equation" | "multline" => vec![Place {
row: last,
number: tag_in(None).unwrap_or(match starred || !f.unnumbered.is_empty() {
true => Number::Unnumbered,
false => Number::Step,
}),
labels: labels_in(None),
}],
"align" | "alignat" | "gather" | "eqnarray" => (0..f.rows.len())
.map(|row| Place {
row,
number: tag_in(Some(row)).unwrap_or(match starred || f.unnumbered.contains(&row) {
true => Number::Unnumbered,
false => Number::Step,
}),
labels: labels_in(Some(row)),
})
.collect(),
_ => vec![Place {
row: last,
number: tag_in(None).unwrap_or(Number::Unnumbered),
labels: labels_in(None),
}],
}
}
pub fn spelled(tokens: &[Token]) -> String {
tokens
.iter()
.filter_map(|t| match t {
Token::Char(c, _) => Some(*c),
Token::Cs(_) => None,
})
.collect()
}
const UNITS: [&str; 4] = ["chapter", "section", "subsection", "subsubsection"];
const FLOATS: [&str; 4] = ["figure", "table", "figure*", "table*"];
#[derive(Default)]
pub struct Numbering {
counters: HashMap<String, i64>,
}
impl Numbering {
pub fn watch(&mut self, eng: &mut Engine, lx: &mut Lexer, tok: &Token) -> Option<String> {
use crate::typeset::{ref_mark, REF_CLOSE, REF_OPEN, REF_VALUE};
let Token::Cs(name) = tok else { return None };
let name = name.name();
if FLOATS.contains(&name) {
return Some(ref_mark(REF_OPEN, ""));
}
if name
.strip_prefix("end")
.is_some_and(|f| FLOATS.contains(&f))
{
return Some(ref_mark(REF_CLOSE, ""));
}
if UNITS.contains(&name) {
if eng.skip_optional_star(lx) {
lx.push_back(&[Token::Char('*', Cat::Other)]);
return None;
}
return Some(ref_mark(REF_VALUE, &self.refstep(eng, name)));
}
if name == "caption" {
let captype = spelled(macro_body(eng, "@captype")?);
return Some(ref_mark(REF_VALUE, &self.refstep(eng, &captype)));
}
if name != "setcounter" && name != "addtocounter" {
return None;
}
let mut taken = Vec::new();
let counter = raw_group(eng, lx, &mut taken);
let value = raw_group(eng, lx, &mut taken);
lx.push_back(&taken);
let value = value?.trim().parse::<i64>().ok()?;
let held = self.counters.entry(counter?).or_insert(0);
*held = match name {
"setcounter" => value,
_ => *held + value,
};
None
}
fn refstep(&mut self, eng: &Engine, name: &str) -> String {
self.step(eng, name);
self.the(eng, name)
}
pub fn step(&mut self, eng: &Engine, name: &str) {
*self.counters.entry(name.to_string()).or_insert(0) += 1;
self.reset_within(eng, name, 0);
}
fn reset_within(&mut self, eng: &Engine, name: &str, depth: usize) {
if depth > 8 {
return;
}
for under in elts(eng, &format!("cl@{name}")) {
self.counters.insert(under.clone(), 0);
self.reset_within(eng, &under, depth + 1);
}
}
pub fn the(&self, eng: &Engine, name: &str) -> String {
let body = macro_body(eng, &format!("the{name}"));
body.and_then(|b| self.represent(eng, b, 0))
.unwrap_or_else(|| self.value(name).to_string())
}
fn value(&self, name: &str) -> i64 {
self.counters.get(name).copied().unwrap_or(0)
}
fn represent(&self, eng: &Engine, toks: &[Token], depth: usize) -> Option<String> {
if depth > 16 {
return None;
}
let mut out = String::new();
let mut at = 0;
while at < toks.len() {
match &toks[at] {
Token::Char(_, Cat::Space | Cat::BeginGroup | Cat::EndGroup) => {}
Token::Char(c, _) => out.push(*c),
Token::Cs(cs) => match cs.name() {
style @ ("arabic" | "roman" | "Roman" | "alph" | "Alph") => {
let (counter, used) = braced(&toks[at + 1..])?;
out.push_str(&styled(style, self.value(&counter))?);
at += used;
}
style @ ("@arabic" | "@roman" | "@Roman" | "@alph" | "@Alph" | "number") => {
let Some(Token::Cs(register)) = toks.get(at + 1) else {
return None;
};
let counter = register.name().strip_prefix("c@")?;
let style = style.trim_start_matches('@');
let style = if style == "number" { "arabic" } else { style };
out.push_str(&styled(style, self.value(counter))?);
at += 1;
}
"relax" => {}
"ifnum" => {
let (holds, after) = self.compare(&toks[at + 1..])?;
let body = &toks[at + 1 + after..];
let (then, otherwise, end) = branches(body)?;
let taken = if holds { then } else { otherwise };
out.push_str(&self.represent(eng, taken, depth + 1)?);
at += 1 + after + end;
}
other => {
out.push_str(&self.represent(eng, macro_body(eng, other)?, depth + 1)?)
}
},
}
at += 1;
}
Some(out)
}
fn compare(&self, toks: &[Token]) -> Option<(bool, usize)> {
let (left, used) = self.operand(toks)?;
let mut at = used + spaces(&toks[used..]);
let Some(Token::Char(relation @ ('<' | '=' | '>'), _)) = toks.get(at) else {
return None;
};
at += 1;
let (right, used) = self.operand(&toks[at..])?;
let holds = match relation {
'<' => left < right,
'=' => left == right,
_ => left > right,
};
Some((holds, at + used))
}
fn operand(&self, toks: &[Token]) -> Option<(i64, usize)> {
let at = spaces(toks);
match toks.get(at)? {
Token::Cs(cs) => {
let value = match cs.name() {
"value" => {
let (counter, used) = braced(&toks[at + 1..])?;
return Some((self.value(&counter), at + 1 + used));
}
"count" => {
let Some(Token::Cs(register)) = toks.get(at + 1) else {
return None;
};
let counter = register.name().strip_prefix("cr@")?;
return Some((self.value(counter), at + 2));
}
"z@" => 0,
"@ne" => 1,
"tw@" => 2,
"m@ne" => -1,
name => self.value(name.strip_prefix("c@")?),
};
Some((value, at + 1))
}
Token::Char(..) => {
let digits: String = toks[at..]
.iter()
.map_while(|t| match t {
Token::Char(c, _) if c.is_ascii_digit() || *c == '-' => Some(*c),
_ => None,
})
.collect();
Some((digits.parse().ok()?, at + digits.len()))
}
}
}
}
fn spaces(toks: &[Token]) -> usize {
toks.iter().take_while(|t| t.is_space()).count()
}
fn branches(toks: &[Token]) -> Option<(&[Token], &[Token], usize)> {
let mut depth = 0usize;
let mut otherwise = None;
for (at, t) in toks.iter().enumerate() {
let Token::Cs(cs) = t else { continue };
match cs.name() {
"fi" if depth == 0 => {
let then_end = otherwise.unwrap_or(at);
let else_start = otherwise.map_or(at, |e| e + 1);
return Some((&toks[..then_end], &toks[else_start..at], at));
}
"fi" => depth -= 1,
"else" if depth == 0 => otherwise = Some(at),
name if name.starts_with("if") => depth += 1,
_ => {}
}
}
None
}
pub fn left(eng: &Engine) -> bool {
macro_body(eng, "@eqnnum").is_some_and(|b| {
b.iter()
.any(|t| matches!(t, Token::Cs(c) if c.name() == "displaywidth"))
})
}
pub fn mlist(text: &str) -> Vec<Noad> {
text.chars()
.map(|c| {
let field = match u8::try_from(c) {
Ok(b) if b.is_ascii() => Field::Char(MathChar {
fam: 0,
character: b,
}),
_ => Field::Literal(c),
};
Noad::Atom(Atom::new(Class::Ord, field))
})
.collect()
}
fn macro_body<'e>(eng: &'e Engine, name: &str) -> Option<&'e [Token]> {
match eng.meanings.get(&CsId::intern(name)) {
Some(Meaning::Macro(m)) if m.params.is_empty() => Some(&m.body),
_ => None,
}
}
fn elts(eng: &Engine, list: &str) -> Vec<String> {
let Some(body) = macro_body(eng, list) else {
return Vec::new();
};
let mut out = Vec::new();
for (at, t) in body.iter().enumerate() {
if matches!(t, Token::Cs(c) if c.name() == "@elt") {
if let Some((name, _)) = braced(&body[at + 1..]) {
out.push(name);
}
}
}
out
}
fn braced(toks: &[Token]) -> Option<(String, usize)> {
match toks.first()? {
Token::Char(_, Cat::BeginGroup) => {
let close = toks
.iter()
.position(|t| matches!(t, Token::Char(_, Cat::EndGroup)))?;
Some((spelled(&toks[1..close]), close + 1))
}
Token::Char(c, _) => Some((c.to_string(), 1)),
Token::Cs(_) => None,
}
}
fn raw_group(eng: &Engine, lx: &mut Lexer, taken: &mut Vec<Token>) -> Option<String> {
let mut first = lx.next_token(&eng.cats)?;
while first.is_space() {
taken.push(first);
first = lx.next_token(&eng.cats)?;
}
taken.push(first);
if !matches!(first, Token::Char(_, Cat::BeginGroup)) {
return None;
}
let mut depth = 1usize;
let mut inner = Vec::new();
while let Some(t) = lx.next_token(&eng.cats) {
taken.push(t);
match t {
Token::Char(_, Cat::BeginGroup) => depth += 1,
Token::Char(_, Cat::EndGroup) => {
depth -= 1;
if depth == 0 {
return Some(spelled(&inner));
}
}
_ => {}
}
inner.push(t);
}
None
}
fn styled(style: &str, n: i64) -> Option<String> {
let letter = |base: u8| match n {
1..=26 => Some(((base + (n - 1) as u8) as char).to_string()),
_ => None,
};
match style {
"arabic" => Some(n.to_string()),
"alph" => letter(b'a'),
"Alph" => letter(b'A'),
"roman" => Some(roman(n)),
"Roman" => Some(roman(n).to_uppercase()),
_ => None,
}
}
fn roman(mut n: i64) -> String {
const DIGITS: [(i64, &str); 13] = [
(1000, "m"),
(900, "cm"),
(500, "d"),
(400, "cd"),
(100, "c"),
(90, "xc"),
(50, "l"),
(40, "xl"),
(10, "x"),
(9, "ix"),
(5, "v"),
(4, "iv"),
(1, "i"),
];
let mut out = String::new();
for (value, digits) in DIGITS {
while n >= value {
out.push_str(digits);
n -= value;
}
}
out
}