Expand description
Perl-style local assignments (dynamic scope)
This module provides stack-based dynamic scoping for global state,
matching Perl’s local mechanism. Each “localized” field uses a Vec<T>
as a stack: push to shadow, pop to restore.
§RAII Guards
Each localized variable has a corresponding Guard type that pushes on
creation and pops on Drop. This prevents leaked state on early returns
or panics:
let _guard = local_current_token_guard(token);
// ... do work ...
// guard auto-pops when _guard goes out of scopeThe explicit set_*/expire_* pairs still exist for cases where
RAII doesn’t fit (e.g., cross-function push/pop with different lifetimes).
Structs§
- Local
Guard - Generic RAII guard that calls an expire function on drop. Zero-cost when the expire function is a simple fn pointer.
- Localized
- These are fields realized via Perl’s “local” mechanism in LaTeXML, but (for now) require explicit “expire” calls in Rust. Ideally their ergonomics gets improved, or we gradually phase them out.
- Suppressed
TabMarks - Disables tab marks for the lifetime of the guard, restoring the previous
count on drop — what ltcmd does around every argument it reads: latex.ltx:2103-2110
(
\__cmd_start:nNNnnn) opens\group_align_safe_begin:(closed at :2128), which raisesalign_stateby the backquote-constant rule (tex.web §442), and physics.sty does it by hand around its fenced reads ({\ifnum\z@=}\fi, physics.sty:35/74/141). TeX's ownmacro_call(§389-399) does NOT disable tab marks — the{disable tab marks, etc.}ofalign_state:=1000000isread_toks, behind\read(tex.web §482) — so a plain macro's delimited argument holding an&inside an alignment errs in TeX too ("Argument of \… has an extra }"); arming this inParameters::read_argumentswould be unfaithful. For an ltcmd/physics reader, without it a&in a **delimiter-fenced** argument reaches the alignment as a cell break: for\mqty( b_0 &0 \ 0 &b_1 )(physics.sty, witness 2605.05903) the row splits mid-argument and the alignment then cannot close its own group —Error:unexpected:\lx@begin@alignment Attempt to close a group that switched to mode restricted_horizontal— truncating the rest of the document. The brace form\mqty{…}was always safe because cell scanning skips balanced groups;(…)is not a group. Perl raises the identical error (11 of them on the 14-line repro), so this is beyond-Perl andpdflatex`, which renders the repro silently, is the ground truth.
Functions§
- align_
group_ count - decrement_
align_ group_ count - expire_
align_ group_ count - expire_
current_ token - expires the most recent (localized) current token.
- expire_
dual_ branch - expire (localized) flag for “dual branch”
- expire_
ifframe - expires the most recent (originally Perl-local)
IfFrame - expire_
reading_ alignment - expire_
state_ unlocked - get_
current_ token - gets the (localized) current token
- get_
dual_ branch - get the current value for “dual branch”
- get_
ifframe - retrieves the most recent (originally Perl-local)
IfFrame - get_
reading_ alignment - has_
reading_ alignment - increment_
align_ group_ count - initialize_
localized - Reset all localized variables to their default state. Must be called between conversion runs to prevent state pollution.
- local_
align_ group_ count - local_
align_ group_ count_ guard - Push a localized align group count; returns guard that pops on drop.
- local_
build_ template - local_
current_ token - localizes a new current token. see
Stomach::invoke_token - local_
current_ token_ guard - Push a localized current token; returns guard that pops on drop.
- local_
dual_ branch_ guard - Push a localized dual branch; returns guard that pops on drop.
- local_
ifframe_ guard - Push a localized if-frame; returns guard that pops on drop.
- local_
reading_ alignment - local_
reading_ alignment_ guard - Push a localized reading alignment; returns guard that pops on drop.
- local_
state_ unlocked - local_
state_ unlocked_ guard - Push a localized state unlocked flag; returns guard that pops on drop.
- mark_
align_ group_ count - The alignment brace count as it stands (tex.web’s
align_state, with its stack of saved values), to be put back byrestore_align_group_countafter a digestion whose tokens are not the document’s input. - open_
conditional_ count - Number of currently open conditional frames (
\if…whose\fihas not been digested yet). Zero at any clean point between top-level constructs — the fragment-yield seam predicate requires exactly that. - restore_
align_ group_ count - Puts back the alignment brace count
mark_align_group_countread: the stack at its depth then, the current value as it was. A{the abandoned digestion scanned and never closed is no longer counted against the document’s&s. - set_
align_ group_ count - set_
build_ template - set_
dual_ branch - sets the (localized) flag for “dual branch”
- set_
ifframe - sets a (originally Perl-local)
IfFramethat needs to be manually expired. - state_
is_ relocked - Whether a reader re-locked the state explicitly: code that is not the document’s own text, a raw file’s or a hook’s
(
\AtBeginDocument’s, run by\begin{document}), as Perl runs them under$UNLOCKED=0. - state_
is_ unlocked - take_
build_ template - with_
building_ template - Runs
calleron the alignment template being built, if one is. A column type’s\NC@rewrite@<c>expanded outside a preamble (an\edefthat meets it) builds nothing: in TeX it only yields tokens there (\NC@find p{…}), and unwrapping the missing template panicked (2605.18869: tabularx’sX, current inside a tabularx since 61s, met by an\edefwhile an algorithm’s unclosed group was open). - with_
current_ build_ template