use crate::catcode::{cat_from_i64, Cat, CatTable};
use crate::lexer::Lexer;
use crate::token::{CsId, Token};
use std::collections::HashMap;
#[derive(Clone, PartialEq)]
pub struct Macro {
pub params: Vec<Token>,
pub body: Vec<Token>,
}
#[derive(Clone, PartialEq)]
pub enum Meaning {
Macro(Macro),
Primitive(CsId),
Char(char, Cat),
}
enum Save {
Cat(char, Cat),
Count(i64, Option<i64>),
Meaning(CsId, Option<Meaning>),
Intercepts(crate::intercepts::Registry),
}
pub struct Engine {
pub cats: CatTable,
pub meanings: HashMap<CsId, Meaning>,
pub count: HashMap<i64, i64>,
pub messages: Vec<String>,
pub escape: char,
groups: Vec<Vec<Save>>,
conds: Vec<CondState>,
global: bool,
pub intercepts: crate::intercepts::Registry,
advice_depth: u32,
}
fn wrapped(body: &[Token]) -> Vec<Token> {
let mut v = Vec::with_capacity(body.len() + 2);
v.push(Token::cs(ADVICE_IN));
v.extend(body.iter().copied());
v.push(Token::cs(ADVICE_OUT));
v
}
pub const ADVICE_IN: &str = "\u{0}advice-in";
pub const ADVICE_OUT: &str = "\u{0}advice-out";
#[derive(PartialEq, Clone, Copy)]
enum CondState {
Taken,
Done,
}
#[derive(Debug)]
pub struct TexError(pub String);
type R<T> = Result<T, TexError>;
const CONDITIONALS: &[&str] = &[
"if",
"ifcat",
"ifnum",
"ifdim",
"ifodd",
"ifvmode",
"ifhmode",
"ifmmode",
"ifinner",
"ifvoid",
"ifhbox",
"ifvbox",
"ifx",
"ifeof",
"iftrue",
"iffalse",
"ifcase",
"ifdefined",
"ifcsname",
];
impl Engine {
pub fn new() -> Self {
Self {
cats: CatTable::new(),
meanings: HashMap::new(),
count: HashMap::new(),
messages: Vec::new(),
escape: '\\',
groups: Vec::new(),
conds: Vec::new(),
global: false,
intercepts: crate::intercepts::Registry::new(),
advice_depth: 0,
}
}
pub fn run(&mut self, src: &str) -> R<()> {
let mut lx = Lexer::new(src);
while let Some(tok) = lx.next_token(&self.cats) {
if self.step(&mut lx, tok)? {
break;
}
}
Ok(())
}
fn begin_group(&mut self) {
self.groups.push(Vec::new());
}
fn end_group(&mut self) -> R<()> {
let Some(frame) = self.groups.pop() else {
return Err(TexError("Too many }'s".into()));
};
for save in frame.into_iter().rev() {
match save {
Save::Cat(c, cat) => self.cats.set(c, cat),
Save::Count(reg, old) => match old {
Some(v) => {
self.count.insert(reg, v);
}
None => {
self.count.remove(®);
}
},
Save::Meaning(name, old) => match old {
Some(m) => {
self.meanings.insert(name, m);
}
None => {
self.meanings.remove(&name);
}
},
Save::Intercepts(old) => self.intercepts = old,
}
}
Ok(())
}
fn save(&mut self, rec: Save) {
if self.global || self.groups.is_empty() {
return;
}
if let Some(frame) = self.groups.last_mut() {
frame.push(rec);
}
}
fn set_meaning(&mut self, name: CsId, m: Meaning) {
let old = self.meanings.get(&name).cloned();
self.save(Save::Meaning(name, old));
if self.global {
for frame in &mut self.groups {
frame.retain(|s| !matches!(s, Save::Meaning(n, _) if *n == name));
}
}
self.meanings.insert(name, m);
}
fn set_count(&mut self, reg: i64, val: i64) {
let old = self.count.get(®).copied();
self.save(Save::Count(reg, old));
self.count.insert(reg, val);
}
fn set_cat(&mut self, c: char, cat: Cat) {
self.save(Save::Cat(c, self.cats.get(c)));
self.cats.set(c, cat);
}
fn step(&mut self, lx: &mut Lexer, tok: Token) -> R<bool> {
match &tok {
Token::Char(_, Cat::BeginGroup) => {
self.begin_group();
return Ok(false);
}
Token::Char(_, Cat::EndGroup) => {
self.end_group()?;
return Ok(false);
}
Token::Char(..) => return Ok(false),
Token::Cs(_) => {}
}
let Token::Cs(name) = &tok else {
return Ok(false);
};
let name = *name;
if self.try_expand(lx, name, false)? {
return Ok(false);
}
match name.name() {
"end" => return Ok(true),
kind @ ("def" | "gdef" | "edef" | "xdef") => self.do_def(lx, kind)?,
"let" => self.do_let(lx)?,
"global" => {
self.global = true;
let Some(next) = lx.next_token(&self.cats) else {
return Err(TexError("Missing control sequence".into()));
};
let out = self.step(lx, next);
self.global = false;
return out;
}
"begingroup" => self.begin_group(),
"endgroup" => self.end_group()?,
"catcode" => self.do_catcode(lx)?,
"count" => self.do_count_assign(lx)?,
"advance" => self.do_arith(lx, Arith::Add)?,
"multiply" => self.do_arith(lx, Arith::Mul)?,
"divide" => self.do_arith(lx, Arith::Div)?,
"message" => {
let text = self.read_group_text(lx)?;
self.messages.push(text);
}
"relax" | "par" | "ignorespaces" => {}
other => {
return Err(TexError(format!("Undefined control sequence \\{other}")));
}
}
Ok(false)
}
fn try_expand(&mut self, lx: &mut Lexer, name: CsId, pending_only: bool) -> R<bool> {
if let Some(Meaning::Macro(_)) = self.meanings.get(&name) {
self.expand_macro(lx, name, pending_only)?;
return Ok(true);
}
match name.name() {
ADVICE_IN | ADVICE_OUT => {
self.advice_marker(name.name());
Ok(true)
}
n if CONDITIONALS.contains(&n) => {
self.do_conditional(lx, n, pending_only)?;
Ok(true)
}
"else" => {
match self.conds.pop() {
Some(_) => {
self.skip_to(lx, false, pending_only)?;
Ok(true)
}
None => Err(TexError("Extra \\else".into())),
}
}
"or" => match self.conds.pop() {
Some(_) => {
self.skip_to(lx, false, pending_only)?;
Ok(true)
}
None => Err(TexError("Extra \\or".into())),
},
"fi" => match self.conds.pop() {
Some(_) => Ok(true),
None => Err(TexError("Extra \\fi".into())),
},
"expandafter" => {
self.do_expandafter(lx, pending_only)?;
Ok(true)
}
"noexpand" => {
if let Some(t) = self.take(lx, pending_only) {
lx.push_back(&[t]);
}
Ok(true)
}
"csname" => {
let built = self.read_csname(lx, pending_only)?;
let id = CsId::intern(&built);
self.meanings
.entry(id)
.or_insert_with(|| Meaning::Primitive(CsId::intern("relax")));
lx.push_back(&[Token::Cs(id)]);
Ok(true)
}
_ => Ok(false),
}
}
fn take(&mut self, lx: &mut Lexer, pending_only: bool) -> Option<Token> {
match pending_only {
true => lx.pending.pop(),
false => lx.next_token(&self.cats),
}
}
fn do_expandafter(&mut self, lx: &mut Lexer, pending_only: bool) -> R<()> {
let Some(held) = self.take(lx, pending_only) else {
return Err(TexError("Missing token after \\expandafter".into()));
};
let Some(next) = self.take(lx, pending_only) else {
return Err(TexError("Missing token after \\expandafter".into()));
};
match &next {
Token::Cs(n) => {
let n = *n;
if !self.try_expand(lx, n, true)? {
lx.push_back(&[next]);
}
}
_ => lx.push_back(&[next]),
}
lx.push_back(&[held]);
Ok(())
}
fn do_conditional(&mut self, lx: &mut Lexer, name: &str, pending_only: bool) -> R<()> {
let truth = match name {
"iftrue" => true,
"iffalse" => false,
"ifnum" => {
let a = self.scan_number(lx, pending_only)?;
let rel = self.read_relation(lx, pending_only)?;
let b = self.scan_number(lx, pending_only)?;
match rel {
'<' => a < b,
'>' => a > b,
_ => a == b,
}
}
"ifodd" => self.scan_number(lx, pending_only)? % 2 != 0,
"ifdefined" => match self.take(lx, pending_only) {
Some(Token::Cs(n)) => self.meanings.contains_key(&n),
_ => false,
},
"ifx" => {
let a = self.take(lx, pending_only);
let b = self.take(lx, pending_only);
self.meanings_equal(a.as_ref(), b.as_ref())
}
"if" => {
let a = self.expand_one_char(lx, pending_only)?;
let b = self.expand_one_char(lx, pending_only)?;
a == b
}
"ifcase" => {
let n = self.scan_number(lx, pending_only)?;
return self.do_ifcase(lx, n, pending_only);
}
other => return Err(TexError(format!("Unsupported conditional \\{other}"))),
};
match truth {
true => self.conds.push(CondState::Taken),
false => {
let hit_else = self.skip_to(lx, true, pending_only)?;
if hit_else {
self.conds.push(CondState::Done);
}
}
}
Ok(())
}
fn do_ifcase(&mut self, lx: &mut Lexer, n: i64, pending_only: bool) -> R<()> {
let mut remaining = n;
while remaining > 0 {
let hit_else = self.skip_to(lx, true, pending_only)?;
if !hit_else {
return Ok(());
}
remaining -= 1;
}
self.conds.push(CondState::Taken);
Ok(())
}
fn skip_to(&mut self, lx: &mut Lexer, stop_at_else: bool, pending_only: bool) -> R<bool> {
let mut depth = 0usize;
loop {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError("Incomplete \\if; all text was ignored".into()));
};
let Token::Cs(n) = &t else { continue };
match n.name() {
n if CONDITIONALS.contains(&n) => depth += 1,
"fi" => match depth {
0 => return Ok(false),
_ => depth -= 1,
},
"else" | "or" if depth == 0 && stop_at_else => return Ok(true),
_ => {}
}
}
}
fn read_relation(&mut self, lx: &mut Lexer, pending_only: bool) -> R<char> {
loop {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError("Missing = inserted for \\ifnum".into()));
};
match &t {
t if t.is_space() => continue,
Token::Char(c, _) if *c == '<' || *c == '>' || *c == '=' => return Ok(*c),
_ => return Err(TexError("Missing = inserted for \\ifnum".into())),
}
}
}
fn meanings_equal(&self, a: Option<&Token>, b: Option<&Token>) -> bool {
match (a, b) {
(Some(Token::Cs(x)), Some(Token::Cs(y))) => {
match (self.meanings.get(x), self.meanings.get(y)) {
(None, None) => true,
(Some(mx), Some(my)) => mx == my,
_ => false,
}
}
(Some(Token::Char(c1, k1)), Some(Token::Char(c2, k2))) => c1 == c2 && k1 == k2,
_ => false,
}
}
fn expand_one_char(&mut self, lx: &mut Lexer, pending_only: bool) -> R<char> {
loop {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError("Missing token for \\if".into()));
};
match &t {
Token::Char(c, _) => return Ok(*c),
Token::Cs(n) => {
let n = *n;
if !self.try_expand(lx, n, pending_only)? {
return Ok('\u{0}');
}
}
}
}
}
fn do_def(&mut self, lx: &mut Lexer, kind: &str) -> R<()> {
let global = matches!(kind, "gdef" | "xdef") || self.global;
let expand_body = matches!(kind, "edef" | "xdef");
let Some(Token::Cs(name)) = lx.next_token(&self.cats) else {
return Err(TexError("Missing control sequence inserted".into()));
};
let mut params = Vec::new();
loop {
let Some(t) = lx.next_token(&self.cats) else {
return Err(TexError("Runaway definition".into()));
};
if matches!(t, Token::Char(_, Cat::BeginGroup)) {
break;
}
params.push(t);
}
validate_params(¶ms)?;
let raw = self.read_balanced(lx)?;
let body = match expand_body {
true => self.expand_to_tokens(lx, &raw)?,
false => raw,
};
let was = std::mem::replace(&mut self.global, global);
self.set_meaning(name, Meaning::Macro(Macro { params, body }));
self.global = was;
Ok(())
}
fn do_let(&mut self, lx: &mut Lexer) -> R<()> {
let Some(Token::Cs(name)) = lx.next_token(&self.cats) else {
return Err(TexError("Missing control sequence inserted".into()));
};
let mut src = None;
while let Some(t) = lx.next_token(&self.cats) {
match &t {
t if t.is_space() => continue,
Token::Char('=', _) => continue,
other => {
src = Some(*other);
break;
}
}
}
let Some(src) = src else {
return Err(TexError("Missing token for \\let".into()));
};
let meaning = match &src {
Token::Char(c, k) => Meaning::Char(*c, *k),
Token::Cs(n) => match self.meanings.get(n) {
Some(m) => m.clone(),
None => Meaning::Primitive(*n),
},
};
self.set_meaning(name, meaning);
Ok(())
}
fn read_balanced(&mut self, lx: &mut Lexer) -> R<Vec<Token>> {
let mut depth = 1usize;
let mut out = Vec::new();
while let Some(t) = lx.next_token(&self.cats) {
match &t {
Token::Char(_, Cat::BeginGroup) => depth += 1,
Token::Char(_, Cat::EndGroup) => {
depth -= 1;
if depth == 0 {
return Ok(out);
}
}
_ => {}
}
out.push(t);
}
Err(TexError("Runaway argument".into()))
}
fn expand_macro(&mut self, lx: &mut Lexer, name: CsId, pending_only: bool) -> R<()> {
let Some(Meaning::Macro(m)) = self.meanings.get(&name).cloned() else {
return Err(TexError(format!("\\{} is not a macro", name.name())));
};
let args = self.match_params(lx, &m.params, pending_only)?;
let mut out = Vec::with_capacity(m.body.len());
let mut i = 0;
while i < m.body.len() {
match &m.body[i] {
Token::Char(_, Cat::Param) => match m.body.get(i + 1) {
Some(Token::Char(d, _)) if d.is_ascii_digit() && *d != '0' => {
let idx = (*d as u8 - b'1') as usize;
if let Some(a) = args.get(idx) {
out.extend(a.iter().cloned());
}
i += 2;
continue;
}
Some(Token::Char(_, Cat::Param)) => {
out.push(m.body[i + 1]);
i += 2;
continue;
}
_ => out.push(m.body[i]),
},
t => out.push(*t),
}
i += 1;
}
let out = self.weave_advice(name, out)?;
lx.push_back(&out);
Ok(())
}
fn weave_advice(&mut self, name: CsId, expansion: Vec<Token>) -> R<Vec<Token>> {
if self.intercepts.is_empty() || self.advice_depth > 0 {
return Ok(expansion);
}
let advice: Vec<crate::intercepts::Intercept> = self
.intercepts
.matching(name.name())
.into_iter()
.cloned()
.collect();
if advice.is_empty() {
return Ok(expansion);
}
let mut out = expansion;
for a in advice {
let body = self.advice_body(&a.handler)?;
out = match a.advice {
crate::intercepts::Advice::Before => {
let mut v = wrapped(&body);
v.extend(out);
v
}
crate::intercepts::Advice::After => {
let mut v = out;
v.extend(wrapped(&body));
v
}
crate::intercepts::Advice::Around => {
let mut v = Vec::with_capacity(body.len() + out.len());
for t in &body {
match t {
Token::Cs(n) if n.name() == "proceed" => v.extend(out.iter().copied()),
other => v.push(*other),
}
}
wrapped(&v)
}
};
}
Ok(out)
}
fn advice_body(&self, handler: &str) -> R<Vec<Token>> {
let id = CsId::intern(handler);
match self.meanings.get(&id) {
Some(Meaning::Macro(m)) if m.params.is_empty() => Ok(m.body.clone()),
Some(Meaning::Macro(_)) => Err(TexError(format!(
"Intercept handler \\{handler} takes parameters; advice handlers take none"
))),
_ => Err(TexError(format!(
"Intercept handler \\{handler} is not a macro"
))),
}
}
fn match_params(
&mut self,
lx: &mut Lexer,
params: &[Token],
pending_only: bool,
) -> R<Vec<Vec<Token>>> {
let mut args: Vec<Vec<Token>> = Vec::new();
let mut i = 0;
while i < params.len() {
if !matches!(params[i], Token::Char(_, Cat::Param)) {
let want = params[i];
let got = self.take(lx, pending_only);
if got.as_ref() != Some(&want) {
return Err(TexError("Use of macro doesn't match its definition".into()));
}
i += 1;
continue;
}
let delim: Vec<Token> = params[i + 2..]
.iter()
.take_while(|t| !matches!(t, Token::Char(_, Cat::Param)))
.cloned()
.collect();
let arg = match delim.is_empty() {
true => self.read_undelimited(lx, pending_only)?,
false => self.read_delimited(lx, &delim, pending_only)?,
};
args.push(arg);
i += 2 + delim.len();
}
Ok(args)
}
fn read_undelimited(&mut self, lx: &mut Lexer, pending_only: bool) -> R<Vec<Token>> {
loop {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError(
"Paragraph ended before argument was complete".into(),
));
};
if t.is_space() {
continue;
}
return match t {
Token::Char(_, Cat::BeginGroup) => self.read_balanced_from(lx, pending_only),
other => Ok(vec![other]),
};
}
}
fn read_balanced_from(&mut self, lx: &mut Lexer, pending_only: bool) -> R<Vec<Token>> {
if !pending_only {
return self.read_balanced(lx);
}
let mut depth = 1usize;
let mut out = Vec::new();
while let Some(t) = lx.pending.pop() {
match &t {
Token::Char(_, Cat::BeginGroup) => depth += 1,
Token::Char(_, Cat::EndGroup) => {
depth -= 1;
if depth == 0 {
return Ok(out);
}
}
_ => {}
}
out.push(t);
}
Err(TexError("Runaway argument".into()))
}
fn read_delimited(
&mut self,
lx: &mut Lexer,
delim: &[Token],
pending_only: bool,
) -> R<Vec<Token>> {
let mut out: Vec<Token> = Vec::new();
let mut depth = 0usize;
loop {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError(
"Paragraph ended before argument was complete".into(),
));
};
match &t {
Token::Char(_, Cat::BeginGroup) => depth += 1,
Token::Char(_, Cat::EndGroup) => depth = depth.saturating_sub(1),
_ => {}
}
out.push(t);
if depth == 0 && out.len() >= delim.len() && out[out.len() - delim.len()..] == *delim {
out.truncate(out.len() - delim.len());
let wrapped = out.len() >= 2
&& matches!(out[0], Token::Char(_, Cat::BeginGroup))
&& matches!(out[out.len() - 1], Token::Char(_, Cat::EndGroup));
if wrapped {
out.remove(0);
out.pop();
}
return Ok(out);
}
}
}
fn do_catcode(&mut self, lx: &mut Lexer) -> R<()> {
let ch = self.scan_number(lx, false)?;
self.skip_equals(lx)?;
let val = self.scan_number(lx, false)?;
let (Some(c), Some(cat)) = (char::from_u32(ch as u32), cat_from_i64(val)) else {
return Err(TexError("Invalid code".into()));
};
self.set_cat(c, cat);
Ok(())
}
fn do_count_assign(&mut self, lx: &mut Lexer) -> R<()> {
let reg = self.scan_number(lx, false)?;
self.skip_equals(lx)?;
let val = self.scan_number(lx, false)?;
self.set_count(reg, val);
Ok(())
}
fn do_arith(&mut self, lx: &mut Lexer, op: Arith) -> R<()> {
let Some(Token::Cs(what)) = lx.next_token(&self.cats) else {
return Err(TexError("You can't use this after \\advance".into()));
};
if what.name() != "count" {
return Err(TexError(format!("Unsupported register \\{}", what.name())));
}
let reg = self.scan_number(lx, false)?;
self.skip_by(lx)?;
let val = self.scan_number(lx, false)?;
let cur = *self.count.get(®).unwrap_or(&0);
let next = match op {
Arith::Add => cur + val,
Arith::Mul => cur * val,
Arith::Div => match val {
0 => return Err(TexError("Arithmetic overflow".into())),
d => cur / d,
},
};
self.set_count(reg, next);
Ok(())
}
fn skip_by(&mut self, lx: &mut Lexer) -> R<()> {
while let Some(t) = lx.next_token(&self.cats) {
if !t.is_space() {
lx.push_back(&[t]);
break;
}
}
let save = lx.pending.clone();
for want in ['b', 'y'] {
match lx.next_token(&self.cats) {
Some(Token::Char(c, _)) if c.eq_ignore_ascii_case(&want) => {}
other => {
lx.pending = save;
if let Some(t) = other {
lx.push_back(&[t]);
}
return Ok(());
}
}
}
Ok(())
}
fn skip_equals(&mut self, lx: &mut Lexer) -> R<()> {
while let Some(t) = lx.next_token(&self.cats) {
match &t {
t if t.is_space() => continue,
Token::Char('=', _) => return Ok(()),
other => {
lx.push_back(std::slice::from_ref(other));
return Ok(());
}
}
}
Ok(())
}
fn scan_number(&mut self, lx: &mut Lexer, pending_only: bool) -> R<i64> {
let mut sign = 1i64;
let mut cur = loop {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError("Missing number, treated as zero".into()));
};
match &t {
t if t.is_space() => continue,
Token::Char('-', _) => {
sign = -sign;
continue;
}
Token::Char('+', _) => continue,
other => break *other,
}
};
if matches!(cur, Token::Char('`', _)) {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError("Missing number".into()));
};
let code = match t {
Token::Char(c, _) => u32::from(c) as i64,
Token::Cs(n) => n
.name()
.chars()
.next()
.map(|c| u32::from(c) as i64)
.unwrap_or(0),
};
return Ok(sign * code);
}
if let Token::Cs(name) = &cur {
let name = *name;
if name.name() == "count" {
let reg = self.scan_number(lx, pending_only)?;
return Ok(sign * *self.count.get(®).unwrap_or(&0));
}
if self.try_expand(lx, name, pending_only)? {
return Ok(sign * self.scan_number(lx, pending_only)?);
}
return Err(TexError(format!("Missing number, found \\{}", name.name())));
}
let mut digits = String::new();
loop {
match &cur {
Token::Char(c, _) if c.is_ascii_digit() => digits.push(*c),
other if other.is_space() && !digits.is_empty() => break,
other => {
lx.push_back(std::slice::from_ref(other));
break;
}
}
match self.take(lx, pending_only) {
Some(t) => cur = t,
None => break,
}
}
if digits.is_empty() {
return Err(TexError("Missing number, treated as zero".into()));
}
digits
.parse::<i64>()
.map(|n| sign * n)
.map_err(|_| TexError("Number too big".into()))
}
fn read_group_text(&mut self, lx: &mut Lexer) -> R<String> {
loop {
let Some(t) = lx.next_token(&self.cats) else {
return Err(TexError("Missing { inserted".into()));
};
if t.is_space() {
continue;
}
if !matches!(t, Token::Char(_, Cat::BeginGroup)) {
return Err(TexError("Missing { inserted".into()));
}
break;
}
let body = self.read_balanced(lx)?;
self.expand_to_text(lx, &body)
}
fn expand_to_tokens(&mut self, lx: &mut Lexer, toks: &[Token]) -> R<Vec<Token>> {
let saved: Vec<Token> = std::mem::take(&mut lx.pending);
lx.push_back(toks);
let mut out = Vec::new();
let mut steps = 0usize;
while let Some(t) = lx.pending.pop() {
steps += 1;
if steps > 200_000 {
return Err(TexError("TeX capacity exceeded".into()));
}
match &t {
Token::Cs(name) => {
let name = *name;
if !self.try_expand(lx, name, true)? {
out.push(t);
}
}
_ => out.push(t),
}
}
lx.pending = saved;
Ok(out)
}
fn expand_to_text(&mut self, lx: &mut Lexer, toks: &[Token]) -> R<String> {
let saved: Vec<Token> = std::mem::take(&mut lx.pending);
lx.push_back(toks);
let mut out = String::new();
let mut steps = 0usize;
while let Some(t) = lx.pending.pop() {
steps += 1;
if steps > 200_000 {
return Err(TexError("TeX capacity exceeded".into()));
}
match &t {
Token::Cs(name) if name.name() == "the" || name.name() == "number" => {
let n = self.read_the(lx, name.name() == "number")?;
out.push_str(&n);
}
Token::Cs(name) if name.name() == "string" => {
if let Some(next) = lx.pending.pop() {
out.push_str(&match &next {
Token::Cs(n) => format!("{}{}", self.escape, n.name()),
other => other.to_text(self.escape),
});
}
}
Token::Cs(name) => {
let name = *name;
if !self.try_expand(lx, name, true)? {
out.push_str(&t.to_text(self.escape));
}
}
other => out.push_str(&other.to_text(self.escape)),
}
}
lx.pending = saved;
Ok(out)
}
fn read_the(&mut self, lx: &mut Lexer, bare: bool) -> R<String> {
if bare {
return Ok(self.scan_number(lx, true)?.to_string());
}
let Some(Token::Cs(what)) = lx.pending.pop() else {
return Err(TexError("You can't use \\the here".into()));
};
if what.name() != "count" {
return Err(TexError(format!("Unsupported \\the\\{}", what.name())));
}
let reg = self.scan_number(lx, true)?;
Ok(self.count.get(®).unwrap_or(&0).to_string())
}
fn read_csname(&mut self, lx: &mut Lexer, pending_only: bool) -> R<String> {
let mut name = String::new();
loop {
let Some(t) = self.take(lx, pending_only) else {
return Err(TexError("Missing \\endcsname inserted".into()));
};
match &t {
Token::Cs(n) if n.name() == "endcsname" => return Ok(name),
Token::Char(c, _) => name.push(*c),
Token::Cs(n) => {
let n = *n;
if !self.try_expand(lx, n, pending_only)? {
return Err(TexError(format!(
"Missing \\endcsname before \\{}",
n.name()
)));
}
}
}
}
}
}
enum Arith {
Add,
Mul,
Div,
}
impl Default for Engine {
fn default() -> Self {
Self::new()
}
}
impl Engine {
pub fn compile_time_def(&mut self, lx: &mut Lexer, kind: &str) -> R<()> {
self.do_def(lx, kind)
}
pub fn compile_time_catcode(&mut self, lx: &mut Lexer) -> R<()> {
self.do_catcode(lx)
}
pub fn scan_number_file(&mut self, lx: &mut Lexer) -> R<i64> {
self.scan_number(lx, false)
}
pub fn scan_number_pending(&mut self, lx: &mut Lexer) -> R<i64> {
self.scan_number(lx, true)
}
pub fn skip_equals_file(&mut self, lx: &mut Lexer) -> R<()> {
self.skip_equals(lx)
}
pub fn skip_by_file(&mut self, lx: &mut Lexer) -> R<()> {
self.skip_by(lx)
}
pub fn read_relation_file(&mut self, lx: &mut Lexer) -> R<char> {
self.read_relation(lx, false)
}
pub fn expand_macro_file(&mut self, lx: &mut Lexer, name: CsId) -> R<()> {
self.expand_macro(lx, name, false)
}
pub fn expand_macro_pending(&mut self, lx: &mut Lexer, name: CsId) -> R<()> {
self.expand_macro(lx, name, true)
}
pub fn read_message_body(&mut self, lx: &mut Lexer) -> R<Vec<Token>> {
loop {
let Some(t) = lx.next_token(&self.cats) else {
return Err(TexError("Missing { inserted".into()));
};
if t.is_space() {
continue;
}
if !matches!(t, Token::Char(_, Cat::BeginGroup)) {
return Err(TexError("Missing { inserted".into()));
}
break;
}
self.read_balanced(lx)
}
}
impl Engine {
pub fn compile_time_let(&mut self, lx: &mut Lexer) -> R<()> {
self.do_let(lx)
}
pub fn advice_marker(&mut self, name: &str) -> bool {
match name {
ADVICE_IN => {
self.advice_depth += 1;
true
}
ADVICE_OUT => {
self.advice_depth = self.advice_depth.saturating_sub(1);
true
}
_ => false,
}
}
pub fn compile_time_intercept(&mut self, lx: &mut Lexer) -> R<()> {
let kind = self.group_text(lx)?;
let advice = crate::intercepts::Advice::parse(&kind).ok_or_else(|| {
TexError(format!(
"Unknown intercept kind `{kind}'; use before, after or around"
))
})?;
let pattern = self.group_text(lx)?;
let handler = self.group_cs_name(lx)?;
if let Some(frame) = self.groups.last_mut() {
frame.push(Save::Intercepts(self.intercepts.clone()));
}
self.intercepts
.register(&pattern, advice, &handler)
.map_err(TexError)
}
fn group_text(&mut self, lx: &mut Lexer) -> R<String> {
let toks = self.next_group(lx)?;
Ok(toks
.iter()
.filter_map(|t| match t {
Token::Char(c, _) => Some(*c),
Token::Cs(_) => None,
})
.collect())
}
fn group_cs_name(&mut self, lx: &mut Lexer) -> R<String> {
let toks = self.next_group(lx)?;
match toks.iter().find(|t| matches!(t, Token::Cs(_))) {
Some(Token::Cs(n)) => Ok(n.name().to_string()),
_ => Err(TexError(
"Intercept handler must be a control sequence, as in {\\handler}".into(),
)),
}
}
fn next_group(&mut self, lx: &mut Lexer) -> R<Vec<Token>> {
while let Some(t) = lx.next_token(&self.cats) {
match &t {
_ if t.is_space() => continue,
Token::Char(_, Cat::BeginGroup) => return self.read_balanced(lx),
_ => return Err(TexError("Missing { for \\intercept".into())),
}
}
Err(TexError("Runaway \\intercept: missing {".into()))
}
pub fn compile_time_begin_group(&mut self) {
self.begin_group();
}
pub fn compile_time_end_group(&mut self) -> R<()> {
self.end_group()
}
pub fn set_global_prefix(&mut self, on: bool) {
self.global = on;
}
pub fn ifx_equal(&mut self, lx: &mut Lexer) -> R<bool> {
let a = lx.next_token(&self.cats);
let b = lx.next_token(&self.cats);
Ok(self.meanings_equal(a.as_ref(), b.as_ref()))
}
pub fn take_file(&mut self, lx: &mut Lexer) -> Option<Token> {
lx.next_token(&self.cats)
}
}
impl Engine {
pub fn is_macro(&self, name: CsId) -> bool {
matches!(self.meanings.get(&name), Some(Meaning::Macro(_)))
}
pub fn read_balanced_pub(&mut self, lx: &mut Lexer) -> R<Vec<Token>> {
self.read_balanced(lx)
}
pub fn read_csname_pending(&mut self, lx: &mut Lexer) -> R<String> {
self.read_csname(lx, true)
}
pub fn read_relation_pending(&mut self, lx: &mut Lexer) -> R<char> {
self.read_relation(lx, true)
}
pub fn meanings_equal_pub(&self, a: Option<&Token>, b: Option<&Token>) -> bool {
self.meanings_equal(a, b)
}
pub fn define_macro(&mut self, name: CsId, body: Vec<Token>) {
self.set_meaning(
name,
Meaning::Macro(Macro {
params: Vec::new(),
body,
}),
);
}
}
fn validate_params(params: &[Token]) -> R<()> {
let mut expect = b'1';
let mut i = 0;
while i < params.len() {
if !matches!(params[i], Token::Char(_, Cat::Param)) {
i += 1;
continue;
}
match params.get(i + 1) {
Some(Token::Char(c, _)) if *c as u8 == expect => {
expect += 1;
i += 2;
}
Some(Token::Char(c, _)) if c.is_ascii_digit() => {
return Err(TexError("Parameters must be numbered consecutively".into()))
}
None => return Err(TexError("`#{` parameter text is not implemented".into())),
_ => return Err(TexError("Illegal parameter number in definition".into())),
}
}
Ok(())
}