lower_expression_templates
Stable function · Load-time lowering and templates
Tier: stable · Kind: function · Topic: Load-time lowering and templates
| Julia | TypeScript | Python | Rust | Go |
|---|---|---|---|---|
lower_expression_templates | lowerExpressionTemplates | lower_expression_templates | not exported | LowerExpressionTemplates |
Julia
lower_expression_templates(raw_data)
lower_expression_templates(raw_data) -> OrderedDict{String,Any}
Run the §9.6.3 rewrite fixpoint over raw_data against each component-local
expression_templates block (esm-spec §9.6.4, Option B): auto match rules
fire, EAGER (target-bearing) apply_expression_template references are
expanded, NON-EAGER references survive as leaves, and each component’s
registered expression_templates block is RETAINED on the returned tree. The
output is the rewritten native OrderedDict{String,Any} document, ready for
coerce_esm_file. A file with no template machinery is returned UNCHANGED
(the same object, by identity — callers use that to detect the fast path).
Throws ExpressionTemplateError
on any of:
- file declares
esm< 0.4.0 but uses templates apply_expression_templatereferences an undeclared template name, or amatchrewrite rule (only match-less templates are invocable by name)- bindings do not exactly match the template’s
params - template-body references form a cycle or exceed
MAX_TEMPLATE_EXPANSION_DEPTH(nested body references themselves are legal, esm-spec §9.7.3) - the
match-rule fixpoint fails to converge withinMAX_REWRITE_PASSES - declaration is malformed (params missing, body missing, etc.)
TypeScript
function lowerExpressionTemplates<T extends object>(input: T, options?: LowerOptions): T
Load the file under Option B (reference preservation, esm-spec §9.6.4): fire
auto-applied match rules to a fixpoint per component (outermost-first,
priority-ordered, bounded — §9.6.3), each firing writing a reference to its
rule (constraint 7); then rewrite the bodies of the templates the references
reach, once per template instance (§9.6.4 rule 3). No reference is expanded:
every apply_operator reference survives and denotes its
expansion (§9.6.4 rule 2). The per-component operators
registries are RETAINED (with referenced rules materialized match-less and
any <name>.variant<k> entries) — they are what expandDocument
consumes (rule 2) and emit materializes (rule 5). Call-site checks (§9.6.9)
and the registration-time makearray-region check run on the
reference-preserving form.
Returns a new file object; the input is not mutated.
Pre-condition: the input has been schema-validated.
Python
lower_expression_templates(file: dict) -> dict
Run the load-time rewrite fixpoint (esm-spec §9.6.3) over file,
Option B (reference-preserving, esm-spec §9.6.4).
Per component, an outermost-first / priority-ordered / bounded-fixpoint
rewrite (_rewrite_to_fixpoint) fires the component’s match
rewrite rules; each firing writes a reference to its rule (esm-spec §9.6.3
constraint 7) and references survive (§9.6.4 rule 4). Coupling
variable_map transforms are rewritten with the receiving component’s
rules. Then the rules apply inside the bodies of the templates the
references reach, once per template instance (§9.6.4 rule 3,
earthsci_ast.template_instances.apply_rule3), which may rewrite a
body in place, add <name>.variant<k> entries, and materialize the
referenced rules without match. The per-component
expression_templates blocks are RETAINED — emit materializes them
(§9.6.4 rule 5) and expand_document
consumes them. Returns a new
dict (does not mutate input). A rewrite deeper than MAX_REWRITE_PASSES
(counted through template bodies) is rewrite_rule_nonterminating; call
sites are checked for unknown template / bindings mismatch (§9.6.9).
Pre-condition: the input has been schema-validated.
Rust
Rust does not export this function.
Go
func LowerExpressionTemplates(view map[string]any) error
LowerExpressionTemplates runs the outermost-first + priority + bounded-fixpoint rewrite over view and strips the operators blocks. Mutates view in place. Declaration order for match-rule tie-breaking is recovered via sorted-name fallback (an already-decoded map has no key order); callers with the raw JSON string should use the load path (applyExpressionTemplatesToJSON), which recovers genuine declaration order.
Pre-condition: the input has been schema-validated.
esm-spec §9.6.4 (Option B): lowering itself PRESERVES surviving apply_operator references and per-component registries; this entry point then Expands them in place (RFC out-of-line-expression-templates §7.7 Expand-at-build), so callers see the Option-A image (references replaced by their expansion, registries stripped) exactly as before. The reference-preserving form is available via ResolveAndLowerReferencePreserving (raw §9.7 pipeline) or EmitReferencePreserving (canonical emitted bytes).