Skip to main content

Module replacement

Module replacement 

Source
Expand description

Shared XML-replacement template AST + parser + runtime interpreter (#171).

A constructor’s "<ltx:…>" replacement is a tiny templating language (see the grammar comment in latexml_codegen::constructable). Historically it had two independent implementations: the compile-time proc-macro (latexml_codegen/src/constructable.rs, a regex-strip state machine fused to codegen) and the runtime byte-scanner (latexml_contrib::script_bindings). Two implementations of one language is drift waiting to happen.

This module is the single source of truth: one [winnow] parser produces one ReplacementOp AST, consumed by both front-ends —

  • the compile-time codegen walks &[ReplacementOp] and emits quote!,
  • the runtime interpreter apply_ops walks the same AST against a live Document.

The semantics mirror the Perl LaTeXML::Core::Definition::Constructor::Compiler (Compiler.pm) faithfully — the existing constructable.rs is the ground truth this reproduces, including its quirks (see unquote). The dialect:

 #1..#9            n-th digested argument                 (Value::Arg)
 #name             named whatsit property                 (Value::Prop)
 &func(args,…)     function call (whitelisted at runtime)  (Value::Func)
 <q a='v' …>       open element + attributes              (OpenElement)
 <q … />           empty element (open + close)
 </q>              close element                          (CloseElement)
 <?q a='v' …?>     processing instruction                 (ProcessingInstruction)
 ?test(if)(else)   conditional                            (Conditional)
 ^ / ^^  prefix    float the next element/attribute       (FloatKind)
 key='v'           set attribute on current node          (SetAttribute)
 literal text      absorb as a string                     (Text)

Structs§

AttrValue
An interpolated attribute value: 'role-#1'[Literal("role-"), Value(Arg 1)].

Enums§

AttrPair
An attribute-list entry inside a <tag …> or <?pi …?> — a key/value pair or a conditional set of pairs (Perl translate_avpairs).
AttrPart
One piece of an AttrValue.
FloatKind
A ^ / ^^ float prefix attaching to the next open-element. ^ floats to where the element is allowed; ^^ additionally closes intervening open elements if possible (Perl Compiler.pm float_type 1 vs 2). Counts ≥2 collapse to Double (counts ≥3 never occur in practice).
FuncArg
A &func(…) argument — either a bare value or a quoted interpolated string.
ReplacementOp
One operation in a compiled replacement template.
Value
A substitutable value: #n, #name, &func(…), or literal text. Literal only arises inside function arguments / attribute strings — never at content position (a bare literal there is ReplacementOp::Text).

Functions§

apply_ops
Execute a parsed replacement against a live Document — the runtime consumer of the AST. Mirrors the Document operations the compile-time codegen emits.
parse_replacement
Parse an XML-replacement template into a ReplacementOp op-list. The entry point for both consumers (codegen at compile time, apply_ops at runtime).
slashify
Double every backslash so the text can be embedded as a Rust string literal (Perl slashify). Used by the compile-time codegen consumer.
unquote
Reverse the template’s escape conventions (Perl unquote). Reproduces the original’s exact behavior, including the quirk that \X (X ∈ #?(&,<>\%) is removed entirely (the original ESCAPED_OP regex has no capture group, so the replacement is the empty string), then ### and &amp;&.