use super::stomach::{CMode, Closer};
use super::{Lowerer, R};
use crate::catcode::Cat;
use crate::charcodes::Table;
use crate::expand::{GroupKind, Meaning, TexError};
use crate::ir::list::{Context, ListOp};
use crate::ir::{Cmd, Num};
use crate::lexer::Lexer;
use crate::stomach::BoxKind;
use crate::token::{CsId, Token};
const ACTIVE_MATH_CODE: i64 = 0x8000;
impl Lowerer {
pub(super) fn st_math(&self) -> bool {
self.st_managed() && self.st_mode().math()
}
pub(super) fn st_math_token(
&mut self,
lx: &mut Lexer,
c: char,
cat: Cat,
out: &mut Vec<Cmd>,
) -> R<bool> {
if !self.st_managed() || self.st_mode() == CMode::Unmanaged {
return Ok(false);
}
let mode = self.st_mode();
if !mode.math() {
if cat != Cat::MathShift {
return Ok(false);
}
if mode.vertical() {
lx.push_back(&[Token::Char(c, cat)]);
self.st_begin_paragraph(lx, out)?;
return Ok(true);
}
self.st_init_math(lx, out)?;
return Ok(true);
}
match cat {
Cat::MathShift => self.st_math_shift(lx, out)?,
Cat::Space => {}
Cat::Letter | Cat::Other => self.st_set_math_char_of(lx, c, out)?,
Cat::BeginGroup => {
self.st_emit(out, ListOp::Noad(0))?;
self.st_align_unbrace();
lx.push_back(&[Token::Char(c, cat)]);
self.st_scan_math(lx, out)?;
}
Cat::Superscript | Cat::Subscript => {
self.st_emit(out, ListOp::SubSup(cat == Cat::Superscript))?;
self.st_scan_math(lx, out)?;
}
_ => return Ok(false),
}
Ok(true)
}
fn st_begin_paragraph(&mut self, lx: &mut Lexer, out: &mut Vec<Cmd>) -> R<()> {
self.st_new_graf(lx, true, out)
}
fn st_init_math(&mut self, lx: &mut Lexer, out: &mut Vec<Cmd>) -> R<()> {
let next = lx.next_token(&self.eng.cats);
let display = matches!(next, Some(Token::Char(_, Cat::MathShift)))
&& self.st_mode() == CMode::Horizontal;
if !display {
if let Some(t) = next {
lx.push_back(&[t]);
}
}
let font = self.eng.cur_font;
self.st_emit(out, ListOp::InitMath { display, font })?;
if display {
self.st_pop();
}
self.st_formula(lx, display, None, out)
}
fn st_formula(
&mut self,
lx: &mut Lexer,
display: bool,
eqno: Option<bool>,
out: &mut Vec<Cmd>,
) -> R<()> {
self.eng
.compile_time_begin_group(GroupKind::MathShift, lx.line());
let mode = match display {
true => CMode::DisplayMath,
false => CMode::Math,
};
self.st_push(mode);
self.st.run_open = false;
let mark = self.globals.len();
let known_mark = self.eng.known.mark();
let display_slots: Vec<i64> = match display {
true => ["predisplaysize", "displaywidth", "displayindent"]
.iter()
.filter_map(|n| crate::params::register_slot(n))
.collect(),
false => Vec::new(),
};
for &slot in &display_slots {
self.eng.known.set(slot, None);
}
if let Some(i) = crate::intpar::index("fam") {
self.eng.assign_intpar(i, -1);
}
let every = match display {
true => "everydisplay",
false => "everymath",
};
if let Some(toks) =
crate::params::toks_register(every).and_then(|r| self.eng.toks.get(&r).cloned())
{
lx.push_back(&toks);
}
let _ = eqno;
let mut body = self.block(lx, None)?;
self.st.run_open = false;
while self.st.nest.len() > 1 && self.st_mode() != mode {
self.st_pop();
}
self.st_pop();
self.eng.compile_time_end_group()?;
let after = self.eng.take_after_group();
lx.push_back(&after);
let (mut saves, keeps) = self.group_parts(&mut body, mark)?;
for &slot in &display_slots {
if !saves.contains(&slot) {
saves.push(slot);
}
}
self.known_after_group(known_mark, &saves, &keeps);
out.push(Cmd::Group { saves, keeps, body });
if display {
self.st_emit(out, ListOp::ResumeAfterDisplay)?;
self.st_push(CMode::Horizontal);
if let Some((t, _)) = self.eng.next_unexpandable(lx)? {
if !t.is_space() {
lx.back_input(&[t]);
}
}
}
Ok(())
}
fn st_fams(&self) -> Vec<u16> {
self.eng.fam_fonts.iter().flatten().copied().collect()
}
fn st_math_shift(&mut self, lx: &mut Lexer, out: &mut Vec<Cmd>) -> R<()> {
if self.eng.innermost_group() != Some(GroupKind::MathShift) {
self.st_off_save(lx, Token::Char('$', Cat::MathShift));
return Ok(());
}
let in_display = self.st_mode() == CMode::DisplayMath || self.st_in_eqno();
if in_display {
self.st_check_dollar(lx)?;
}
let fams = self.st_fams();
self.st_emit(out, ListOp::EndMath(fams))?;
self.st.end_block = true;
Ok(())
}
fn st_in_eqno(&self) -> bool {
let n = self.st.nest.len();
n >= 2 && self.st.nest[n - 1] == CMode::Math && self.st.nest[n - 2] == CMode::DisplayMath
}
fn st_check_dollar(&mut self, lx: &mut Lexer) -> R<()> {
match self.eng.next_unexpandable(lx)? {
Some((Token::Char(_, Cat::MathShift), _)) => {}
other => {
if let Some((t, _)) = other {
lx.back_input(&[t]);
}
self.eng.report(lx, "Display math should end with $$");
}
}
Ok(())
}
fn st_off_save(&mut self, lx: &mut Lexer, t: Token) {
let esc = self.eng.esc().to_string();
let (closer, shown) = match self.eng.innermost_group() {
Some(GroupKind::SemiSimple) => (Token::cs("endgroup"), format!("{esc}endgroup")),
Some(GroupKind::MathShift) => (Token::Char('$', Cat::MathShift), "$".to_string()),
Some(GroupKind::MathLeft) => {
lx.push_back(&[Token::Char('.', Cat::Other)]);
(Token::cs("right"), format!("{esc}right."))
}
_ => (Token::Char('}', Cat::EndGroup), "}".to_string()),
};
lx.push_back(&[closer, t]);
self.eng.report(lx, &format!("Missing {shown} inserted"));
}
fn st_math_code(&mut self, lx: &mut Lexer, c: char) -> Option<i64> {
let code = self.eng.charcodes.get(Table::Math, c);
if code == ACTIVE_MATH_CODE {
lx.push_back(&[Token::Char(c, Cat::Active)]);
return None;
}
Some(code)
}
fn st_set_math_char_of(&mut self, lx: &mut Lexer, c: char, out: &mut Vec<Cmd>) -> R<()> {
if let Some(code) = self.st_math_code(lx, c) {
self.st_emit(out, ListOp::MathChar(Num::Literal(code)))?;
}
Ok(())
}
fn st_scan_math(&mut self, lx: &mut Lexer, out: &mut Vec<Cmd>) -> R<()> {
loop {
let Some((t, p)) = self.st_next_non_blank(lx)? else {
return Err(TexError("Missing { inserted".into()));
};
let code = match t {
Token::Char(c, Cat::Letter | Cat::Other) => match self.st_math_code(lx, c) {
Some(code) => Num::Literal(code),
None => continue,
},
Token::Char(c, Cat::Active) => {
match self.eng.active_meaning(c) {
Some(id) => lx.push_back(&[Token::Cs(id)]),
None => self.eng.report(lx, "Undefined control sequence"),
}
continue;
}
Token::Cs(n) => match self.st_math_char_cs(lx, n, p)? {
Some(MathCode::Code(code)) => code,
Some(MathCode::Reread) => continue,
None => {
lx.push_back(&[t]);
return self.st_math_group(lx, out);
}
},
_ => {
lx.push_back(&[t]);
return self.st_math_group(lx, out);
}
};
return self.st_emit(out, ListOp::MathFieldChar(code));
}
}
fn st_math_group(&mut self, lx: &mut Lexer, out: &mut Vec<Cmd>) -> R<()> {
self.st_emit(out, ListOp::BeginMathGroup)?;
self.st_list_body(lx, CMode::Math, Closer::Math, out)
}
fn st_math_char_cs(&mut self, lx: &mut Lexer, n: CsId, p: Option<CsId>) -> R<Option<MathCode>> {
match self.eng.meanings.get(&n) {
Some(Meaning::CharDef(code)) => {
let code = *code;
let c = char_of(code)?;
return Ok(Some(match self.st_math_code(lx, c) {
Some(m) => MathCode::Code(Num::Literal(m)),
None => MathCode::Reread,
}));
}
Some(Meaning::MathCharDef(v)) => return Ok(Some(MathCode::Code(Num::Literal(*v)))),
Some(Meaning::Char(c, Cat::Letter | Cat::Other)) => {
let c = *c;
return Ok(Some(match self.st_math_code(lx, c) {
Some(m) => MathCode::Code(Num::Literal(m)),
None => MathCode::Reread,
}));
}
_ => {}
}
Ok(match p.unwrap_or(n).name() {
"char" => {
let code = self.st_literal(lx, "\\char")?;
let c = char_of(code)?;
match self.st_math_code(lx, c) {
Some(m) => Some(MathCode::Code(Num::Literal(m))),
None => Some(MathCode::Reread),
}
}
"mathchar" => Some(MathCode::Code(self.number(lx)?)),
"delimiter" => {
let code = self.st_literal(lx, "\\delimiter")?;
Some(MathCode::Code(Num::Literal(code / 0o10000)))
}
_ => None,
})
}
fn st_literal(&mut self, lx: &mut Lexer, what: &str) -> R<i64> {
match self.number(lx)? {
Num::Literal(v) => Ok(v),
_ => Err(TexError(format!(
"{what} needs a number known while lowering"
))),
}
}
fn st_scan_delimiter(&mut self, lx: &mut Lexer) -> R<Num> {
let Some((t, p)) = self.st_next_non_blank(lx)? else {
return Ok(Num::Literal(0));
};
let code = match &t {
Token::Char(c, Cat::Letter | Cat::Other) => {
self.eng.charcodes.get(Table::Delimiter, *c)
}
Token::Cs(n) => match self.eng.meanings.get(n) {
Some(Meaning::Char(c, Cat::Letter | Cat::Other)) => {
self.eng.charcodes.get(Table::Delimiter, *c)
}
_ if p.unwrap_or(*n).name() == "delimiter" => return self.number(lx),
_ => -1,
},
_ => -1,
};
if code < 0 {
lx.back_input(&[t]);
self.eng.report(lx, "Missing delimiter (. inserted)");
return Ok(Num::Literal(0));
}
Ok(Num::Literal(code))
}
pub(super) fn st_math_command(
&mut self,
lx: &mut Lexer,
name: CsId,
out: &mut Vec<Cmd>,
) -> R<bool> {
if let Some(Meaning::Char(c, cat)) = self.eng.meanings.get(&name) {
let (c, cat) = (*c, *cat);
return self.st_math_token(lx, c, cat, out);
}
match self.eng.meanings.get(&name) {
Some(Meaning::CharDef(code)) => {
let c = char_of(*code)?;
self.st_set_math_char_of(lx, c, out)?;
return Ok(true);
}
Some(Meaning::MathCharDef(v)) => {
let v = *v;
self.st_emit(out, ListOp::MathChar(Num::Literal(v)))?;
return Ok(true);
}
_ => {}
}
match name.name() {
"char" => {
let code = self.st_literal(lx, "\\char")?;
let c = char_of(code)?;
self.st_set_math_char_of(lx, c, out)?;
}
"mathchar" => {
let code = self.number(lx)?;
self.st_emit(out, ListOp::MathChar(code))?;
}
"delimiter" => {
let code = self.st_literal(lx, "\\delimiter")?;
self.st_emit(out, ListOp::MathChar(Num::Literal(code / 0o10000)))?;
}
"mathord" | "mathop" | "mathbin" | "mathrel" | "mathopen" | "mathclose"
| "mathpunct" | "mathinner" | "underline" | "overline" => {
let kind = match name.name() {
"mathord" => 0,
"mathop" => 1,
"mathbin" => 2,
"mathrel" => 3,
"mathopen" => 4,
"mathclose" => 5,
"mathpunct" => 6,
"mathinner" => 7,
"underline" => 8,
_ => 9,
};
self.st_emit(out, ListOp::Noad(kind))?;
self.st_scan_math(lx, out)?;
}
"displaylimits" => self.st_emit(out, ListOp::Limits(0))?,
"limits" => self.st_emit(out, ListOp::Limits(1))?,
"nolimits" => self.st_emit(out, ListOp::Limits(2))?,
"radical" => {
let code = self.number(lx)?;
self.st_emit(out, ListOp::Radical(code))?;
self.st_scan_math(lx, out)?;
}
"mathaccent" | "accent" => {
if name.name() == "accent" {
let esc = self.eng.esc().to_string();
self.eng.report(
lx,
&format!("Please use {esc}mathaccent for accents in math mode"),
);
}
let code = self.number(lx)?;
self.st_emit(out, ListOp::MathAccent(code))?;
self.st_scan_math(lx, out)?;
}
"vcenter" => {
self.st_begin_box(lx, BoxKind::VCenter, Context::Shift(Num::Literal(0)), out)?;
}
"displaystyle" => self.st_emit(out, ListOp::MathStyle(0))?,
"textstyle" => self.st_emit(out, ListOp::MathStyle(2))?,
"scriptstyle" => self.st_emit(out, ListOp::MathStyle(4))?,
"scriptscriptstyle" => self.st_emit(out, ListOp::MathStyle(6))?,
"nonscript" => self.st_emit(out, ListOp::NonScript)?,
"mathchoice" => {
self.st_emit(out, ListOp::BeginChoice)?;
for _ in 0..4 {
self.st_list_body(lx, CMode::Math, Closer::Choice, out)?;
}
}
"above" | "over" | "atop" | "abovewithdelims" | "overwithdelims" | "atopwithdelims" => {
let code: u8 = match name.name() {
"above" => 0,
"over" => 1,
"atop" => 2,
"abovewithdelims" => 3,
"overwithdelims" => 4,
_ => 5,
};
let (left, right) = match code >= 3 {
true => (self.st_scan_delimiter(lx)?, self.st_scan_delimiter(lx)?),
false => (Num::Literal(0), Num::Literal(0)),
};
let thickness = match code % 3 {
0 => self.dimen_number(lx, false)?,
_ => Num::Literal(0),
};
self.st_emit(
out,
ListOp::Fraction {
code,
thickness,
left,
right,
},
)?;
}
"left" => {
let delim = self.st_scan_delimiter(lx)?;
self.st_emit(
out,
ListOp::LeftRight {
right: false,
delim,
},
)?;
self.st_left_group(lx, out)?;
}
"right" => match self.eng.innermost_group() {
Some(GroupKind::MathLeft) => {
let delim = self.st_scan_delimiter(lx)?;
self.st_emit(out, ListOp::LeftRight { right: true, delim })?;
self.st.end_block = true;
}
Some(GroupKind::MathShift) => {
let _ = self.st_scan_delimiter(lx)?;
let esc = self.eng.esc().to_string();
self.eng.report(lx, &format!("Extra {esc}right"));
}
_ => self.st_off_save(lx, Token::Cs(name)),
},
"eqno" | "leqno" => {
if self.st_mode() == CMode::DisplayMath
&& self.eng.innermost_group() == Some(GroupKind::MathShift)
{
let leqno = name.name() == "leqno";
self.st_emit(out, ListOp::EqNo(leqno))?;
self.st_formula(lx, false, Some(leqno), out)?;
let fams = self.st_fams();
self.st_emit(out, ListOp::EndMath(fams))?;
self.st.end_block = true;
} else {
let esc = self.eng.esc().to_string();
self.eng.report(
lx,
&format!("You can't use `{esc}{}' in math mode", name.name()),
);
}
}
"mkern" => {
let was = self.eng.set_mu_units(true);
let w = self.dimen_number(lx, false);
self.eng.set_mu_units(was);
let w = w?;
self.st_emit(out, ListOp::MuKern(w))?;
}
"mskip" => {
let spec = self.st_glue_spec(lx, "mskip", true)?;
self.st_emit(
out,
ListOp::Glue {
spec,
mu: true,
leaders: None,
},
)?;
}
_ => return Ok(false),
}
Ok(true)
}
fn st_left_group(&mut self, lx: &mut Lexer, out: &mut Vec<Cmd>) -> R<()> {
self.eng
.compile_time_begin_group(GroupKind::MathLeft, lx.line());
self.st_push(CMode::Math);
let mark = self.globals.len();
let known_mark = self.eng.known.mark();
let mut body = self.block(lx, None)?;
self.st_pop();
self.eng.compile_time_end_group()?;
let after = self.eng.take_after_group();
lx.push_back(&after);
let (saves, keeps) = self.group_parts(&mut body, mark)?;
self.known_after_group(known_mark, &saves, &keeps);
out.push(Cmd::Group { saves, keeps, body });
Ok(())
}
}
enum MathCode {
Code(Num),
Reread,
}
fn char_of(code: i64) -> R<char> {
u32::try_from(code)
.ok()
.and_then(char::from_u32)
.ok_or_else(|| TexError(format!("Bad character code ({code})")))
}