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 emitsquote!, - the runtime interpreter
apply_opswalks the same AST against a liveDocument.
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§
- Attr
Value - An interpolated attribute value:
'role-#1'→[Literal("role-"), Value(Arg 1)].
Enums§
- Attr
Pair - An attribute-list entry inside a
<tag …>or<?pi …?>— a key/value pair or a conditional set of pairs (Perltranslate_avpairs). - Attr
Part - One piece of an
AttrValue. - Float
Kind - 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 toDouble(counts ≥3 never occur in practice). - FuncArg
- A
&func(…)argument — either a bare value or a quoted interpolated string. - Replacement
Op - 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 isReplacementOp::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
ReplacementOpop-list. The entry point for both consumers (codegen at compile time,apply_opsat 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 originalESCAPED_OPregex has no capture group, so the replacement is the empty string), then##→#and&→&.