Skip to main content

Module local_assignments

Module local_assignments 

Source
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 scope

The explicit set_*/expire_* pairs still exist for cases where RAII doesn’t fit (e.g., cross-function push/pop with different lifetimes).

Structs§

LocalGuard
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.
SuppressedTabMarks
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 raises align_state by 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 own macro_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 by restore_align_group_count after a digestion whose tokens are not the document’s input.
open_conditional_count
Number of currently open conditional frames (\if… whose \fi has 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_count read: 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) IfFrame that 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 caller on the alignment template being built, if one is. A column type’s \NC@rewrite@<c> expanded outside a preamble (an \edef that meets it) builds nothing: in TeX it only yields tokens there (\NC@find p{…}), and unwrapping the missing template panicked (2605.18869: tabularx’s X, current inside a tabularx since 61s, met by an \edef while an algorithm’s unclosed group was open).
with_current_build_template